Using rng for positions leads to many children added instead of one.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CaveRaider

I’m trying to make a powerup generator for a little game I’m making. The problem is when I try to make the powerup appear in a random location. I set the x and y positions to a random number using ranf_range, but when the function is triggered it creates 8 powerups instead of one.’
if poweruptimer == 0:
powerupObj = ExtraLife
powerup = powerupObj.instance()
powerup.position.x = rand_range(200.0, 1200.0)
powerup.position.y = -20
get_node(“/root/GameScene/Entities”).add_child(powerup)
poweruptimer = 900
else:
poweruptimer -= 1`

Probably not an issue of the rand_range()

Try hardcoding powerup.position.x = 200, then printing to the console something like: print("Powerup added: "+str(powerup)) at the end of the if statement.

My guess is the if poweruptimer == 0 is being evaluted 8 times instead of the 1 time you were expecting. If that’s the case, you’d want to look and see what might be causing that to be called the “extra” times.

eod | 2020-01-25 08:26