Saving level layout to file with Scriptable objects

I built a level builder for a simple match 3 game.
I have a Board class that is sets up of Tiles which is also a class. On each Tile I can place a Block.
The Block is a simple MB class:

public class Block : MonoBehaviour
SerializedField]
[private BlockType blockType;

// more stuff below
}

Each block has a type which is a ScriptableObject class named BlockType.
I use it as Extendable Enum (I saw on YT about using SO’s as extendable Enums and I really liked the idea)
In the editor I’m making a new BlockType asset with a different name for each playable block these are my Enum values and also my dictionary keys.

The basic of BlockType looks like this:

public class BlockType : ScriptableObject
{
public string BundleName;

// more stuff will be added in the future
}

The level builder is for internal use and its purpose is to give a colleague of mine the ability to create level layouts and save it (to file on disk/server etc.) so we can later load it at runtime to the game scene. (not making a scene for each level of course).

I tried a few serialization methods to save the layout but without success. It always boils down to some problems with the List of scriptable objects not serializing/deserializing well. I want to avoid mappings from string to BlockType cause it’s just hard to maintain and defeats the purpose I used SO’s from the beginning.
Also, I would like an opinion on my method of doing stuff. If there is a better way to do it I would love to hear about it.

Cheers.

1 Like

Anyone?

You need a lookup of some sort, and a persistent unique id attached to the BlockType.
Whether it be string, or int or anything else if up to you.

So:

  1. Setup a unique id field in BlockType class;
  2. Upon deserialization through your editor, reach the lookup of those <id, BlockType> and fetch actual ScriptableObjects instead of ids.
  3. Done.

Lookup can be anything really, depends which suites you best.
It could be MonoBehaviour with Dictionary<id, BlockType> costructed from the List of all BlockTypes.
Or ScriptableObject list of all BlockTypes of the same maner.

On serialization:

  1. Write unique ids to the file instead of List;
  2. Done.

Yes, as I said at the last paragraph of my original post I was trying to avoid that cause it seems a bit fragile and need extra steps to maintain. But maybe I’m over-complicating this. Thanks!