Search Unity

Logic with scriptable objects: data accesibility

Discussion in 'Scripting' started by Tsequier, Feb 3, 2019.

  1. Tsequier

    Tsequier

    Joined:
    Apr 20, 2016
    Posts:
    23
    Hello everyone ! After watching 2 videos (
    and
    ) and reading a lot on the subject, I decided to try making a project using scriptable object architecture. Everything is going quite smoothly, but I am still inexperienced in this matter, so I might doing some things the wrong way, that's why I needed a bit of help from people who have a lot more of knowledge of scriptable object and what are the Do and Don't.

    Basically my question is : I have a player object with different parts (head, body, legs) and I have several systems that need to access data of the parts' gameobject. How do I do that using scriptable objects ?

    The solution that came to my mind is having a scriptable object with 3 Gameobject field, and the player populate these fields at the start of the game so every object having a reference of this scriptable object can access the parts of the player. Does this make sens? Is there a better way that I am missing ?

    Thanks a lot !
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Tsequier

    I haven't used SOs that much, but think it like this;

    Instead of Prefabs or GameObjects, you now have asset file, that you cannot drag to scene itself, but you can drag it to fields of your scripts that are attached to GameObjects, or you can drag it to some fields in assets, that only exist in assets folder.

    So first you have to have at least a reference to SO in scene. Let's say you were making some RPG/adventure game. Then you have a chest script, which would give items to player. Drop your chest contents SO into field of chest script in GameObject (it has to accept the type of the SO you defined).

    Then you can have that SO asset itself have fields, that can be also accepting some other SO class asset. Prepare some loot, add SO items you define (Item : ScriptableObject, Food : Item...), in a list or fields of your chest contents SO.

    Then when your game is running, when you access your chest script, you can access the chest contents SO, and then it's fields, containing SO items data.

    You cannot save the state in SO, it does look like that in Editor, but not in build, so if you manipulate SO values runtime in editor, the values will change, but not so much in build / when you reboot the game.

    Also, you must define the SO class, then create asset out of it, using CreateAssetMenu attribute... if you want assets that you can drag and drop in editor.

    It seems to necessary to also save each SO class into it's own file, otherwise editor won't like it AFAIK.
     
  3. Tsequier

    Tsequier

    Joined:
    Apr 20, 2016
    Posts:
    23
    Thanks for the answer @eses ! It answered some of my concerns regarding SO !
    In your example the communication goes like : The script is using data from a SO that its assigned to it.

    Do you thinks its correct to also have a communication between SO and script that goes like :

    I have a SO called Body that have a reference to 3 Gameobjects (head, body, legs)
    I have a monobehavior script Player with a reference to Body

    At the start of the game Player give the 3 gameobjects to Body (Body.head = playerHead ... )

    SO after that, if I have another script that need the player head, it can just reference the SO Body and fetch Body.head.

    Is it something correct to do with Scriptable Objects ?