#Reference variable from other scripts
1 messages · Page 1 of 1 (latest)
Since the user interface is a node which you are supposedly instancing into your scene tree... you cannot reference it by a class_name. Typing the class name references the script and not the actual object/instance.
It works with autoloads because autoloaded scripts become instanced nodes at runtime.
You can have scripts that don't need to be attached to created objects in order to work. But those scripts must be static, they cannot store any dynamic variables or any data that changes, only constants. A new object must be created using the script in order for the data to be manipulated.
In this case you should look into get_node()
You can probably use get_tree().current_scene.get_node("YourPath").UpgradeLink.show()
Understanding the path to use inside of get_node() can be hard to grasp at first. I suggest looking up as much info as you can on Node Paths. They are relative which mean they start at the node you're currently on.
Any node in the tree can access any other node with the right path. There are tools to help get around long paths. Like scene unique names. And Find_child()
I was just looking at the code as I noticed there are variables in the data script that I am changing from other scripts too. Using Game.ref.data.<Variable)
Using the same idea I can get the UpgradeLink variable to not give an error, I'm just battling with how to trigger this now...
Is the issue that the variable is attached to a UI element?
OK so cool looks like you stored a reference to UserInterface instanced object in the Game autoload
That should work fine without get node
You said you're having issues triggering that unlock checker function?
Yeah, when I use this code in the actual UserInterface script it works fine. But here it isn't triggering.
I think the connect is coming from a signal that is emitted every time playerControl var is updated...
So that script is not a node and therefore has no access to ready function
Sorry, Some of the logic of this game is built on a tutorial which has helped a lot in understanding some of how everything is working but I'm still assuming some things should 'just work' becasue it's working in another script xD
In some other nodes ready function, connect the signal
Or make unlock system an autoload
Maybe it is already?
(In which case it should not have a class name)
I'm not really sure what or how an autoload is done?
I tried to make a node to attach the UnlockSystem to but I get this error
Sorry I think I misunderstood.
Is that the whole UnlockSystem script? Does it extend anything?
No, this is the entire script for it. Just trying to get the first one working before adding anything else to it.