Respawn object with variable - problem (SOLVED)

I create 3 enemies (instances of the same object called “alien”).
Then I make a "For each object alien —> I assign a variable in the object called Random value Pos that defines its position on the screen.
The problem is that after one of them is destroyed (deleted in case of a collision with a shot) after a while I create a new = respawn.

But when I create it, it comes without the internal variable I need. If I run the For each object again, the alien objects that are still “alive” will switch places as well.

I’m breaking my head here and I’d like some suggestions.

First of all, don’t create object variables here using events. Go to the Object tab in Scene Editor and Press the three dots beside the Enemy object->Add Object Variable. Then add the variable there explicitly.

This way every instance of your object will have that variable with it.

That’s the problem, even if I set the variable in the scene editor I want to change the random variable for each object I create dynamically.

So that each enemy has a position (degree) on the screen.

When I change the variable for each of them, after all are created, it works very well. But if one dies and I need to create another, I can not change the variable of this new one.

So you want the enemy to spawn at random position right?
You don’t need a variable for this. You can use the action Common actions for all objectObjectCreate an object->Then specify the X and Y positions as Random variables. For example, look what i did down below.

It creates enemies at random X positions. No variables used here :wink:

But if you want to use a variable, try making a Global or Scene Variable. It’s not related to any object in particular. So the enemies being deleted will have no effect on this variable.

My case is a little more complicated. when I create my enemies each of them has to stay in one position, move at a different speed, in different directions … this is the least important … but the most important thing is that I have to make a random draw between 0 and 360. I give the result to a scene variable and then I need to assign that same result to my enemy angle and also to his position with respect to an X and Y point.

As I said before, I can do all these things, all at once, to an indeterminate number of enemies. My problem is being reproduced in one, when the other ones are already created.

What would serve me would be a kind of “create object” already with variable specifications, so I did not have to use a “for each”.

try creating the object and assing a value to the i
object variable like this
create object enemy at x,y
do =0 to random of enemy

I think I know how to solve this! I think what you’re lacking here is a condition to make sure the objects that already have a value in RandomPos won’t be affected by the event.

I think a way to do this would be to assign a value to another Object Variable at the same time you assign a value to RandomPos, which we will call “Alive”. This way, when an object is created at a random position, it will also be assigned a value of 1 in its “Alive” object variable.
Then, you make the condition of the “For each…” event check if the object has a value in its Alive variable. If it doesn’t, then it also hasn’t been assigned a position yet.

For each instance of NewObject:
If variable "Alive" of NewObject = 0
  do = Random(700) to variable "RandomPos" of NewObject
  do = 1 to variable "Alive" of NewObject

This way, the event will not affect objects that already have the 1 in the Alive variable. That being said, you should be able to create new instances of the same object and assign them different values in their RandomPos variable without re-assigning values to the other ones in the scene.
Also, I think an important part of this would be to not add the “Trigger Once” condition to the For Each event. That condition could stop it from triggering again when an object respawns without the variable. If you insist on adding it, though, then I hope you can re-trigger the event with another at a later time at least.
I hope this works!

Very good MayhemCats, problem solved.

At the end of how many it was just add a variable saying that the object was “alive” when applying the condition.

My code stayed like this to whoever interests:

Thank you all.