0 votes

Hi everyone,

In my project I need room prefabs that have their own public properties like width, height ...
These properties are needed when the biome (node that contains the rooms) chooses which rooms go where based on their width, height, doors possible position ...

So the parent (biome) has a list of handmade scenes and he needs to select one of these based on their public properties without instantiate it.

This is the parent:
```
public readonly List RoomPrefabs = new List{
ResourceLoader.Load("res://World/Layers/Biomes/Ressources/Mossy/Rooms/Normal.tscn"),
};

public override void _Ready()
{
// Here is what I want to do
GD.Print(RoomPrefabs[0].Width);
}
```

And this is the general class of a Room:
```
[Export]
public int Width;

[Export]
public int Height
```

Godot version latest
in Engine by (21 points)
edited by

1 Answer

+1 vote
Best answer

You can't access variables of preloaded node, unless it is of resource class. Advised structure of code in your case would be to keep room properties in Resource or JSON and use them for getting info and for creating rooms on the go. You wouldn't need so many scenes in this case - just one class scene that would modify itself according to data in Resource or JSON.

by (5,132 points)
selected by

Thanks for your quick response, your comment answer one of my concern which is: How should I build random rooms with prerequisites, but I would also like to place handmade rooms with specifics nodes like bosses, structures ... How would you do ?

I am doing similar project right now and I made a custom scene to create and edit rooms - roomeditor. This scene consists of scripted tilemap. I place the tiles how I want end place other objects on them, and i choose rooms name in exported variable. When I run the roomeditor it saves tiles and objects as dictionaries in a file with a name I chose as roomname.

In my main project there is roomgenerator node that builds rooms using those written files, according to certain algortihm ( how many rooms, available rooms of type, boss rooms and so on). It starts from one starting room, iterates thru this rooms door passages and checks if other rooms ( randomly chosen ) would fit when connected by these doors. It uses information from those written files to simulate new layout of tiles in 4 different orientations ( east, west and so on ). If any of rooms tiles would overlap already existing tile - roomgenerator drops this room and iterates another random room. When room would finally fit without collision it is built into world, and its doors are iterated next, in the same manner, until maximum amount of rooms is reached.

Is there any other way ? I think it would take me so much time to implement. And it would be quite difficult to serialize all type of scenes.

It is always like this in programming. The more time You can sacrifice to set your workflow, the more time You save on tedious tasks.
I know You already took your time to create current code structure, it always feels sorry to build things anew. I encourage You to take my ideas an implement something simpler and using resources You already created. However some things are impossible to implement any other way - especially if You want to use editors UI and tilemap to create scenes, that will later be created dynamically in main tree.
Your easy options are :
1. Make simpler roomeditor scene, that will overwrite one file with one nested dictionary {roomname : { width : x, heigth : y}} and it will only count max tiles diagonally and horizontaly. Main key (roomname) can even be string path straight to your room scene.
2. More tediously, You can use one Resource class with the same dictionary and every time you make a room in editor count tiles by yourself and create new key/value in this dictionary.
3. Eventually look for other people's TOOL scripts on github and asset libraries, maybe someone had that problem before and made something to make life easier

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.