Why does my Godot object only spawn once?

  • Thread starter Darkmisc
  • Start date
In summary, the conversation was about spawning an object at the top of the screen and having it move towards the bottom before disappearing and respawning again. The code provided was not working as expected, with the object only spawning once and not repeating the cycle. The expert suggested that the mistake might be in the "queue_free()" line, which could be destroying the object at y=200 and preventing the if statement from being activated. The object is a green square and the code is attached to it to control its movement.
  • #1
Darkmisc
220
31
TL;DR Summary
I'm just starting to learn GDScript. I'd like an object to spawn near the top of the viewport, move towards the bottom, disappear and then spawn again. I can only get it to spawn once.
Hi everyone

I'd like to spawn an object at the top of the screen, have the object move towards the bottom, disappear and then spawn again at the top of the screen (to repeat the cycle). I've used the below code.

When I run it, the object spawns once, moves towards the bottom, disappears and that's it. It doesn't spawn again.

Does anyone know what I've done wrong?Thanks

gameplay:
extends Node2D
var plsquare = preload("res://MainScenes/square/square.tscn")func _ready():
    var square = plsquare.instance()
    get_tree().current_scene.add_child(square)
    square.position = Vector2(-200, rand_range(0, 100))

    if square.position.y>199:
        square = plsquare.instance()
        get_tree().current_scene.add_child(square)
        square.position = Vector2(-200, rand_range(0, 100))
square:
extends Area2Dexport var speed: float = 100
var vel:=Vector2(0,0)

func _process(delta):
    pass
    func _physics_process(delta):
    vel.y=speed
    
    position +=vel * delta
    if position.y>200:
        queue_free()
 
Technology news on Phys.org
  • #2
I use Unity, not Godot, so I don't know if I can help you. But I'll give it a shot. First, what is actually moving here? I see the position+= vel* delta line, but what is actually moving? I don't see any object that the code acts on. Is it like unity where certain things are implicit to the object the script is attached to? Also, should queue_free() be called? That sounds like it might be destroying an object.
 
  • Like
Likes Darkmisc
  • #3
The object is just a green square. Eventually, it might become an enemy in a space shooter or Tetris block.

I'm familiar with Unity, but it sounds like it might work like Godot. The script is attached to the object and tells it what to do.

I think you might have found the mistake in my code. I've destroyed the object at y=200, so the if statement might not get activated.
 

FAQ: Why does my Godot object only spawn once?

Why does my Godot object only spawn once?

One common reason for this issue is that the code responsible for spawning the object is only executed once, such as in the _ready() function. Make sure that the spawning logic is triggered repeatedly, such as in the _process() function or in response to a specific event.

Could the object be getting destroyed after it spawns?

It is possible that the object is being destroyed after it spawns, which would explain why it only appears once. Check your code for any functions or scripts that may be destroying the object shortly after it is spawned.

Is the object's spawn location changing after the initial spawn?

If the object's spawn location is being modified after the initial spawn, it may appear as though the object is only spawning once. Double-check your code to ensure that the spawn location remains consistent if you want the object to spawn multiple times.

Are there any conditions preventing the object from spawning again?

Check for any conditional statements in your code that may be preventing the object from spawning again. Make sure that all necessary conditions are met for the object to spawn multiple times, and adjust your logic accordingly.

Could there be a bug in the spawning logic?

If none of the above reasons seem to be causing the issue, there may be a bug in the spawning logic itself. Review your code carefully and consider debugging techniques to identify and fix any potential issues with the object spawning process.

Back
Top