Search Unity

what's in an entity, anyway?

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

  1. tbriley


    Sep 10, 2013
    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):
    2. namespace FinerGames {
    3.   export class AudioBehaviourFilter extends ut.EntityFilter {
    4.     entity: ut.Entity;
    5.     audioSource: ut.Audio.AudioSource;
    6.     name: FinerGames.Name;
    7.   }
    9.   export class AudioBehaviour extends ut.ComponentBehaviour {
    11.     data: AudioBehaviourFilter;
    13.     static audioSources = {};
    15.     OnEntityEnable(): void {
    16.       console.log(
    17.       let entityName =;
    18.       let entity =;
    19.       console.log(entity)
    20.       AudioBehaviour.audioSources[] = entity;
    21.     }
    23.     OnEntityDisable(): void {
    24.       delete AudioBehaviour.audioSources[];
    25.     }
    26.   }
    27. }
    ... so that my AudioSystem can respond to AudioEvents and play the cached source:

    Code (CSharp):
    2. namespace FinerGames {
    4.   //TODO -> this doesn't seem to be working
    5.   // @ut.executeAfter(FinerGames.AudioBehaviour)
    6.   export class AudioSystem extends ut.ComponentSystem {
    8.     OnUpdate(): void {
    9.[ut.Entity, FinerGames.EventData, FinerGames.AudioEvent],
    10.         (entity, eventData, audioEvent) => {
    11.           let audioEntity = AudioBehaviour.audioSources[audioEvent.Name];
    12.           if (!, ut.Audio.AudioSourceStart))
    13.   , ut.Audio.AudioSourceStart);
    14.         });
    15.     }
    16.   }
    17. }
    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 and 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 followed by thanks!
    Last edited: Dec 20, 2018
  2. Antypodish


    Apr 29, 2014
    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


    Sep 10, 2013
    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.