Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

OnAwake alike function ?

Discussion in 'Project Tiny' started by Zoelovezle, Mar 27, 2019.

  1. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    I have been wondering how can i create Awake or Start alike functions since the OnEntityEnable from BehaviourComponent is executed after the OnUpdate method from the component system?
     
  2. Pakor

    Pakor

    Joined:
    Feb 2, 2017
    Posts:
    32
    I don't think there is one at this point in time. I could be wrong, so I hope one of the devs answers this. For now you can recreate the Awake function by using a static bool in the system where you need it. The static bool never reloads, so it will never run twice. The start function can be recreated by setting a bool on a component and then setting it to true once you ran what you wanted to. This way when you re-enter the 'scene' it will run again because the component loads again.
     
    Zoelovezle likes this.
  3. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    For entities you can also create a component used for initialization, and have a system execute code for all entities with that component, then remove the component. This will cause the entity to be picked up only once, and will make sure that no unneeded code is still executed for checking a boolean-value.

    Example:
    Code (CSharp):
    1. export class InitializationSystem extends ut.ComponentSystem {
    2.     OnUpdate (): void {
    3.         this.world.forEach([ut.Entity, game.InitializationComponent], (entity, initializeComponent) => {
    4.             //Execute initialization code for the entity.
    5.             this.world.removeComponent(entity, game.InitializationComponent);
    6.         });
    7.     }
    8. }
     
    Maras, sniffle63, Pakor and 1 other person like this.
  4. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. namespace game {
    2.     export class DataProcessorBehaviour extends ComponentBehaviour {
    3.  
    4.         // Somehow, Tiny will read this and fill the variable with the properly contents
    5.         data: DataProcessorFilter
    6.  
    7.         OnEntityEnable() {
    8.             // Initialization here
    9.         }
    10.     }
    11.  
    12.     export class DataProcessorFilter extends EntityFilter {
    13.         entity: ut.Entity
    14.         mydataComponent: MyDataComponent      
    15.     }
    16. }
     
    sniffle63 likes this.