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

Change text in component

Discussion in 'Project Tiny' started by TheFordeD, Feb 5, 2019.

  1. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    I have TS code:
    Code (JavaScript):
    1. this.getComponent(world, gameInterface.Labels.Credits, [ut.Text.Text2DRenderer], function (Text) {
    2.                 Text.text = Data.credits;
    3.             });
    this method:
    Code (JavaScript):
    1. static getComponent(world: ut.World, entity: ut.Entity, comp: any, callback: (obj: any) => any): void {
    2.             world.usingComponentData(entity, comp, callback);
    3.         }
    Why text not a changed after method?
     
  2. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Tiny doesn't use GetComponent() they have getComponentData() method


    1) Get componentData from Entity
    2) Change Data
    3) Set Data onto Entity
     
  3. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Result:
    upload_2019-2-5_17-32-58.png

    Code:
    Code (JavaScript):
    1. static getComponent(world: ut.World, entity: ut.Entity, comp: any, callback: (obj: any) => any): void {
    2.             world.usingComponentData(entity, comp, callback);
    3.             var Component = world.getComponentData(entity, comp);
    4.             world.setComponentData(entity, Component);
    5.         }
     
  4. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Ignoring the callback part, and the static class. . . try this logic

    Code (JavaScript):
    1.    
    2.             let textRenderer:ut.Text.Text2DRenderer = this.world.getComponentData(entity, ut.Text.Text2DRenderer);
    3.             textRenderer.text = "I'm Loving it!";
    4.             this.world.setComponentData(entity, textRenderer);
     
    TheFordeD likes this.
  5. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Thanks. I do it another


    Code (JavaScript):
    1. static getComponent<T>(world: ut.World, entity: ut.Entity, comp: any, callback: (obj: T) => T): void {
    2.             let Component:T = world.getComponentData(entity, comp);
    3.             Component = callback(Component);
    4.             world.setComponentData(entity, Component);
    5.         }
    6.  
    7. ...
    8.  
    9.  
    10. this.getComponent<ut.Text.Text2DRenderer>(world, gameInterface.Labels.Credits, ut.Text.Text2DRenderer, function (Text) {
    11.                 Text.text = Data.credits.toString();
    12.                 return Text;
    13.             });
     
    vincismurf likes this.