Spread an ID value through multiple instances of the same node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

See title. I have several teleporters in my test stage and I would like them to each take the player to a different section of the game. However, at the moment they all take him to the same section. Is there a way I could spread an ID value across these nodes to help differentiate them from one another? Example:

func on_teleport_body_entered():
    if id == 0:
        (Send Player to a section)

    if id == 1:
        (Send Player to another section)
:bust_in_silhouette: Reply From: johnygames

The simplest way I can think of is to use the export var option.

  1. Go to your teleporter scene and create a new script, if you haven’t already.

  2. Add the following variable in the beginning of the script:

export (String) var ID_number
  1. A new field named ID_number will appear on the right side of the editor when you click on the scene. Change the ID to whatever you want.

  2. Repeat step 3 for all instances of your teleporter scene in your main scene.

Does this answer your question adequately? If it does, please mark this answer as best, so that others can find it useful too.

That would work if the teleporter objects were in the scene itself. I use a tilemap to load different objects (boss gates, teleporters, hazards that can’t be placed into a tilemap) into the scene based on what level the player is on when the stage loads. Maybe there’s a way to change that value at runtime after the teleporter objects have spawned?

9BitStrider | 2019-08-07 15:07

Can you place all the objects in a group or an array? Then you could iterate over the items of that group/array and assign an ID based on a separate array of IDs, effectively “stitching” the items to their corresponding IDs. I guess you could do that with a for loop.

johnygames | 2019-08-07 15:27