Enemy only instances once

Godot Version

4.3

Question

I’m probably making a really simple mistake, but I can’t find it. I have code that’s supposed to spawn in an enemy everything the function (spawnem) is called.

If I only call it once, it works exactly as intended. The snowgoon (a characterbody2d) spawns somewhere randomly in the world. Calling it twice, no matter how, will always fail. It’ll print “okey dokie!” twice, but there will be 0 snowgoons. (the snowgoonNo is set to 1 because I have an instance I put there myself and I was going to use it to control how many snowgoons are in the world at once)
I’m very new to coding and stuff, but I’ve looked up this issue in 700 different ways and have seen code that resembles mine and works. I have no idea what’s wrong.
If you need more details to figure this out, I am so totally open to sharing

Betting the snowgoons are spawning inside one another and causing physics problems, maybe flying away because they are constantly overlapping each other. You can check the “remote” tab in the scene tree while your game is running to see how th real scene tree develops as you test.


They don’t spawn somewhere randomly, only at spawn2’s global position. You probably have a warning along the lines of “Statment has no effect” on the line with array.shuffle, without parenthesis you are not using the function, just stating it. Add parenthesis, or use pick_random()

# with parenthesis the array is shuffled
array.shuffle()
# Using pick_random()
exist = [spawn1, spawn2].pick_random().global_position

Even with this there is a very high 50% chance they spawn in the same place again and cause collision overlap issues.


Make sure to paste scripts instead of screen shots

1 Like

Thank you a bunch! I’ll try this out as soon as I get the chance