#Reference variable from other scripts

1 messages · Page 1 of 1 (latest)

hollow sparrow
#

I'm trying to reference variables across scripts so that I can have a single script that handles what UI elements should be unlocked based on factors however I'm unsure how to get them to interact with each other?

lime blaze
#

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()

hollow sparrow
#

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?

lime blaze
#

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?

hollow sparrow
#

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...

lime blaze
#

So that script is not a node and therefore has no access to ready function

hollow sparrow
#

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

lime blaze
#

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)

hollow sparrow
#

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

lime blaze
#

Sorry I think I misunderstood.
Is that the whole UnlockSystem script? Does it extend anything?

hollow sparrow
#

No, this is the entire script for it. Just trying to get the first one working before adding anything else to it.

lime blaze
#

So in the script where playerControl_created signal is defined...

Try connecting the signal there in its ready function.

playerControl_created.connect(UnlockSystem.unlock_checker)

#

You're on the right track regardless

#

G2g for now good luck

hollow sparrow
#

I've been playing around with it a bunch but just get this when adding it into the _ready of the script with the connect

#

That's with Game.ref.UnlockSystem.unlock_checker

With just UnlockSystem.unlock_checker I get