Godot Version
Godot Engine 4.7.2
Question
I’ll preface by saying I’m very new to Godot and this is my first game that isn’t following a tutorial. I’m essentially trying to make a FlappyBird clone.
I’m trying to have the obstacles spawn in at a fixed interval using the pipe_shaft scene set up with a kill_zone similar to how Brackeys used it for enemies in his video ‘How to make a video game - Godot beginner Tutorial’
This is what I’m using to spawn the obstacles in the main scene:
extends Node2D
const SHAFT_SCENE = preload("res://scenes/pipe_shaft.tscn")
func _ready() -> void:
#
# Adding pipe shafts
#
spawn_pipes()
# Fills the game 2D space with pipe shaft obstacles
func spawn_pipes():
var y = 120 # Initial pipe y coord
var shaftBottom = Vector2(168, y) # Initial pipe position
var shaftInstance # SHAFT_SCENE instantiate var
# Spawn a pipe every 160 pixels
for i in range(3):
shaftInstance = SHAFT_SCENE.instantiate() # Instantiate the pipe_shaft
# scene
shaftInstance.position = shaftBottom # Assign position to shaftInstance
add_child(shaftInstance) # Spawn pipe
y = y + 160 # Increment y coord 160 pixels
shaftBottom = Vector2(168, y) # Assign new position
# Debugging help
print(i)
print(shaftInstance)
print(shaftInstance.position)
The first pipe spawn in as expected, though following pipes are not present. Console output shows the loop is incrementing position as it should, but shaftInstance gets replaced with a generic name, leading me to believe that adding the scene.
0
PipeShaft:<Area2D#38973474360>
(168.0, 120.0)
1
@Area2D@2:<Area2D#39040583228>
(168.0, 280.0)
2
@Area2D@3:<Area2D#39107692096>
(168.0, 440.0)
This supports my assumption that all the children need to be uniquely named, but other examples I’ve found of this online (ie. Adding multiple of the same child to a scene) make it seem like that shouldn’t matter in this case. My attempts to dynamically rename shaftInstance didn’t seem to help either.
Any other tips or methods for what I’m trying to do are also welcome and appreciated.