Search Unity

what's in an entity, anyway?

Discussion in 'Project Tiny' started by tbriley, Dec 20, 2018.

  1. tbriley

    tbriley

    Joined:
    Sep 10, 2013
    Posts:
    115
    edit: this is referring to project tiny, not unity's main ecs

    normally with ECS i like to work with systems that can process in a reactive way (which is sort of what behaviours are trying to do), over multiple groups (which you can do with 'regular' systems).

    for example, with an audio system, a pattern that feels nice is to cache the audio sources and also react to events / commands to play the cached sources. at the moment i've accomplished this by splitting the functionality between a behaviour and a system (though ideally this would all be accomplished in a reactive system, and not by exposing a bunch of methods in the behaviour).

    now to my actual problem: the trouble i am having is it's not clear to me exactly what an entity or entity view are. my AudioBehaviour caches AudioSource components:

    Code (CSharp):
    1.  
    2. namespace FinerGames {
    3.   export class AudioBehaviourFilter extends ut.EntityFilter {
    4.     entity: ut.Entity;
    5.     audioSource: ut.Audio.AudioSource;
    6.     name: FinerGames.Name;
    7.   }
    8.  
    9.   export class AudioBehaviour extends ut.ComponentBehaviour {
    10.  
    11.     data: AudioBehaviourFilter;
    12.  
    13.     static audioSources = {};
    14.  
    15.     OnEntityEnable(): void {
    16.       console.log(this.data.entity)
    17.       let entityName = this.world.getEntityName(this.data.entity);
    18.       let entity = this.world.getEntityByName(entityName);
    19.       console.log(entity)
    20.       AudioBehaviour.audioSources[this.data.name.Value] = entity;
    21.     }
    22.  
    23.     OnEntityDisable(): void {
    24.       delete AudioBehaviour.audioSources[this.data.name.Value];
    25.     }
    26.   }
    27. }
    28.  
    ... so that my AudioSystem can respond to AudioEvents and play the cached source:

    Code (CSharp):
    1.  
    2. namespace FinerGames {
    3.  
    4.   //TODO -> this doesn't seem to be working
    5.   // @ut.executeAfter(FinerGames.AudioBehaviour)
    6.   export class AudioSystem extends ut.ComponentSystem {
    7.  
    8.     OnUpdate(): void {
    9.       this.world.forEach([ut.Entity, FinerGames.EventData, FinerGames.AudioEvent],
    10.         (entity, eventData, audioEvent) => {
    11.           let audioEntity = AudioBehaviour.audioSources[audioEvent.Name];
    12.           if (!this.world.hasComponent(audioEntity, ut.Audio.AudioSourceStart))
    13.             this.world.addComponent(audioEntity, ut.Audio.AudioSourceStart);
    14.         });
    15.     }
    16.   }
    17. }
    18.  
    it's not clear to me why in the behaviour code above logging the entities returns a different result. the first looks like it logs a pointer. the second the entity data, which is what this.world.hasComponent() and this.world.addComponent() seem to expect in order to work.

    can i get some clarification on what is going on here and whether there is a more straightforward way of working with the entity rather than calling through this.world.getEntityName() followed by this.world.getEntityByName()? thanks!
     
    Last edited: Dec 20, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    9,039
    I think you should add to the title, that you are referring to audio. As entity itself is simply an index with its version. Two integers.

    Edit: Oh just realized that is project tiny, so not sure how relevant my post will be.
     
  3. tbriley

    tbriley

    Joined:
    Sep 10, 2013
    Posts:
    115
    i'm just using the audio systems as an example. perhaps i shouldn't have led with that, but i though it necessary to explain so that the intent of the code is clear.

    my main question is: why does the filter not seem to give me what i need to pass to the world, and is there an easier way to achieve that than by finding the entities name in the world and then getting the entity data by world name?

    edit: just saw your edit, i'll update the post so it's more clear this is specific to tiny.
     
unityunity