Why does my Godot object only spawn once?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on a Godot script issue where an object is intended to spawn repeatedly at the top of the screen but only spawns once. The user provided a script that utilizes the Godot engine's Node2D and Area2D classes. The problem arises because the object is destroyed using the queue_free() method when its position exceeds 200 on the y-axis, preventing further spawns. The solution involves adjusting the spawning logic to ensure that new instances are created after the object is removed.

PREREQUISITES
  • Familiarity with Godot Engine 3.x scripting
  • Understanding of Node2D and Area2D classes
  • Knowledge of the queue_free() method in Godot
  • Basic understanding of game object lifecycle in game development
NEXT STEPS
  • Review Godot's documentation on Node2D and Area2D classes
  • Learn about object instantiation and destruction in Godot
  • Explore the _process() and _physics_process() functions in Godot
  • Investigate Godot's signal system for managing object events
USEFUL FOR

Game developers using Godot Engine, particularly those working on object spawning and lifecycle management in 2D games.

Darkmisc
Messages
222
Reaction score
31
TL;DR
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.
 
  • Like
Likes   Reactions: Darkmisc
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K