How to transfer a variable from script to another

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Elie Obeid
:warning: Old Version Published before Godot 3 was released.

I have 3 sprites A, B and C, each one has area2d attached to it and collision shape

the script for A

extends Area2D
var done = 0

func _input_event(viewport, event, shape_idx):
    if event.type == InputEvent.MOUSE_BUTTON \
    and event.button_index == BUTTON_LEFT \
    and event.pressed:
	    get_parent().hide()
	    done = 1

And all like that

Now the main node has a script attached to it

extends Node2D

func _ready():
	if get_node("lA").get("done")==1 && get_node("B").get("done")==1 && get_node("C").get("done")==1:
		    get_tree().change_scene("res://scenes/scene2.tscn")

But it doesn’t work, why?

The error when doing if get_node("lA").done == 1 is

Invalid get index 'done' (on base: 'Sprite').

What I’m trying to do is collect some items, when done, move to a new scene

 

I push comment Button by mistake, but i want do an answer…

807 | 2017-07-30 02:01

:bust_in_silhouette: Reply From: Rasmus

You should be able to access a variable like this:

get_node("SomeNode").variable

So in your instance:

if get_node("lA").done == 1

tried that, the scene stops working, at least in my question above, everything works except the script

Elie Obeid | 2017-07-29 14:52

I am not sure you can have more than the event parameter in _process_input() but maybe if you could post your errors it would be easier to pon point your problem, and maybe some more explaining as your initial question is how to access a variable in another script.

Rasmus | 2017-07-29 15:42

please read my edit

Elie Obeid | 2017-07-29 16:52

:bust_in_silhouette: Reply From: 807

You are trying to get a variable in the sprite, but the variable is in the area, because the script is in the area (2 nodes after main node). The correct path is get_node(“1a/area2d”) or whatever … Sprite doesn’t have “done” variable.