godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.03k stars 21.18k forks source link

Error when trying to instantiate a scene when there s already and instance of the same scene #96390

Closed baileyhalfcat closed 2 months ago

baileyhalfcat commented 2 months ago

Tested versions

System information

Godot v4.3.stable (77dcf97d8) - Windows 10.0.19045 - Vulkan (Forward+) - dedicated Radeon RX 580 Series (Advanced Micro Devices, Inc.; 31.0.12027.9001) - AMD FX(tm)-4350 Quad-Core Processor (4 Threads)

Issue description

so imagine u have a room on the exits of that room is a node that spawns another room which has more nodes. for some reason it doesnt instantiate nodes in newly instantiated room returning a an error:

E 0:00:01:0348   _parse_ext_resource: res://room1.tscn:13 - Parse Error: [ext_resource] referenced non-existent resource at: res://node_3d2.tscn
  <C++ Source>   scene/resources/resource_format_text.cpp:159 @ _parse_ext_resource()
and a warning:
W 0:00:01:0380   node_3d.gd:18 @ try_to_spawn_room(): Node './Node3D' was modified from inside an instance, but it has vanished.
  <C++ Source>   scene/resources/packed_scene.cpp:254 @ instantiate()
  <Stack Trace>  node_3d.gd:18 @ try_to_spawn_room()
                 node_3d.gd:15 @ _ready()

no it is not a reccursion issue since its kept in check in code for it not to run infenetly

it creates a room but nodes fail to spawn i posted on this on official forum couldnt resolve scoured internet for answer coudnt resolve confronted a sowtfare enguineer she couldnt resolve it either

Steps to reproduce

extends Node3D

@export var rooms_array : Array = [preload("res://room1.tscn")]
@export var max_rooms_pre_randit : int
@export var rooms_min_random : float
@export var rooms_max_random : float

func _ready() -> void:
    if !global.max_rooms_decided:
        global.max_rooms = int(max_rooms_pre_randit * randf_range(rooms_min_random, rooms_max_random))
        global.max_rooms_decided = true
    print(global.max_rooms, global.rooms_spawned)
    if global.rooms_spawned < global.max_rooms:
        print(global.max_rooms, global.rooms_spawned)
        try_to_spawn_room()
        global.rooms_spawned += 1
func try_to_spawn_room():
    var instance = rooms_array.pick_random().instantiate()
    add_child(instance)
    print("hello world i spawned a thing")

add one of the nodes with this code into the main scene and another in the room scene set the preload to the room in question

Minimal reproduction project (MRP)

in a new project create a scene as main scene create a global script and add var max_rooms: int = 10 in there create another scene as spawner node, add the folowing script to the node: extends Node3D const ROOM_1 = preload("res://room1.tscn") @export var rooms_spawned: int func _ready() -> void: if global.max_rooms > rooms_spawned: global.max_rooms += 1 var instance = ROOM_1.instantiate() add_child(instance) create a scene called room1 and add a mesh to it and add a spawner node offset both from the centre in the main scene add the spawner node

AThousandShips commented 2 months ago

Please upload an MRP even if the steps are simple, it helps a lot to identify exactly what is wrong, and helps working out the steps, you might have missed something or something you didn't think of might be important

AThousandShips commented 2 months ago

From your description, though I can't quite understand your instruction which is why I think an MRP would help, it sounds like you have a circular reference where one scene loads another that loads the first one, that's not supported in this way with preload, but will test once you upload an MRP

baileyhalfcat commented 2 months ago

From your description, though I can't quite understand your instruction which is why I think an MRP would help, it sounds like you have a circular reference where one scene loads another that loads the first one, that's not supported in this way with preload, but will test once you upload an MRP

im actually a moron thanks a lot and sorry for wasting ur time x3

baileyhalfcat commented 2 months ago

turns out preload functions shouldnt be used that way x3

AThousandShips commented 2 months ago

If there's some area of the documentation you feel would help if updated to clarify things don't hesitate to open an issue over in the docs repo