How can I access a Node from the SceneTree from an Object I created

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

Good day!
I’m learning Godot right now and I have the following Problem:
I have a Node with a script attached that holds a dict. with Int Values.
On Another Node I have a script attached with a static function that creates an Object from another Script which is not attached to any Node.
From that created Object I want to access the Node with the dict. and change Values.
get_node() doesn’t Work because the Object from which I access isn’t in the Scene Tree
Is it possible to access the dict. in some Way by that object?
Searched for quite some Time now and did not find any conclusive Solution, so I would be very grateful if somebody could Help me.

Hard to say what might be the best solution based on the limited info provided, but it might make sense to create / access the Dictionary from a Singleton instead of a specific object. Does that seem feasible?

If you did that, you could access the Dictionary from anywhere else in your game…

jgodfrey | 2023-01-16 23:16

Oh that should work, didn’t know about Singleton yet.
Thanks a lot^^

Lenrow | 2023-01-16 23:19

:bust_in_silhouette: Reply From: jgodfrey

Based on the above, here’s a basic outline:

  • Create a new script (say, to hold “global” data - including your Dict.
  • Create the dict as appropriate in the script: var myDict = {...}
  • Make the script an Autoload via Project | Project Settings | AutoLoad (tab)
  • Add your script to the list, and make sure it’s Enabled
  • Now, from anywhere in your game, you can access the Dict via: Globals.myDict

use Engine.get_main_loop() to get the scene tree from static functions or from non node scripts