+1 vote

Why doesn't it work?

var enemy_lives = get_node("enemy2").lives
    if enemy_lives < 1:
        print("Hello")

Debugger:
Invalid operands 'Nil' and 'int' in operator '<'.

The script at enemy is:
var lives = 100

I look forward to any help.

in Engine by (73 points)

Yes you can... i do it all the time.

make sure you spelled everything correctly and that the path to the enemy node is valid( the enemy_lives variable is in the parent node of the enemy node and that the path is indeed a valid one and that you get the enemy node by doing that

It I've written everything right, but it still doesn't work.

Example node hirarchy : Parent/Child/GrandChild

In GrandChild:

var life = 1.0

In Parent:

var grand_child_life = $Child/GrandChild.life
print(grand_child_life)
Output: 1.0

In Parent:

var armor = 2.0

In GrandChild:

var parent_armor = get_node("../..").armor
print(parent_armor)
Output: 2.0

This will always work with that particular hirarchy.

Yes this works, but < does not work

Upload project file, I'll give it a quick look.

Another possibility is that you are doing both the initialization of the variable and the access to it inside the _ready functions of the objects. If this is the case, it is possible that the code assigning the value runs after the code that access it.

In the case of what Artium Nihamkin said, check your node hirarchy and make sure the node, that initializes the variable enters the scene tree first.

Thank you for your help, I have solved the problem.

Please log in or register to answer this question.

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.