I still have no clue how calling variables from other scripts work

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

Hello,
I’ve been developing this rhythm game, (I know it’s quite ambitious for my second game on this engine) and I’ve come across a recurring problem: I can never seem to effectively call variables from other objects.
I’ve had this problem before with a group of position2d nodes trying to communicate with each other, but those were all children of a single node so I just used:
var pos = get_parent().get_node("Position2D")
However now, I am trying to call variables from a node in a different scene, and I keep getting an Invalid index: null instance error. Any advice would be appreciated, as i am very new to this engine.

code for the variable in question:
`func _on_Area2D_body_entered(body):
var piss = get_parent().get_node(“Player”)

if "Player" in body.name && piss.ATK == false:
	piss.HP -= 1 
	print_debug (piss.HP)
	queue_free()`
:bust_in_silhouette: Reply From: kidscancode

To access another node, you need a reference. It doesn’t matter where that node is in relation, as long as you have a reference you’re good.

Your area is detecting contact with a body. A reference to that body is passed to the function: body.

If you want to know if the body it contacted is the player

if body.name == "Player" && body.ATK == false:
    body.HP -= 1

You don’t need to get_node() the player, since you already have it!

thanks man! ill be sure to try that out!

LimeLite | 2020-08-30 01:31