Same Object deletion upon collision not functioning

Hello! I have this random spawning logic of an environmental sprite object (a tree Stump). Credits to Silver-Streak for the spawning logic, iirc.

The map is supposed to be infinite so I have it so they spawn randomly off-screen around the player with a limit of 10 at a time. As the player moves, the Stumps delete themselves so more can be created but it also should check if Stumps overlap to delete them.

The object deleting above 2500 pixel distance appears to work well but the condition for Stump collision and deletion isn’t. I’m finding Stumps overlapping as well. Sometimes I get 3 or 4 overlaps. They have collision boxes, the Player also has collision checks with the stumps and that works well. I’m not sure what’s wrong here.

Edit: There’s a Start/reset timer at beginning of scene not shown in the image. Not relevant to the problem but probably worth mentioning.

can you provide a small project with this, i would like to help. had this one or two times with other engines. one solution was:
in a first logic loop you just flag the stump to be deleted. (say you set a variable of the object to “delete=1”
in a 2nd logic loop you just really delete objects if they are flagged.
this way some more complex comparison of object with same object can be managed (as when you delete an object, the loop compare itself is influenced)

Working with the collision of 2 objects of the same type is a pain. Instead, to prevent creating an object on top of another one, I’d suggest:

  1. duplicating the Stump object and rename the duplicate StumpChecker.
  2. events along the lines of:
1 Like

@ perlingg, not sure how to do the small project thingy.

@ MrMen, isn’t that order of events going to fail to loop? I’m not sure if I’m interpreting the events correctly…
So at the beginning of the scene the variable is true so the while event triggers, sets the variable to false then the first StumpChecker spawns. No collision is detected because it’s the first environmental thing to spawn so the collision condition doesn’t trigger, then the Stump and Trunk objects spawn on the Stump Checker but the While event already happened and now the variable is false so it can’t trigger the While event a 2nd time.

Ok, created it in my space. This works with 2 scene variables and a dummy spawner icon. To not mix up the objects in comparison.
1.spawncoin is a dummy_icon used to try and see if the real placed coins are overlapping.
2. spawn_tries is a counter that limits the maximum tries, if you have too many in too little space it wont ever find a solution.
3 is_colliding is a helper var (scene var) but can be made as a local var too.

Nope :smiley:

There’s no “Beginning of scene” condition.


The variable is a local variable. It only exists while the event (and it’s subevents) are active. Once out of scope, it’s removed. The next time the event is actioned, the variable is again instantiated with a default value of “True”.

1 Like

Its not failing at all. The condition is, every time you press space it tries to place new 5 coins. The tutorial file platformer was used (with the square jumping and the golden coins). Its quite save to use, but you must stick to the eventflow like shown. Otherwise you might run in “unlimited retry” situation and freeze the game. As the spawn position is randomly picked.

Thanks, m8! It seems to be working great!

I know there’s no beginning of the scene event. I meant when I preview the scene, thus the scene begins.

That last part is critical information i didn’t know. So every time the timer is > 1 seconds the variable resets to true, did i get this right?

You can consider it that way, if it makes it easier to grasp.

In reality, it depends how GDevelop implements local variables. Worst case the local variable is instantiated every game frame. Best case is it only gets instantiated when the timer is > 1 second.

However, the important part is when the events are run, the variable has been instantiated with a value of true

If i removed that condition then would it be every frame? Whatever condition i put there is what instatiates the variable as true?
I’m asking so i can understand it more clearly and its potential usefulnness.
What i’m failing to understand is twofold:

  • you state that “when events are run”. What events? Given that all events manipulating the local variable are dependent on said variable being true and contained within itself, and given that Gdevelop reads events from top to bottom, it would stand to reason (my limited reasoning) that if an action within it turns it “false” and failing the “true” part, it would disable the local variable event permanently.

  • following the 1st point, if the variable “resets” itself to true every 1 second anyway, why do i need the action in the event to tell it to become true again?

It’s not the condition that instantiated the variable. I believe the variable is instantiated with a value of True. Then if the conditions are not met, the variable is no longer in scope.

However, if the conditions are met, then the actions and the subevents are processed.


The first event and it’s subevents.


Remember stillLooking is an object variable. It only exists while the event it’s declared in and it’s subevents are being processed. Once the event block is no longer processed, the local variable ceases to exist.

I’ll try to explain how it works with this coloured in image, and I’m using the example when the timer is > 1 second:

When the purple event is processed, it starts off with the variable stillLooking being created and set to true. This variable is now in scope and GDevelop knows about it.

Next to be processed is the while event. This controls the subevents - whole orange section and is only actioned when stillLooking is true. The first action is to change stillLooking to false. Then a StumpChecker is placed at a random position.

The last event of the orange section checks whether StumpChecker is on an existing Stump. If it is, stillLooking is set to true. Then While event is processed again as before.

If, however, StumpChecker is not colliding with a Stump object, then stillLooking remains false. When the While event is processed again, stillLooking is false, and so the control (or prcessing) drops out of the orange section, and begins with the green section.

The orange section keeps placing a StumpChecker until such time that it doesn’t collide with an existing stump object. stilLookning ensures that process repeats until the StumpChecker object is placed in a free spot.

Once the last event (the green one) has finished processing, GDevelop drops out of the purple event’s scope and processes the next event (that’s off the bottom of the screen snip). At that point the variable stillLooking is out of scope, and it no longer exists for GDevelop. It is neither true nor false because it is no longer there. GDevelop does not know anything about it.

When GDevelop gets round to the purple event once the timer is > 1 second, the whole process starts up again and stillLooking is instantiated again with a value of true.

I hoe this explains things.