How do I compare instance variables of the same object type?

0 favourites
  • 6 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • This probably has a simple solution, but I've been banging my head against the wall for a week trying to figure it out alone.

    I have two instances of object type "Royalty": royal1 and royal2. They both have the instance variable of "married" with a boolean value and are created at the start of the layout.

    I want to create a condition that says do this if royal1.married == false AND royal2.married == false. In Construct 3, is it possible to compare the instance variables of instances of the same type?

    For context, the last engine I used was Unity. I know how to compare instance variables with OOP languages, but I'm finding it difficult to understand how to target specific instances in Construct's event sheet when the object instance isn't the last active instance.

    Any insight would be helpful.

  • Pick the first instance, save it to a temporary local variable, then pick the second instance and compare it then.

  • Construct's picking system is good for filtering instances but not great when you want to compare two different instances.

    plazatin's idea could look like this:

    var other=0
    
    pick sprite instance 1
    -- set other to sprite.variable
    
    pick sprite instance 2
    sprite: variable = other
    -- do something

    If you want to compare more than one value from another instance then you could store the iid instead:

    var other=0
    
    pick sprite instance 1
    -- set other to sprite.iid
    
    pick sprite instance 2
    sprite: variable = sprite(other).variable
    -- do something

    Another common approach is to add a family with just that type, and name it otherSprite. The only caveat is you'd need to add the variables to the family to make them accessable from both sprite and otherSprite.

    pick otherSprite instance 1
    pick sprite instance 2
    sprite: variable = otherSprite.variable
    -- do something
  • You can also reference instances by picking with IID in an expression:

    Example:

    if (Sprite(0).x > Sprite(1).x)

    -- do something

    Cleaner and shorter, but just keep in mind that it can get tricky since IIDs can change, unlike UIDs.

    reference:

    construct.net/en/make-games/manuals/construct-3/project-primitives/events/expressions

    You can add a zero-based object index to get expressions from different object instances. For example Sprite(0).X gets the first Sprite instance's X position, and Sprite(1).X gets the second instance's X position. For more information see index IDs (IIDs) in instances. You can also pass another expression for the index. Negative numbers start from the opposite end, so Sprite(-1).X gets the last Sprite's X position.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hi all, thank you so much for your assistance. With your help, I figured something out using "Pick with UID" and storing the value of an instance variable into a local variable. (The IID is a great idea, but I need an invariable reference for this project.)

    I hoped to get around using UIDs because I can see the code getting unwieldy as the project grows. However, I'll play around with arrays and dictionaries to see if they can help manage object references.

    Once again, thank you.

  • When I started out, I was actually using pick by UID too, but found that it was easier to just pick objects by a custom instance variable (e.g. name). You can pick by evaluate or comparison. For me, that beats remembering numbers.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)