This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes


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:

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

in Engine by (326 points)

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

Thxs, but the Use node can access the var cursorstate, the CollisionShape cannot, it just get the first var cursorstate, 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.

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?

See my coment to njamster.

2 Answers

0 votes
Best answer

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.

by (10,634 points)
selected by

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

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"

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

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

Ok, that's what i got with it:

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

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.

Okokooooooooooooooooooook! cheers! :)

0 votes

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.

by (271 points)

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!

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.