0 votes

I'm attempting to access a tilemap node in the scene tree from a C# class that is not attached to a node. I have a MapGenerator class and I want to pull a reference to the tilemap so I can populate it procedurally. I can't see a need to attach it to a node, even though it works fine that way, but I feel there should be a way to use it as a stand-alone class.

GetNode() fails to find the tilemap node.

I've tried...


... and every variation possible (except the correct one maybe?)

The c# class works fine when attached to a node in the scene or if I pass the node reference in a parameter.

I've searched and searched all of the internets for a solution but I thought I would try here before I make a coding commitment.

Godot version 4.0 Beta 14
in Engine by (22 points)
edited by

GetTree() will be null unless the node is in the tree (that is, there’s no SceneTree until added to one). Is there a reason why it isn’t in the tree if it needs to interact with other nodes? For example, a game may have a ‘Map’ parent node with Tilemap nodes as children, where the parent node populates the children as appropriate (e.g. the enemies layer or walls). You could perhaps broadcast (like an event) the reference but this seems like asking for trouble.

I'm developing a rather complex colony survival game and the original c++ concept code contains dozens of classes. The classes are mostly for intangibles such as atmosphere, resource data, etc. If I created a node for every script the scene tree would be bloated. I also find c-style code easier to read and maintain (my background) so just hammering out small, useful classes is kind of my thing.

I understand the lack of a scene tree if the script is not "instantiated" within it. I've been using Init() functions to pass node references and that's working ok. It's what I would have done in c++ so no big deal. GDscript will definitely spoil you. I started prototyping in Godot with the intention of porting to Unity, but as I'm sure most users have experienced, I'm now in an early alpha version with no intention of porting lol.

I consider this an answer to my question! Not sure how to turn your comment into an answer though.

Thanks for the help!!

1 Answer

0 votes
Best answer

Please see comment from spaceyjase for the answer...

by (22 points)
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 Frequently asked questions and 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 [email protected] with your username.