Access property of an instanceless preloaded scene

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

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<PackedScene> RoomPrefabs = new List<PackedScene>{
        ResourceLoader.Load<PackedScene>("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
:bust_in_silhouette: Reply From: Inces

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.

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 ?

Zennyth | 2021-10-25 09:23

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.

Inces | 2021-10-25 11:20

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.

Zennyth | 2021-10-25 15:38

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

Inces | 2021-10-25 19:42