Refereincing to methods or variables of sub-scenes works when instantiated, but not after I add to tree and call them differently

Godot Version


Greetings to everyone.
I can’t make up my mind on how to make this work.

I have a main scene. It is a city that I generate procedurally, putting roads and buildings on a grid.
Roads and buildings are preloaded as packed scenes.
Roads are just sprites.
But buildings are a somewhat complex scenes having at their core a StaticBody2D

Actually I have extended the class using:
class_name building
extends StaticBody2D

but… it’s not clear to me what this means to Godot, because… please continue reading and you’ll understand, hopefully.

There are various stuff inside the building class, because buildings themselves are procedurally generated: their height, windows, color, other stuff… all procedural.


Suppose I want to instantiate a building in a certain position AND set its parameters or call the method construct(), which I coded in the “gd” code of the buildings (and essentially create the building with the parameters given).

If I do something like this:

var piece_of_building: building = building.instantiate()
piece_of_building.global_position = Vector2(x * 320, y * 320 + (160-75))
piece_of_building.grid_coordinate_x = x
piece_of_building.grid_coordinate_y = y

Well, everything works.
And I can also get or set the values of various variables like x, y, with a syntax like:
var a = piece_of_building.height
piece_of_building.height = randi_range(3, 6)

But now, say I have added a lot of buildings to my map, repeatedly doing stuff like above.
Now I want to do stuff in the buildings, like changing the height or calling the method construct or asking the value of x or other stuff.

I try doing something like this:

var buildings = get_tree().get_nodes_in_group(“building”)

… and this already does not work nice, because if then I print those “buildilgs” Godot tells me that they are StaticBody2D objects: not “building” objects.
And I don’t like this. But it gets worse:

for b in buildings:

… and then, nothing works.

It does not work to write b.height to set, or get, the height or b.

It does not work to write b,construct() or other methods.

Godot does not find ANYTHING!!!

After a search on the web, I figured out that I can GET the value of variables by writing:
but I can’t find a way to SET it, or to call methods. For Godot, they are nonexistent.
Casting the type of b (“b as building”) does not help at all.

In short: stuff that worked before, for the variable piece_of_building created as an instantiated building, does not work anymore when I later try to reference to the same objects that I added.

Why? But more importantly, what can I do now?
I need to do stuff on the buildings AFTER I add them to the map because I want to give clusters of buildings the same parameters (height, color), and I don’t know where a cluster is if I don’t have all the buildings,

YES; I could solve the problem differently: first I create an array of “0” and “1” for the position of the buildings, i search for clusters in the array and only after that I instantiate everything.

It would work. Eventually I might consider doing this. But it pisses me a lot that what would see, to me, very natural, i.e. keeping to call methods and use variables of scenes as I did before, stops working after I get those scenes in a different way from the tree. They are the same objects, for God’s sake, Godot what do you want me to do now? :frowning: what is the correct syntax?! I don’t find it in the documentation!!

Thank you in advance for your help :slight_smile: