Search Unity

EntityReference '==' operator not working as expected

Discussion in 'Project Tiny' started by Berzee, Dec 11, 2018.

  1. Berzee

    Berzee

    Joined:
    Sep 3, 2013
    Posts:
    11
    This might just be me making silly Typescript mistakes, but here's my problem.

    I created a component with four EntityReference fields.
    upload_2018-12-11_9-6-5.png

    I then added this component and a Sprite2DSequencePlayer to the same entity and assigned some sprite sequences.
    upload_2018-12-11_9-15-55.png


    As you can see, SequencePlayer defaults to using the "Anim_D" sequence referred to by Animations.D. However, the following System script does not find them to be equal even after setting the sequence again directly.
    Code (JavaScript):
    1. OnUpdate():void {
    2.             this.world.forEach([game.Movement, ut.Core2D.Sprite2DSequencePlayer, game.Animations],
    3.                 (movement, sequencePlayer, animations) => {
    4.                  
    5.                     console.log(this.world.getEntityName(sequencePlayer.sequence));
    6.                     console.log("sequencePlayer.sequence == animations.D? " + (sequencePlayer.sequence == animations.D));
    7.                     sequencePlayer.sequence = animations.D;
    8.                     console.log("How about now? " + (sequencePlayer.sequence == animations.D));
    The output of this code is:

    Anim_D
    sequencePlayer.sequence == animations.D? false
    How about now? false


    Any idea why these "==" checks are returning false? Is there a different way to test if two EntityReferences are equal, or pointing to the same entity? I've been able to get around it for now by comparing the results of this.world.getEntityName() for each, but that's obviously not ideal.

    Thanks pals. =)
     

    Attached Files:

  2. etienne_unity

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    88
    Hi @Berzee

    EntityReference fields are represented as
    ut.Entity
    objects in TypeScript. The equality operator on JavaScript objects will only return true if the two operands share the same memory address, which is not the case in your example. See this page for more info.

    To compare two
    ut.Entity
    instances, you can do this:

    Code (JavaScript):
    1. static Equals(a: ut.Entity, b: ut.Entity) : boolean {
    2.   return (a.index == b.index && a.version == b.version);
    3. }
     
  3. Berzee

    Berzee

    Joined:
    Sep 3, 2013
    Posts:
    11
    Ahh, I thought it would be something like that. Your suggested equals function works fine, as does just comparing the index values in my case. Thanks!

    I am idly curious why this line in my code above:
    sequencePlayer.sequence = animations.D;

    doesn't actually set the variables to the same memory address, when something similar in Javascript gives a different result (https://jsfiddle.net/Berzee/89udgket/) ... but I'm quite satisfied with the index solution. =)
     
  4. v_vuk

    v_vuk

    Unity Technologies

    Joined:
    Jul 11, 2017
    Posts:
    35
    Comparing just the index values isn't safe -- the indices are reused as entities are created and destroyed, and the version distinguishes that. But.. it does highlight that we didn't actually expose an equals method on Entity in TypeScript. Oops. Will fix.
     
unityunity