How to properly instance RigidBody2D Scenes at regular intervals?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kingbard


I am new to Godot so I am sure I am making a simple, stupid mistake, any help is much appreciated.

I am trying to make a node that regularly spawns platforms off screen that will move until they are queue_freed when they make it to the other side of the screen. I am having difficulty when trying to call the .instance method.

I have tried a few different ways to call the floor node variable into the spawning script to no avail. Please help! :slight_smile:

I am receiving the following error when trying to instance new platforms:

Invalid call. Nonexistant function ‘insance’ in base ‘RigidBody2D’

Godot version: 3.2.1.stable.official

Local Tree:

  • FloorSpawn (Node2D)
    ---- Timer
    ---- Floor (RigidBody2D)

Script (attached to node root):

extends Node2D

onready var new_floor = $Floor

func _on_Timer_timeout():
	var new_floori = new_floor.instance()

extends RigidBody2D

export var speed = -100

func _physics_process(delta):
	move_local_y(delta * speed)

func _on_DieArea_area_entered(area):
:bust_in_silhouette: Reply From: kidscancode

Your new_floor variable is a reference to an existing node “Floor”. Nodes have no such method as instance().

instance() is a method of PackedScene - it takes a saved scene and creates all of its nodes/scripts/etc.

If you want to make a copy of an existing node, you can use duplicate(). Or you can save the floor object as a .tscn and then instance() it as many times as you need.

Aight that makes sense, thank you!!

kingbard | 2020-07-18 00:50