set_pos doesn't work properly

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

Greetsing! I seem to have a code (see below) which doesn’t properly position an object. I load the function in the process(delta). Tried printing the position and everything seems to be ok. Any help would be greatly appreciated. Thanks!

ps I’m using Godot 2.1.3(old version)

func load_human():
var humans = human.instance()
var xpos 
var ypos  
for i in range(human_count):
	xpos = rand_range(1, 1020)
	ypos = rand_range(1, 760)
	humans.set_pos(Vector2(xpos, ypos))

If I understand this code correctly then you create a single instance of human and then add the same instance multiple times as child (of what, the scene root?). Adding the same child multiple times sounds strange.

So you set the position and name of this same instance multiple times. And it should be visible on the last position set.

I can just guess that you actually want to create multiple instances in the loop. In that case I would create the instance inside the loop.

You say also, that you call this function from _process(). That would mean that the number of added_childs will explode as you will add multiple instances on each frame (i.e. 60 frames per second).

As you didn’t tell it, I have no idea what you actually want to do but usually such a call is usefull on _ready() or at a certain (single) event.

wombatstampede | 2020-01-22 08:07

HI! You’re correct on that. I did transfer the human.instance() inside, and everything worked perfectly. Thanks for the reply wombat, much appreciated.

ddarkmoto | 2020-01-25 11:55

:bust_in_silhouette: Reply From: ddarkmoto

For anyone having the same issues, I just transferred the var humans = human.instance() inside the loop and everything worked.