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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
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