latest steam version : 4.3 stable
this is related to my last post about instantiation not working correctly
i figured out that for some reason it cant create an instance of a scene if an instance of the same scene is already present does anyone know any solutions for that?
Could you show your code so far? It is certainly possible. The names get pretty mangled, maybe that is confusing?
extends Node3D
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")
this is the whole code not considering the global which i think ull understand whats in global anyway x3
Maybe you can change the line of
if global.rooms_spawned < global.max_rooms:
to
while global.rooms_spawned < global.max_rooms:
its spawns a million instances in 1 place which i dont need as im trying to create makeshift dungeon generator
Sounds like a step in the right direction no? If 1 million is too many, you need to reduce your max_rooms_pre_randit
or other min/max random variables. Otherwise you have 1 million instances to be placed however you like.
no its not i need 1 node to only spawn max of 1 room but in that room i have more nodes which will spawn more tooms the problem is that it doesnt spawn nodes when i spawn the room
What node does that script belong to? The rooms?
Ah so this script is attached to every room, and the children are to execute it as well. It’s recursive.
You have to keep in mind _ready
is called as soon as the node is added to the scene (via add_child
) so your code will be adding children then counting up global.rooms_spawned
but it never gets to the count since it’s adding children until that number increases.
One solution could be to defer adding children, or re-ordering your lines.
if global.rooms_spawned <= global.max_rooms: # 2: also <= to fix off-by-one
print(global.max_rooms, global.rooms_spawned)
global.rooms_spawned += 1 # 1: count up before adding children
try_to_spawn_room()
# or replace add_child(instance) with:
add_child.call_deferred(instance)
code is only attached to the nodes
it kinda worked but kinda didnt coz now it says that it spawns stuff in the debug thingy but it doesnt do anything in reality
wait gimme a sec i might have messed up a bit
It is probably overlapping all your new rooms. Try moving the Node3D away from origin, this should form a straight line based on the offset. You can check the “Remote” tab in the Scene editor’s panel to see what the running scene looks like under the hood.
i did offset it but it doesnt seem to work like it doesnt spawn the nodes as u can see but spawns the room
it doesnt work it just renders the meshes but the code doesnt run (the interesting thing is if i make nodes local it starts working but i cant do that as changing the settings later would be super tidious)
I think you will have a much easier time with this if you had a different node to handle the creation of your rooms instead of letting the rooms generate recursively. Your scene tree could be as follows:
World
–Player
–RoomManager
Give RoomManager a script that handles spawning rooms using a loop. You won’t need a global variable anymore since it will be managed by RoomManager.
You can use Marker3D nodes in your room scenes that tell it where to connect the new room to the previous room. Or if your rooms are perfectly square and all the same size, then you can keep track of which direction you’ve been spawning them with integer coordinates and multiply by the (width, length) of your room to get the position of where the next room should go.
Good luck! Let us know if you need more help
Hard to tell what is going on, did you check out the “Remote” tab? What are the errors in your debugger?
it might be a good solution but its still good to know whats wrong with this so i wont have to struggle with it later
Seems like some relevant errors. Not sure what resource it’s talking about but I’d recommend digging around the resources, betting it’s something wrong with the mesh, no idea what from here though.
I just noticed that you’ve used @export var but did not assign a default value
@export var max_rooms_pre_randit : int
@export var rooms_min_random : float
@export var rooms_max_random : float
I’m not sure, but this could mean that these values are all not populated for the next generation of spawn nodes in your recursion. Can you try to replace those lines of code with this?
@export var max_rooms_pre_randit : int = 29
@export var rooms_min_random : float = 0.9
@export var rooms_max_random : float = 1.3
Does anything change? I also don’t see in the code anywhere that you’re changing the position of the newly spawned scenes. Just to see that they are in fact being spawned can you give them a random global position at the end of their ready function, say something like this?
global_position = Vector3(randf_range(-20,20),0,randf_range(-20,20))
i checked and in the room scene it says the same stuff just in case i change the code and nothing changed:(
Might be worth zipping up your project and uploading it. I’m not sure how to start or describe what I would look for when given that error message.