How to reach a variable in another function in another node?

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


Got a cursor_state variable in the Use button node and its func _on_Use_pressed():, and i want to get it for a click on the second CollisionShape.

My tree: Imgur: The magic of the Internet

Trouble is, i could get a var at the begining of the Use script, but not the same var inside its function _on_Use_pressed.

So the state of the cursor_state variable changes after calling _on_Use_pressed()? Yet you can’t access the variable from inside the function? If the Use node’s attached script extends the button class, the function can access the variable (maybe by using the self keyword).

Ertain | 2020-02-20 00:28

Thxs, but the Use node can access the var cursor_state, the CollisionShape cannot, it just get the first var cursor_state, unchanged at the beginning of the script:

 extends Button
    var cursor = load("res://Textures/Icons/UseC.png")
    var cursor_state = "normal"
 func _on_Use_pressed():
    	var self.cursor_state = "selecting"

See? And ‘self’ doesn’t change that.

Syl | 2020-02-20 12:32

Rather than asking how to transfer a variable’s value between 2 seemingly unrelated nodes in your scene tree, maybe you can just explain what you’re actually trying to do here.

It feels like there’s probably something off in your architecture, and rather than providing some contorted, hacky solution to this specific case, maybe you need a different approach.

So, maybe give us a better explanation of what you’re actually trying to do here?

jgodfrey | 2020-02-21 03:28

See my coment to njamster.

Syl | 2020-02-21 12:18

:bust_in_silhouette: Reply From: Xian

Just checking if I understand. You want to pass the value of "cursor_state " from the “use” node to the “CollisionShape” node right?

Well, the roundabout and lazy method I got is to save it on a “non-visible label node”. Since the value of "cursor_state " is a string u can just save it as

 extends Button

 var cursor = load("res://Textures/Icons/UseC.png")
 get_node("cursor_state").text = "normal"

 func _on_Use_pressed():
        get_node("cursor_state").text = "selecting"

and have “CollisionShape” access it like this:


there are better ways than doing it this way but its too difficult to teach without seeing most of your code.

There really is no point in adding a “non-visible label node” containing the text. Saving the current state as a string variable (as OP does) is perfectly fine. The problem here seems to be just about accessing the label-node properly. With which your answer does not help, because you abbreviated the path even though the scene tree was provided. I understand that you’re probably aware of how to do this, but OP clearly isn’t!

njamster | 2020-02-21 11:36

:bust_in_silhouette: Reply From: njamster

Assuming the scene tree you presented is the active main scene, you should be able to access the cursor_state from the CollisionShape2D with this line:


This path is provided relative to the root of the tree, instead of relative to the node which is running the script. So it does not matter to which node in the tree the script is attached that is running this code, as long as it’s in the same tree as the Button and the path to the button does not change.

Thxs, but the issue is that if i can get cursor_state var from the Use button, i cannot get it once it has been modified to “selecting” by button_pressed func. I just get the cursor_state set to “normal” from the script. See my code at the begining of the thread.

This is the code of my collisionshape:

func _on_Area2D_input_event(_viewport, event, _shape_idx):
	var roll = roll_a_dice(1, 100)
	var node = get_node("/root/Node2D/Control/Use")
	if event is InputEventMouseButton and event.pressed and event.button_index == 1:
		if roll < GlobalP.charm and node.cursor_state == "selecting":

That cursor_state should be “selecting” cause i click the collisionshape having pressed the button and changed my cursor just before. But this script doesn’t recognize it, it just get the cursor_state “normal”…

Syl | 2020-02-21 12:13

Then you’re likely not setting the value of cursor_state correctly. Did you make sure _on_Use_pressed() is connected and actually run when you click the button? The button script should (judging from your descriptions) look like this:

extends Button

var cursor = load("res://Textures/Icons/UseC.png")
var cursor_state = "normal"

func _on_Use_pressed():
    cursor_state = "selecting"

njamster | 2020-02-21 12:24

Yes, the _on_use_pressed is connected: the signal is set and my cursor is changing as it should.
If i put “selecting” instead of “normal” for the first var cursor_state, then my collisionshape script gives the expected result…

Syl | 2020-02-21 12:53

Make sure, there is no var in front of the second mention of cursor state.

If you do…

extends Button

var cursor = load("res://Textures/Icons/UseC.png")
var cursor_state = "normal"

func _on_Use_pressed():
    var cursor_state = "selecting"

…, you declare a global variable cursor_state with a value of “normal” and then, when you click the button, you create another (local) variable with the same name and a value of “selecting”. However, as the variable is only local, outside of the function, accessing cursor_state will still give you the value of the global variant, i.e. “normal”.

njamster | 2020-02-21 13:22

Ok, that’s what i got with it: Imgur: The magic of the Internet

Close, but for the fact that it’s a neverending list of print, although i clicked only once… And the “okokoooooooooooooook” appears only once or twice…

Syl | 2020-02-21 13:57

The “okokoooooooooooooook” is printed when you click the Area2D with the left mouse button. The rest is printed on any input-event, including e.g. mouse movements. So just move your if-condition to the first line of your _on_Area2D_input_event-function and everything should work as expected.

njamster | 2020-02-21 14:30

Okokooooooooooooooooooook! cheers! :slight_smile:

Syl | 2020-02-21 15:16