Multi-Resolution Game Development With GameBuilder Studio Check it out!
NEW Version 0.9.7 has arrived! New Blazing Fast Particle Engine & More Game Actions. Check it out
How can I Dynamically address an entity in a Change Property Action
  • Let's Say I have a group of entities with a naming convention such as: "Platform_1", "Platform_2", "Platform_3", etc...
    What would be the best practice for dynamically addressing an entity (and it's properties)? 
    Is it possible to use concatenation techniques in a reference?
  • 6 Comments sorted by
  • @DbD yes I cover this in the Hungry Hero tutorial when creating the obstacles. You can only do this in an Expression field. Use the "Entity" keyword and put the name of the entity in quotes between an open and close square bracket:

    Entity[ "Platform_" + Self.Properties.index ].Spatial.position.x

    The "Self.Properties.index" part can point to another property with an index or you can put the index value directly after the "+" sign. Then you just access the components on the entity just like normal.
  • @DbD keep in mind that you can only reference/point to properties of dynamic entities using the concatenate technique because the Change Property action only allows you to set properties using a Property Reference. This is by design. If you have to set a property on an entity and you don't know the entity you want to set this value on until run time of the game and you need to dynamically point to the entity, the only way to do this currently would be to store the entity object in a property somewhere and then point to the components on that property just like you would when referencing the "Self" keyword in an expression but using a property reference. 

    So you would store the dynamically named entity into a property using an expression like this:

    @Properties.CurrentEntity   =    Entity[ "Platform_" + Self.Properties.index ]

    then use a property reference to point to the components on that property

    @Properties.CurrentEntity.Spatial.position.x


    The important thing here is that you remember to clear that property "@Properties.CurrentEntity" later when you are done with it because you will create a memory leak in your game if you don't. Also don't point to that property unless you know that it has a value because it will throw an error at runtime.
  • Thanks for this answer,
    So my understanding of this is as follows:
    @Properties is a Data Container in which we've created a property named CurrentEntity.
    index is another property created in @Properties? (Could be replaced by .RandomRange?)

    So I've managed to apply this since typing that last bit. Thanks a lot.
    As far as clearing the CurrentEntity Property, i'm a little confused when it comes to clearing properties.
    If you could explain that bit, I'd really appreciate it.

  • So the other thing I've learned here is that a property I create in a data container can have it's value type changed at runtime? It can be created to store a string value but then become an object reference?
  • @DbD Correct. To clear a property just set it to an empty string or empty value using a Set Property action


  • Great, Thanks!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion