Adding and extracting objects to an array or something like that

I have such a problem, I have several creatures in the game that are both enemies and game characters at the same time.

According to my idea, the player can take control of one of them and use the unique characteristics of any of them.

But the problem is that I can’t change the game character without duplicating the control code for each one.

Is there a way to create an array or variable that will contain an object that is currently under the player’s control?

Simulate pressing “Left” key for Player
(Which can be a bird, a mouse, or a horse(for example), depending on its current state)

Simulate pressing “Left” key for Bird
Simulate pressing “Left” key for Mouse
Simulate pressing “Left” key for Horse
(And so it is for absolutely every control)

I tried to use groups of objects, but they seem to be impossible to change during the game. I also tried to use different variables and arrays, but I didn’t find one that can remember an object. I’ve also tried using animation variations, but it looks like a dubious solution.

You can use a group, you just need a condition to pick just one object. Any condition would be fine. Or you could delete the other characters and only have 1 object in the scene at a time. Just use the group name for any event.

Give each character an object variable like “name” or whatever works for you. Then use a variable like “CurrentPlayer”. Then put your events for the group as a sub-event.

Set CurrentPlayer to “Bird”

Variable Name of GroupName = CurrentPlayer

I guess alternatively, you can delete the other objects if they’re not needed on screen. Then just keep 1 object in the group. You would lose any object properties and variables. Just use create object by name from your group.

Each character would need identical behaviors, variables and animations name. You might still need a name variable should each character have any special abilities or interactions.

It would be easier if you could use the built-in objectName property but there’s currently no condition for it and the compare 2 strings condition only works as a trigger without picking the matching objects. Although, you could use the compare 2 strings condition if there was always just 1 object in the scene at a time.

1 Like

Oh my God, it works. Thank you very much, I would never have figured it out myself in my life.

1 Like