[Solved] Set instance variables to external scene objects?

Here’s the code, I think it’s pretty self explanatory, but:

What I’m trying to do is, for each player, a bunch of objects from the external scene “HUD” will be created, and I want to set the ID variable of these objects to correspond to each player’s HUD

However, what happens is:


Both of them show only the 2nd player stats

Any help would be greatly appreciated, thanks!

I might use linking instead of an id but that’s another conversation and I think it’s important that you understand why this isn’t working as expected and 1 way to fix it.

Without any conditions all of the instances from the external layout are being picked. So, it sets the ID the first time to 1 but then sets them all to 2 for the next player instance in the for each object. (The previous intances and new ones)

To fix it, I would move the setup to it’s own event group at the top of the event sheet. I’d put the for each player as a subevent of an at the beginning and remove the at the beginning that’s inside the loop. Just like trigger once they don’t always work well inside loops. I would assign an id variable to the player objects instead of using the instance count and set the IDs to 1, 2… I would use the player object variable for the external object ids. I would also create a group with all of the external layout objects. Let’s call it Group for my example.

The events would be

At the beginning
   For each player
       Add external layout
           id object variable in Group = 0   |  id of object variable in Group equals Player.Variable(id)

That should set the IDs of each group to the player ID. This is one way to do it. Nothing is wrong with it. I just find linking is more organized and readable.

1 Like

You meant this, right?


And if so, thank you! It worked!

And also, I think I’ll switch to using linking. Thank you for the suggestion too!

1 Like

Oh and, I’m sorry for asking again but, how would you do it in linking?
I tried doing this but now neither of them work:

Yes, I prefer to keep all the initialization and at the beginning events at the top. It helps me focus on the events that run on every frame. That’s what helps me.

1 Like

It would depend on which objects you need to modify later. You could link all of them or only the ones you need to change later. The fewer the better. I would use the same Boolean setup to filter them. I’ve seen people use the logic that you’re attempting but inside a for each object, you can only reference one player instance at a time. Since I believe you can only link 2 objects at a time, you could link them individually or using a for each object.

For specific objects, you could use:

For all of the objects you could use:

1 Like