Typesafe instance, adressing subnodes without the knowledge of their path

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

Firstly, hi everyone :). I’m mainly working in Unigine, but I’m trying to toy with Godot and this is the first issue that I was unable to solve via the docs and google-fu :(. I am using c#, but I welcome the solution in all supported languages :).

So I basically just want to add “my object” to the scene, connect to its parameters, maybe flip character, maybe register to “area” volume, maybe whatever else, but I want to do it with object/interface type safety, basically. So that when I rename “area”, or change its tree structure, it won’t break everywhere…

See here

I am familiar with the UE4 and UNIGINE and both could easily work with “spawned” actors/nodes, but I am unable to find the right way in Godot. Am I doing it inherently wrong?

The testing idea now was to have a tile for an endless runner. I need to spawn these “forever”, so I need to work with three tiles and every time the player crosses from the second tile to the third tile, I need to drop the first tile, create the “fourth” tile and move everything a tile back (so that I don’t run out of coordinates, etc). Pretty standard, easy stuff.

My instincts are to have some tile handler that spawns and despawns the tiles and communicates only with what he needs.

:bust_in_silhouette: Reply From: GonziHere

That question was a moment of weakness of me, because the solution could not be simpler. I don’t like querying with strings and I was so focused on that, that I did NOT see the obvious choice: query with string once, in the constructor of the Chunk scene and store the result for the usage in the world…

public class Chunk : Node {
    // this will stay the same
    public Area myArea;
    public override void _Ready() {
        // even if I'll have to change the path here
        myArea = GetNodeOrNull<Area>("CollisionShape/Area");
    }
}

and then simply do this:

public class World : Spatial {
public override void _Ready() {
	var chunk = ResourceLoader
            .Load<PackedScene>(chunkScenePath)?.InstanceAs<Chunk>();
    var thatChunkArea = chunk.myArea;

So yeah, really stupid question, shoutout to kleonc for helping me on reddit.