0
\$\begingroup\$

I've invented an innovative, new game mechanic for Bricker Breaker Quest and want to create a quick prototype to recruit a game development team. After wasting a week researching the best way to easily represent the simple level data, I reluctantly started using Scriptableobjects. Each level is defined as a List<Block>, where :

public class Block : MonoBehaviour, IComparer<Block>
{
    public int x;
    public int y;

    public int lives = 0;
    private int pastLife = 0;

    private SpriteRenderer spriteRenderer;
    private TextMeshPro text;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        lives--;

        if (lives > 0)
            UpdateVisualState();
        else
        {
            gameObject.SetActive(false);
        }
    }

    private void Awake()...

    public void AddLives(int life)...
}

How do I properly serialize the Block object so that I can enter the X, Y, and Lives integers directly in the Inspector? As a quick hack, I created a List<Vector3Int> instead of the Block List and use the Z input field to enter the lives. Seems like overkill to create a PropertyDrawer just to relabel one field.

Any other advice is appreciated.

\$\endgroup\$
2

1 Answer 1

2
\$\begingroup\$

Do you need the Block class to be a MonoBehaviour? if not, then why not making it a class with the Serializable attribute? That way it will show on the inspector, because otherwise by making a list of MonoBehaviours you will only make object fields of that class on the inspector.

[System.Serializable]
public class Block : IComparer<Block>
{
    public int x;
    public int y;
    public int z;

    public int Compare<Block a, Block b>
    {
        /// Do whatever evaluation you want to make.
        return 0;
    }
}

And if you need it to be on either a MonoBehaviour or ScriptableObject, then just make a member of Block type:

public class Mono : MonoBehaviour
{
    public Block block;
}

// Or...

public class ScriptableClass : ScriptableObject
{
    public Block block;
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'd recommend a struct for this data — similar effect, but a little lighter weight on allocations / garbage management. \$\endgroup\$
    – DMGregory
    Apr 27, 2019 at 22:37
  • \$\begingroup\$ Totally agree on that. I just copied and modified the asker's code for the sake of giving a quick example. \$\endgroup\$ Apr 28, 2019 at 0:42
  • \$\begingroup\$ I'm afraid I underrepresented the Block class properties and methods for simplicity. If the block class is controlling it's Active state and contains both a SpriteRenderer and a TextMeshPro object, it needs to be a MonoBehaviour, right? Can the 2nd recommendation work with the updated class data? \$\endgroup\$
    – MXMLLN
    Apr 28, 2019 at 7:17
  • 1
    \$\begingroup\$ Yes, it would work, but then instead of referencing a Block object, you could instead reference the MonoBehaviour that holds a Block object, so you could get both the active state and the TextMeshPro reference from that same MonoBehaviour. And yes, for the sake of more clarity it would be better to show the main members of a class so anyone here can find out a solution taking those key members into account. \$\endgroup\$ Apr 28, 2019 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .