How to make a variable from function declared for all scripts in the scene?

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

Greets!

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

Cheers!

:bust_in_silhouette: Reply From: Eric Ellingson

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

and

# AnotherNode.gd

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

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():
	Input.set_custom_mouse_cursor(cursor)
	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":
			 Input.set_custom_mouse_cursor(null)
			 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():
	randomize()

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

func _on_Use_pressed():
	roll_a_dice(1, 100)

func roll_a_dice(minimum, maximum):
	var roll = randi() % (maximum-minimum+1) + minimum
	print(roll)
	 	
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:
				     print("okokok")

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

Syl | 2020-02-13 18:13