Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scriptable Object replacement

Discussion in 'Entity Component System' started by Kandy_Man, Feb 22, 2019.

  1. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    67
    I like to use Scriptable Objects a lot in Unity, they're a great way to define things such as enemies, their stats and abilities, items, they work really well with the "old" way of doing Unity.

    I think it's safe to say Scriptable Objects aren't something we're gonna be able to use with ECS in the simple way that we currently do with MonoBehaviour. Seeing as ECS was described as component system 2.0 (I forget who said it, I wanna say Lucas?), are there any plans to have a Scriptable Object 2.0 that would be compatible with ECS? Is it even necessary, and if not, how would you go about predefining things like variations of enemies with ECS? I'm still trying to get my head around ECS so this might be a stupid question in which case please forgive my ignorance.
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    I think Scriptable Objects are still very usable in ECS in terms of tooling and wiring up data. Actually, it'll be just the same for Hybrid ECS. One idea that I had was to make systems into SOs, so that in editor you can wire up system dependencies, orderings, and which world they belong to, etc. To put the same System into multiple different worlds, you can just make more instances of the SO.

    If the upcoming ECS tooling features can accommodate all these without the use of SOs, I'm also fine with that.
     
  3. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    126
    A few ways I can think of is,

    1. Allowing a ScriptableObject create an entity in OnEnable() with the preset values in the ScriptableObject. This is similar to how [Shared]/ComponentDataProxy (formerly known as [Shared]/ComponentDataWrapper) works.

    2. If you have a MonoBehaviour which has a reference to that ScriptableObject, you can perform a data copy from the ScriptableObject into a newly created Entity with the specific data types.

    E.g. For both examples, if you have a scriptable object with like a (float) Damage field that you set, you can in: OnEnable()/Start(), call the Active World's EntityManager to construct a new entity having a DamageProxy and copy the data from the scriptable object to the entity.

    3. You can avoid using Scriptable Objects and put ComponentDataWrapper/Proxy onto a gameObject because those will have a GameObjectEntity with their associated Components, that you can fetch from the ComponentGroup. Although, I like using ScriptableObjects too for custom inspectors I can design.
     
  4. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    67
    Thanks guys, very interesting. I very much like the idea of a MonoBehaviour with a SO reference that just creates an entity. Thinking aloud, I can picture an enemySpawner MonoBehaviour having a list of all the different enemy types in the game, each of which are SO's, and then spawning entities with the appropriate components based on the data within the SO's. If they aren't looking at a SO 2.0 this might be the best way to go, at least for my use case, although I would prefer a way to go pure ECS and still have the "database" style way of working with SO's. When coming up with enemies in an RPG, SO's are hands down the best way to work I think.
     
  5. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Based on the Unity Tiny preview and ECS "sub scenes", there will most likely be a way to save a pre-authored group of entities as an asset. You can then load that and reference as needed?
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    You can actually kind of already do something similar to that. There are built in tools to convert an entire scene to entity representation and load it at run time.

    I probably wouldn't do it on a per entity basis, but it's great for groups of entities.
     
    psuong likes this.
  7. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    In the Streaming and Serialization ECS talk, there's mentioning of a ECS-specific file format (.entities) that contains serialized archetype and chunk data. I don't know whether or not this is available right now. @tertle, were you referring to the .entities files or something else?

     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    psuong, Singtaa and learc83 like this.