Need help with instantiating

Godot Version

4.x

Question

I am making a flappy bird clone and I am having trouble instantiating the pipes. The pipes don’t seem to spawn.

Spawner code:

extends Node2D

@onready var timer := $Timer
var pipe = preload("res://scenes/pipe.tscn")

func _ready() -> void:
	timer.wait_time = 1

func spawn_pipe():
	var pipe_instance = pipe.instance()
	add_child(pipe_instance)

Pipe code:

extends CharacterBody2D

const movespeed = -25

func _ready() -> void:
	print("pipe spawned")
	position.x = 140
	position.y = randi_range(40, -40)

func _process(_delta: float) -> void:
	if(position.x <= -140):
		queue_free()
		print("pipe deleted")
	
	velocity.x = movespeed * Global.difficulty
	move_and_slide()

Do the print statements trigger, i.e. “pip spawned”?

If so maybe your pipes have been created off-set from the 2D origin at 0,0 Could you share a screenshot of the pipe scene?

The print statements don’t trigger. I have tested the pipe and it works fine as long as it is not instantiated.

Here is a screenshot:

Cool, then the timer’s timeout signal must not be connected to your spawn_pipe function, and/or your timer is not set to autostart, you can do this through the editor or in code

func _ready() -> void:
	timer.wait_time = 1
	timer.start()
	timer.timeout.connect(spawn_pipe)

Turns out that the timer did not autostart, it’s always the simple things that elude me! Thanks!

Nevermind, I started getting an error.
image

as of 4.0 it is now .instantiate()

Thanks, it all works great now!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.