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
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to assign logic to a game object?

Discussion in 'Project Tiny' started by TheFordeD, Jan 22, 2019.

  1. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Hi people again, a can't understand how i can assign script logic to gameObject of the scene?
     
  2. ullatussimo

    ullatussimo

    Unity Technologies

    Joined:
    Jun 16, 2015
    Posts:
    108
    Hello @TheFordeD,

    Short answer: As the Tiny runtime is of Entity Component System architecture, you can't directly assign script logic to a Game Object of the scene.
    Consider creating a dummy component (for example, PlayerTag) and using that one to single out the object and use a system for the logic.

    Longer answer: With Tiny, the entities are basically just data containers instead of actors (Game Objects).
    This effectually means that you can not directly assign script logic to an entity.

    Instead, Tiny is built around the idea of Systems that apply the same set of operations to a multitude of entities that fit a certain criteria.
    With Game Objects and Scripts the logic goes something like each Game Object has a set of instructions (the scripts) and then does what the instructions say, right?
    With Entities and Systems the logic is that a System has the set of instruction and it applies it to each Entity according to the data that the Entity has.

    Keeping this in mind, you could apply logic to all entities that have a 'game.PlayerTag' component for example like this:

    this.world.forEach([ut.Entity, game.PlayerTag],(entity, player) => {
    // Logs the entity name to the browser console
    console.log(this.world.getEntityName(entity));
    });

    If you instead wanted to apply logic to each entity in the scene that have a Sprite2DRenderer and a position, you could do something like this:

    this.world.forEach([ut.Entity, ut.Core2D.Sprite2DRenderer, ut.Core2D.TransformLocalPosition], (entity, spriterenderer, transformLocalPosition) => {
    // Logs the entity name and the position to the browser console
    console.log(this.world.getEntityName(entity) + " is at " + JSON.stringify(transformLocalPosition.position) );
    });

    So, you can't directly assign script logic to an entity, instead you should create a system that makes the entity do what you want!

    Hope this helps.

    Kind regards,
    Simo
     
    TheFordeD likes this.
  3. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Thank you so much, now I understand the principle of building the application logic in Tiny
     
    ullatussimo likes this.