1

I've been trying to learn more about scriptable objects because I've heard they're quite useful, however, I'm still a bit confused about how to work with them since it seems to conflict with the try to decouple your code. From my understanding, you want to try and limit the times you reference scripts since this causes things to become dependent, but with scriptable objects, it seems like you're supposed to reference them.

Here's my scriptable object

public class PlayerInfo : ScriptableObject
{
    public float maxHealth;
    public float health;
    public float shield;

    public BaseClass playerClass;
    public List<BaseAbility> abilities;

    public Controls controls;
}

I originally just had a health script that handled all of it, but now it just references the scriptable object and adjusts the values in it

public class HealthController : MonoBehaviour
{   
    public PlayerData playerData;

    void Start()
    {
        playerData = GetComponent<Player>().playerData;
        ClassSelectButton.OnClassSelected += SetMaxHealth;
    }

    public void SetHealth(float newHealth)
    {
        playerData.health = newHealth;
    }

    public void SetMaxHealth(BaseClass newClass)
    {
        playerData.maxHealth = newClass.health;
        playerData.health = playerData.maxHealth;
    }

    public void Damage(float damageTaken)
    {
        if (playerData.shield > 0)
            playerData.shield -= damageTaken;
        else
            playerData.health -= damageTaken;

        if (playerData.health <= 0)
            Kill();
    }
}

So is this actually how you'd want to use scriptable objects or did I miss something? lol

KiynL
  • 4,097
  • 2
  • 16
  • 34
Konjointed
  • 51
  • 3
  • Does this help you? https://stackoverflow.com/questions/67669140/use-of-scriptableobject-unity?rq=1 –  May 24 '22 at 01:33
  • In my experience , It's a serialize global data. maybe not eazy to decouple , But it just like a global "config" that is a good solution. because it can link your asset resources. – TimChang May 24 '22 at 06:03
  • In your case , It don't really need ScriptableObject. ScriptableObject asset it's for global setting. if you just want to save some playerdata . just use prefab enough. – TimChang May 24 '22 at 06:08

1 Answers1

1

Although the scribtable object is a very useful tool for storing information as an asset, it is not very useful for what you want to do. Because player information can be stored in an independent class or in the player itself, or even struct. Remember the code you wrote above. Returns the same amount of Player Data in the field and is ineffective. However, to do the above, you must have a scriptable object asset. I will show you two ways below.

playerData = GetComponent<Player>().playerData;

Create Asset Menu

The first way is to create an asset through an attribute. Use the create asset menu as below and create the asset. Then refer it to the field defined in the player class.

[CreateAssetMenu(fileName = "New Player Info", menuName = "MyMenu/ Player Info")]

public class PlayerInfo : ScriptableObject
{
   // something in here...
}

enter image description here


Create Scriptable Object Instance

This will help you build an asset in the code. It is useful when you want each instance to have a separate asset. To use it, do the following.

PlayerInfo _info = ScriptableObject.CreateInstance<PlayerInfo>();

After running one of the above methods, you can access the scriptable object.

_info.health += 20;

Debug.Log(_info.health);
KiynL
  • 4,097
  • 2
  • 16
  • 34