[Bug/Work Around] Actions after object creation not functioning as expected

Good Evening,

I am running into a weird problem that I hope is just a 1:30 am brain fart.

Normally when I spawn a unit, any actions underneath it would only apply to that object unless I use a For Each Object Loop.
This can be confirmed with the test spawn event below.



Only the spawned unit has its scale and health adjusted.


However when I am spawning the objects in my timer, the actions are affecting all units currently on the map.
The events used are exactly like the test spawn event and there are no For Each Object Loop within the whole event/group.



All units on screen have their scale and health adjusted.

Am I missing something or is this a bug? Any help/thoughts would be appricated.

I had a similar problem.

I didn’t want to make a new object for each and every item in the game, so I created an object “items” and use different animations for each one.

At the beginning of the scene, I create the objects where they’re needed.
Then I need to set the animation for each individual item so they aren’t all the same. So what I did is when I’m creating the items, each has an object variable to identify one item from the other with a unique number in the object variable.

For your action next to “control key is pressed”, you could use
Set enemy_ant.Variable(ID) == Variable(spawn_ID)

then add to your conditions for spawning the ant: ID == spawn_ID
then spawn the ant
then increment spawn_ID

there might be a better way to do it, but I think that will achieve the desired effect.

[edit]
that may not work, because you’re setting the enemy variable before it’s spawned, there’s nothing to set it to. So, instead,

create the ant
set ant object variable
condition: Variable(spawnID) == ant.Variable(ID)
use your effects

Possibly, this condition is interfering with the instance filtering GDevelop makes to apply the actions:
image

Hi fitkoh,

Thanks for the reply and ideas. The problem with setting a UUID variable is in that event block all units are being effected which would lead to all units getting the same UUID after each object created. (Much like how all the units are having their health object variable changed)

Spawn 1 - UUID #1
Spawn 2- UUID #1 (also including spawn 1)
Spawn 3 - UUID #1 (also including spawn 1 & 2)

Hi Gruk,

I had a slight suspicion it could have been that as it’s only thing that’s really different over the test event (but it was lat at night so, brain farts). I decided to move the spawning event block into function to see if it would work correctly and it does.

Would it be worth trying to replicate this in an empty project as a bug report, or do you see this as intended behavior?

This looks like unintended behavior indeed. Could you make a minimal project showcasing the issue to let us investigate what is happening?

In the meantime (as workaround) you could use another initialization method that might also be cleaner. By setting all that up when creating the object you make more work for yourself. Let’s imagine you want other ways to spawn, you would have to copy paste that initialization code. It makes lots of redundant repetition that can slow down exports, make your event sheet unecessary long, and make refactoring (changing one of those initialization events) very tedious.

Solution 1:
Use a custom behavior. You can move your logic for the enemy to an “Enemy Behavior”. You can then use the lifetime behavior function onCreate to initialize the object when it is created.

Solution 2:
Add in your event sheet an event with condition object variable “initialized” of enemy is 0, and then all the initialization actions + an action to set the object variable “initialized” of the enemy to 1. As variables are 0 by default all of this will fire if the object is new.

Solution 1 is more complex but better as

  1. It doesn’t run every frame but only when the object is created
  2. It triggers as soon as the object is created, it doesn’t wait for the correct event to be reached
  3. The solution 2 can only initialize 1 object per frame so some might stay uninitiated for a few frames if there are too many enemies being spawned at once.

Hey arthuro555,

Thank you for the in-depth reply. I have gone ahead and created a bug report with a basic project replicating the issue.

I have found a work around earlier (using a function to spawn the objects) but i’ll look more into what you have suggested for better implementation.

1 Like