4
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

According to this QA post you should be able to access your variable doing get_node("node").variable

If that doesn't work try using a getter method for your variable:

var done = 0 setget ,done_get

func done_get()
    return done
\$\endgroup\$
2
  • \$\begingroup\$ I tried the first method, edited my question to indicate that. I should place that function in each of the scripts? and how to call it from the main node script? \$\endgroup\$
    – Lynob
    Commented Jul 29, 2017 at 16:57
  • \$\begingroup\$ You put that function in the script that has the variable done. And you access it like any other function get_node("node").done_get() or by the above method. \$\endgroup\$
    – Charanor
    Commented Jul 29, 2017 at 17:01
3
\$\begingroup\$

The reason why you get an invalid index error is that not your Sprite node holds the done variable but the Area2D node. That means you have to use:

if get_node("1A/Area2D").done == 1 and get_node("B/Area2D").done ...:
    get_tree().change_scene("res://scenes/scenes2.tscn")

If you renamed the Area2D node you must change the names accordingly.

Hope that helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .