0
\$\begingroup\$

I'm developing a skill system for a client-server game, all available player's skills server send to it, then client load it and put it to a spellbook. So I stuck in loading stage, how for performance and/or memory will be better:

  • Save all spells in a bunch of prefabs and then just instantiate them
  • Create JSON/binary/etc file, parse them and create a gameobject at runtime

From the side of editing, both methods are equal.

\$\endgroup\$
1
\$\begingroup\$

Well, for starters:

  • A prefab is a copy of an instance, therefore, it is cheaper than a GameObject created at runtime.
  • Setting aside that in your case both methods represent the same for you in editing. In general it is better to have a prefab asset that can be recycled over and over again on scenes, and update everyone when you have to modify it.

For loading stage, you could have those spells as persistent data, and just load them once, say on the splash screen. For that, you would need a data structure, such as an Object Pool, where you can recycle your spells everytime you want to. Example:

public astract class Spell : MonoBehaviour
{
    void OnEnable()
    {
        Cast();
    }

    void Awake()
    {
        DontDestroyOnLoad(gameObject);  // With this, you keep it persistent and it gets on a scene called DontDestroyOnLoad.
    }

    public abstract void Cast();
}

public class Fire : Spell
{
    public override void Cast()
    {
        Debug.Log("Casting Fire...");
    }
}

public class Ice : Spell
{
    public override void Cast()
    {
        Debug.Log("Casting Ice...");
    }
}

So, instead of destroying spells at runtime, just deactivate their GameObjects, so you don't have memory leaks during object's instantiation and destruction.

If you are interested to know how an Object Pool would me made for this, let me know so I update my answer, or check Sebastian Lague's Video about it, just make it also persistent for all scenes.

Hope it helps.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.