0 votes


That is, got my var from a func onbutton_pressed():, and on another collisionshape script, i gottta check that var.


in Engine by (325 points)

1 Answer

+1 vote

In general, it is usually pretty helpful to include some of your code, even if it is just generalized to the specific issue you are experiencing.

# SomeNode.gd

func _on_button_pressed() -> void:
    var my_var = "something" # this is not accessible outside of this function call
    # other stuff...

If what you are asking is to retrieve my_var from outside that function call, unfortunately it doesn't work like that. What you could do is have something like this

# SomeNode.gd

var my_var # declare this as a node-level property

func _on_button_pressed() -> void:
    self.my_var = "something"
    # other stuff...


# AnotherNode.gd

func some_function() -> void:
    var some_node = load("res:///path/to/SomeNode.gd").new()

    var value_from_node = some_node.my_var
by (1,650 points)

Ok, here's some of my code :). Trying to trigger an area2d and rolling a dice to set the outcome.

Got a 'Use' button wich, when pressed, change the cursor to a lil hand, and i want to trigger the area2d only with this custom cursor. Here's the button's script to change the cursor:

extends Button

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

func _on_Use_pressed():
    var cursor_state = "selecting"

func _input(event):
    var cursor_state = "selecting"
    if event is InputEventMouseButton and event.pressed:
        if event.button_index == BUTTON_RIGHT and cursor_state == "selecting":
             cursor_state = "normal"

And here's the collisionshape's script that roll my dice and trigger the area2d only with the custom cursor:

extends CollisionShape2D

onready var cursor_state = get_parent().get_parent().get_parent().get_parent().get_parent().get_parent().get_parent().get_node("Use").cursor_state

func _ready():

func _physics_process(delta):
    if Input.is_action_just_pressed("ui_accept"):

func _on_Use_pressed():
    roll_a_dice(1, 100)

func roll_a_dice(minimum, maximum):
    var roll = randi() % (maximum-minimum+1) + minimum

func _on_Area2D_input_event(viewport, event, shape_idx):
        if event is InputEventMouseButton and event.pressed and event.button_index == 1:
              if cursor_state = "selecting"
                    if roll <= GlobalP.charm:

Trouble are the 'roll' on func area2d wich isn't declared, though in the same script, and the 'if cursor_state = "selecting" just before, wich is an unexpected assign...

Think that i have solved the issue of gettin this var from the button node, but i can't be sure...

If you could see through it, meanwhile i'll tinker with your data, and btw, what do you think of that method to get a var from outside with a dictionary?: link

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.