(Solved) Problems on Creating Object and Deleting Certain of Them

I am making a tower defence game, and I refered this example to code so that the enemies will be deleted when its hp variable is less than or equal to 0:

What I did:

But the problem is when the game runs, it will only spawn 1 TargetRoundButton. The spawning code:

So, how can I make the game keep spawning TargetRoundButton every 2 seconds, and delete the ones which their hp variable is less than or equal to 0?

edit: the code does not spawn any TargetRoundButton, it appears because I manually placed it on the scene

Hi, so just for understanding, the upper events are not yours, that’s just an example you found?

The second event should just work, if the hp is lower or equal to 0.

Your spawn event should also work as intended. What are you doing with the TargetRoundButton after creation? If you don’t move it anywhere the buttons will just spawn at the same spot and it may look as there is just one of them without disappearing. Maybe that’s obvious but it is hard to say what is going on without seeing your other events.

Yes, the first image is an example while the second one is my code

I did couple of things after creating the TargetRoundButton. I let it move towards an object (with pathfinding behavior) and when the TargetRoundButton goes near enough the tower placeholder with animation#1 (indicating a tower is placed on the tower placeholder), the tower placeholder will create bullet, starting from tower placeholder towards the TargetRoundButton, subtracting 2 hp from TargetRoundButton. Here’s the code related to everything mentioned above:

I really cannot find anything that would prevent your spawn event to work. I made something similar as a test and it works as intended.

I would suggest to test your spawn event in an empty scene just to figure out whether it works.

For your damage events it was the right thing to add the “Trigger once condition” and it should also work without any problems.

Hmmm…That’s strange… I tried the spawn event in another scene it works. I think I will try to copy everything in the new scene and try to run it again

Did you confirm this in the debugger?

After copying every single line of code and objects (with same name) into a new scene, I realise that the code does not spawn any TargetRoundButton. The one TargetROundButton I mentioned earlier was not spawned by the code but is originally in the scene (I placed it on the scene).

Did you find the problem? The spawn event as shown in your events should actually work. Gruk is right, using the debugger can confirm that.

Two other things you can check:

  • Do you have any objects in your scene that obstruct TargetRoundButton (e.g. a background image with a higher z-order)?
  • Is your End.X() and End.Y() located within the screen area so that the objects will be visible for you when they move to this position?

Just adding one more check to @Drona comment that you could make: look if the variable hp of TargetRoundButt object is greater than 0. But look at the object and not the instance that’s in the scene.

1 Like

I am not very sure how I can use the debugger to check my code, but when I start preview and debugger and there isn’t any error. Also I’ve checked:

  • There isn’t any background
  • The TargetRoundButton that I placed on the screen moves perfectly, so the problem shouldn’t be on the End

Sorry I am not very sure what do you mean, I am quite new to GDevelop, could you please explain a little bit more?

When you click ‘start preview and debugger’ you can see the preview and behind the debugger window. In the inspectors-window is a button ‘refresh’. After clicking it will show all the objects in your scene and also the instances of your spawned TargetRoundButton - if they are being spawned.

I hope Rasterisko is okay when I answer this question. In your events you delete TargetRoundButton when its hp-variable is 0. If you, by chance, spawn the instances of TargetRoundButton with 0 hp they will be immediately deleted, so you have to make sure that your object has an hp value over 0 as an object variable.

1 Like

I tried it, there is only 1 TargetRoundButton (again, it is the one I placed it in the scene)

I am not sure if I did this correctly, I change the hp variable of TargetRoundButton at the beginning of the scene, after that spawn 1 TargetRountButton every 2 seconds. I think this may be related to my problem because actually there is another problem which is about the second-last line of code:

The money keeps adding 50 about every 2 seconds, which is the “spawn” timer value.

Okay, you can set the object variable of your TargetRoundButton in the scene editor. You click on the three dots next to your object and choose ‘Edit object variables’. You have to make sure that the name of your variable is exatly the one you use in your events (case sensitive).

If you set the object variable at the beginning of the scene it applies this value only to the object that is in the scene and not to those that are created later.

I tried it, and it works! Thank you and sorry for making such a small mistake.

No need to be sorry. And all props to @Rasterisko. I didn’t think of the object variable.

1 Like