Advanced Play Mode Functions

Saving objects

You might want to save a Sloyd object at runtime. If, for example, a player created an object while playing and she wants to save her progress and get back to it later.

The way to accomplish this you need to turn the object, at it's current state, into a json file and save it (in Sloyd objects are made out of code that's stored in json). Then, at loading, you need to retrieve the json file and create a mesh object.

First for exporting a Sloyd object into json file, follow these steps:

  • Create a JsonExporter class

JsonExporter jsonExporter = new JsonExporter();
  • Then run this function

sloydObject.GetControls().MakeCurrentValuesAsDefault();
  • You're ready to export the file

jsonExporter.Export(Path.Combine(folderPath, "MyObject.sloyd"), sloydObject);

Alternatively, you can manually save the file with:

byte[] bytes = jsonExporter.GetBytes(sloydObject);

Now when it's time to load a game with Sloyd objects, you'll need to create a SloydObject instance via its constructor:

string jsonText = GetJsonText(); // read text file from disc
SloydObject sloydObject = new SloydObject(jsonText, "MyObject");
sloydObject.RebuildMesh();

Fetching object metadata

When an object is generated out of a text prompt it also comes with metadata. One important field in the metadata is 'confidence score'. The confidence score is a ranking of how close the generated model is to the text prompt. You can read more about it in Using Confidence Score (https://doc.clickup.com/d/h/kh4k0-705/ed114594b2af381/kh4k0-8035)

First, make this call to fetch metadata:

SloydAIHelper.ClipResponse response = await SloydAIHelper.ClipRequest(promptText, Token); 

The ClipResponse contains the following metadata:

public struct ClipResponse
{
    public float Confidence { get; set; }
    public float CurrrentThreshold { get; set; }
    public bool Doubt { get; set; }
    public Dictionary<string, float> JsonDefaults { get; set; }
    public string ObjectCategory { get; set; }
    public string ObjectId { get; set; }
    public string Variant { get; set; }
}

Once you get a response, the call you can call:

SloydObject object = await RequestSloydObject(response, cachedTemplatesSO, paletteCollectionScriptableObject);

To get your sloyd object created.

Configuring object properties

A Sloyd object can be turned into a static object. You can read more about it in Edit Mode (https://doc.clickup.com/d/h/kh4k0-705/ed114594b2af381/kh4k0-7555) at the last section "Configuring Sloyd object properties".

To change a dynamic Sloyd object to a static object at runtime you need to:

SloydObjectContainer container = sloydObject.Container;
container.IsStatic = true;

To change an object back to a dynamic object at runtime you need to:

SloydObjectContainer container = sloydObject.Container;
container.IsStatic = false;

To switch a Sloyd object to 'Solid Mesh' (see details in) mode you need to:

sloydObject.IsSolid = true;
sloydObject.RebuildMesh();

To make a Sloyd object non interactable (see details in) you need to:

SloydObjectContainer container = sloydObject.Container;
container.IsInteractable = false;