How to move spawned object?

Godot 4

Hi! I try to move spawned object across the screen (right now I just try to push them in Y axis), but it doesn’t work. I coded this inside a timer node. Right now it just spawns objects without moving them. I have no idea what else could I try, I will apreciate all sugestions!

extends Timer

var cow_01 = preload("res://prefabs/Shooter/shooter_cow_01.tscn")
var coin_01 = preload("res://prefabs/Shooter/shooter_coin_01.tscn")
var coin_05 = preload("res://prefabs/Shooter/shooter_coin_05.tscn")

func _process(delta):
	pass

func _on_timeout():
	randomize()
	var colectables = [cow_01,cow_01,cow_01,cow_01,cow_01,coin_01,coin_01,coin_05]
	var kinds = colectables[randi()% colectables.size()]
	var spawner = kinds.instantiate()
	spawner.position = Vector2(randi_range(10,990), randi_range(10,590))
	#spawner.position = Vector2(randi_range(180,1200), randi_range(1750,1200))
	add_child(spawner)
	_move(spawner)
	#wait_time = randi_range(0,1)

func _move(spawner):
	while spawner.position.y >= -100:
		spawner.position += Vector2(0,1)

Can I ask you to print out the spawner.position.y inside of your _move() method?

What’s the timer’s wait_time?

yes, it prints position y, changes it by adding 1 with every line

1 second

a while loop will happen instantly, it does not wait for anything. You should attach a script to the spawned object to move it with a _process function, this function operates once every frame.

Chances are you should see a infinite loop warning, since the y position is always greater than -100

1 Like

which means it moves by 1 pixel per second. This is very very slow.
Did you try adjusting the value to be spawner.position += Vector2(0,20) just to see if you can see something noticable?
I would also open up the remote tab and inspect the spawner.position as the timeouts happen. You will see the value increase ever so slightly.

1 Like

oh okey, but how can I do it? I don’t understand how can I put this function inside _process function when content of it comes from _on_timeout function. I don’t know how to use veriable with spawned child inside separate function. Because my logic is that that var spawner = kinds.instantiate() needs to be inside _on_timeout to work, is that correct?

I can, but the spawned objects are not showing at all when I try to move them. When I applied your sugestion nothing happend.

While your game is running, please open up your remote tab and see if the instantiated spawners are inside the sceneTree.
grafik
There should be one created each time the _on_timeout() method is executed.

yes, it prints position y, changes it by adding 1 with every line

If this is true, then your spawner must exist inside your sceneTree and the position is updated correctly. But the spawner is not shown.
We need to go into the Remote tab and inspect the spawner, it can be a shooter_cow_01, shooter_coin_01 or shooter_coin_05.
While the game is running, can you set the position of any spawner to be (500, 500) ? Is it visible?
Or did you somehow change the modulate to be invisible?

1 Like

You’re correct that most of the code should be in _on_timeout, spawning the object should be there. The movement part needs to be a part of a new script, attached to the spawned object. Attach a script like this to all the spawnable objects, you could even do this through code if they don’t already have scripts.

extends Node2D

var speed: float = 100

func _process(delta: float) -> void:
     self.position.y +=  speed * delta
1 Like

When I don’t use _move function the objects are spawning as children of timer node, but when I do use _move function then only one object spawns in remote tab. I didn’t make them invisible, but I am not a pro in this so I might have messed something up along the way :sweat_smile: When I changed position to 500 and 500 the object is still not visible.

IT WORKEEEDD! Thank you so much! It makes so much more sense now! :smiley: