Why does my Godot object only spawn once?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
AI Thread Summary
The discussion centers around a coding issue in a game development context, specifically using Godot. The user is attempting to create an object that spawns at the top of the screen, moves downward, disappears, and then respawns at the top. The provided code successfully spawns the object and moves it downward but fails to respawn it after it disappears. Key points of confusion include the movement mechanics and the use of the `queue_free()` function, which removes the object from the scene. It is suggested that the issue lies in the condition for respawning the object, as the current logic does not allow for the object to be recreated after it is destroyed at the y=200 position. The conversation highlights the importance of understanding object lifecycle management in game development.
Darkmisc
Messages
222
Reaction score
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

[CODE title="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))



[/CODE][CODE title="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()
[/CODE]
 
Technology news on Phys.org
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.
 
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top