3. Update the snapshot

In this section we’re going to add a health pack entity to the SpatialOS world. There are two ways to do this:

  • At runtime, by sending a CreateEntity command with an EntityTemplate object to the SpatialOS Runtime.
  • At start-up, by adding a health pack entity to the Snapshot, so it's loaded into the world when the SpatialOS Runtime loads.

We will do the latter, so that when the game begins there will already be a health pack in a pre-defined location.

The SpatialOS menu in your Unity Editor contains a "Generate FPS Snapshot" option. This menu item runs Improbable.Gdk.Fps.GenerateFpsSnapshot(). We will now modify this function to add a HealthPack entity to our snapshot:

Step 1. In your Unity Editor, locate Assets/Fps/Scripts/Editor/SnapshotGenerator/SnapshotMenu.cs and open it in your code editor.

Step 2. Copy and paste the function below into the SnapshotMenu class.

private static void AddHealthPacks(Snapshot snapshot)
{
    // Invoke our static function to create an entity template of our health pack with 100 heath.
    var healthPack = FpsEntityTemplates.HealthPickup(new Vector3(5, 0, 0), 100);

    // Add the entity template to the snapshot.
    snapshot.AddEntity(healthPack);
}

In your own game may want to consider moving default values (such as health pack positions, and health values) into a settings file. But for now, we will keep this example simple.

Step 3. Copy and paste the snippet below inside GenerateDefaultSnapshot() to call your new function.

AddHealthPacks(snapshot);

Your SnapshotMenu will now look like this:

using System.IO;
using Fps.Config;
using Improbable;
using Improbable.Gdk.Core;
using UnityEditor;
using UnityEngine;

namespace Fps.Editor
{
    public class SnapshotMenu : MonoBehaviour
    {
        private static readonly string DefaultSnapshotPath =
            Path.Combine(Application.dataPath, "../../../snapshots/default.snapshot");

        private static readonly string CloudSnapshotPath =
            Path.Combine(Application.dataPath, "../../../snapshots/cloud.snapshot");

        [MenuItem("SpatialOS/Generate FPS Snapshot")]
        private static void GenerateFpsSnapshot()
        {
            SaveSnapshot(DefaultSnapshotPath, GenerateDefaultSnapshot());
            SaveSnapshot(CloudSnapshotPath, GenerateDefaultSnapshot());
        }

        private static Snapshot GenerateDefaultSnapshot()
        {
            var snapshot = new Snapshot();
            snapshot.AddEntity(FpsEntityTemplates.Spawner(Coordinates.Zero));
            AddHealthPacks(snapshot);
            return snapshot;
        }

        private static void SaveSnapshot(string path, Snapshot snapshot)
        {
            snapshot.WriteToFile(path);
            Debug.LogFormat("Successfully generated initial world snapshot at {0}", path);
        }

        private static void AddHealthPacks(Snapshot snapshot)
        {
            // Invoke our static function to create an entity template of our health pack with 100 health.
            var healthPack = FpsEntityTemplates.HealthPickup(new Vector3(5, 0, 0), 100);

            // Add the entity template to the snapshot.
            snapshot.AddEntity(healthPack);
        }
    }
}

Step 4. Regenerate our snapshot. From the Unity Editor, select SpatialOS > Generate FPS Snapshot.

Now that you have changed the snapshot generation script in SnapshotMenu.cs, we will need to regenerate the snapshot to see the new HealthPickup entity appear in your game world.

You can validate that the snapshot was updated by launching a local deployment (Ctrl + L/Cmd + L in your Unity Editor) and looking in the Inspector.

Note: If you have a local deployment running, make sure to close it before launching a new one!

If we were to test the game at this point, the health pack entity would appear in the inspector but not in-game. This is because we have not yet defined how to represent the entity on your client or server-workers. We'll do this in the next section.

The Inspector uses the entity_type string field from the schema standard library component Metadata as the entity name.

All SpatialOS GDK projects contain a directory named snapshots in the root of the project. Your snapshots can be found in that directory: gdk-for-unity-fps-starter-project/snapshots.

There is a spatial command that will convert snapshots to a human-readable format. However, you cannot launch a deployment from a human-readable snapshot, so it must be converted back to binary before it is usable. To find out more about working with snapshots you can read about the spatial snapshot command.

While they are human-readable and you can manually edit the values of the properties within, be careful to not make mistakes that will inhibit the conversion back to binary form!

Step 5. Before you move on, in the terminal window that's running the SpatialOS process, enter Ctrl+C or stop the process.

Updated about a year ago



3. Update the snapshot


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.