Hello everyone,

I am currently in the process of trying to respawn a collectible item after it has been picked up and added to an inventory. Here is the code:

The plan is to have the item respawn 4 seconds after it is picked up. I just don't know what I should put after creating the timer. I know I should use .instance() to create the new instance and use .position and Vector2 to set the position. But as far as I know, .instance only works on scenes and my Moon_item which I am trying to add is a node, specifically an Area2D node. Is there any simple way to create a new instance of a node or convert a node into a scene?

  • xyz replied to this.

    xyz I suppose my inexperience made me worried that it would mess things up. I have successfully created a scene for the item so I can instantiate the object. But now I've run into a new problem: the game instantly crashes when I try to run it. After commenting out several different lines, I have found the cause of this to be a variable that preloads the scene.

    extends Area2D
    
    @export var slot_data : SlotData
    @onready var sprite_2d : Sprite2D = $Sprite2D
    
    #var scene_to_instance = preload("res://word_pickup.tscn") <----- this is the problem
    var player
    var word_x
    var word_y
    
    func _ready():
    	word_x = self.global_position.x
    	word_y = self.global_position.y
    	sprite_2d.texture = slot_data.item_data.texture	
    
    #func instance_object():
    	#var object = scene_to_instance.instantiate()
    	#object.global_position = Vector2(word_x, word_y)
    	#call_deferred("add_child",object)
    
    
    func _on_area_entered(area):
    	player = area.get_parent()
    	player.inventory_data.pick_up_slot_data(slot_data)    
    	queue_free()
    	await get_tree().create_timer(4).timeout
    	#instance_object()

    There is no message nor error in the debugger that tells me anything; the game just crashes instantly. This code is all contained in a pickup script that is attached to the root node of the collectible item scene. I'd be grateful for any advice; I really have no idea what's going on.

    • xyz replied to this.

      Derek9132 If I got it correctly, you're trying to instantiate the scene from within that scene itself? This can certainly cause a crash. A scene should normally be instantiated "from outside". Otherwise you'll end up in endless recursive instantiation resulting in stack overflow.

        xyz I am trying to instantiate the item scene in the main 2D scene of my game. I figured out the error; the var scene_to_instance was placed too high up in the code. Once I brought it down to line 15, in between the ready and instance_object functions, the game opens and runs. Now the problem I am facing is the fact that the item doesn't actually respawn after I collect it. This time, the debugger is giving me the following errors:

        E 0:00:00:0619 load: res://word_pickup.tscn:3 - Parse Error: [ext_resource] referenced nonexistent resource at: res://word_pickup.gd
        <C++ Source> scene/resources/resource_format_text.cpp:490 @ load()
        E 0:00:00:0619 _load: Failed loading resource: res://word_pickup.tscn. Make sure resources have been imported by opening the project in the editor at least once.
        <C++ Error> Condition "found" is true. Returning: Ref<Resource>()
        <C++ Source> core/io/resource_loader.cpp:222 @ _load()
        E 0:00:00:0620 set_path: Another resource is loaded from path 'res://word_pickup.tscn' (possible cyclic resource inclusion).
        <C++ Error> Method/function failed.
        <C++ Source> core/io/resource.cpp:75 @ set_path()

        My guess is that I should be instantiating the scene from a different script. Currently I am attempting to instantiate the scene from a script attached to the scene itself. The script shown in the picture is attached to the word_pickup node on the left sidebar, which is a child of the main 2D root acting as the scene. Maybe I should try instantiating the object from the script attached to the main 2D root?

          Derek9132 Maybe I should try instantiating the object from the script attached to the main 2D root?

          You shouldn't instantiate a scene from that scene itself (unless you know exactly what you're doing). So yeah, do it from the top node in the main scene. I'd recommend going through a basic tutorial on scene instancing to get better understanding of how it works.

          Derek9132 I figured out the error; the var scene_to_instance was placed too high up in the code. Once I brought it down to line 15, in between the ready and instance_object functions, the game opens and runs.

          That shouldn't have made any difference.

          • xyz replied to this.

            DaveTheCoder I think OP also removed instantiate() call from _ready(). Now that the scene doesn't instantiate itself into oblivion it gets a chance to display actual errors 🙂