Can't figure out why I can't access my nodes

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

Hi ! I can’t figure out how to make things work, I tried a lot of things but I’m lost

var atk is defined inside of Tenshi Area2D which is in my main scene

The error says “null instance”

I’m sorry if that question looks stupid or anything, but I really can’t figure it out

The image you provided does not work. If the error says something about a “null instance” you’re trying to access some property or call a method on an object that doesn’t exist. Usually because you’re using a wrong path with get_node().

njamster | 2020-05-08 16:19

The image does not work.
I guess you have referenced the node with something like this outside of a function/method:

var node = $"path_to_node"

but you need smothing like this:

 onready var node = $"path_to_node"

But that’s just a guess…

whiteshampoo | 2020-05-08 17:38

Ups sorry, I edited it it should work now

Well yeah, but I can’t figure out why this get_node() doesn’t work, it’s in the same tree tho

Rensae | 2020-05-08 17:38

:bust_in_silhouette: Reply From: njamster

From the screenshot it’s not obvious to which node the displayed script is attached but unless it’s attached to the “Character”-node (which it isn’t because that’s not an Area2D) the paths you are using are wrong. A path is relative to the node the script is attached to, so if your script is attached to e.g. Alice you have to use:

$"../../Character/Tenshi".atk and $"../../Character/Remilia".atk

The .. is the short-hand form of get_parent(), so you’re getting the parent of the “Alice”-node (Ennemies) first, then the parent of that node (Main), then it’s child-node “Character” and finally that nodes child-node “Tenshi” (or “Remilia”).

Thanks a lot it helped me understand how it worked

The actual script is attached to Alice who’s in Ennemies, my bad for not having say that earlier

However I tried it like that

stats.health -= $"Ennemies/Main/Character/Tenshi".atk

and still got "Invalid get index “atk” (on base : ‘null instance’)

Am I missing something really obvious ?

Rensae | 2020-05-08 18:39

It’s

stats.health -= $"../../Character/Tenshi".atk

not

stats.health -= $"Ennemies/Main/Character/Tenshi".atk

The latter would only work if the “Alice”-node itself had a child-node called “Ennemies”, which had a child-node called “Main” and so on…

njamster | 2020-05-08 18:59

Oh I see ! Thanks a lot again and sorry for misunderstanding that…

Now it work, but there is a new problem in there

My project actually has a character (Tenshi) that fires Bullet when a timer signals a Timeout function.
To make it work, whenever this timer Timeouts, Tenshi create a child that’ll hit the Ennemy : Alice.

But now, whenever Tenshi creates a child, it’s name becomes @bullet@X, where X is X+1 whenever it creates another.

Here is the code I use to create the bullet :

var new_bullet = preload("res://Character/Tenshi/Bullet.tscn")
var bullet = new_bullet.instance()
if AliceDead == false:
	if spell == false:
		add_child(bullet)
		bullet.position = Vector2(0, -140)
		bullet.name = "bullet"

Also, when my bullet hit Alice, Alice doesn’t take any damage but the bullet disappear. Is there a weird connection between Kinematic bodies and Area2D ?

To make the bullet disappear, I made this :

func _on_Area2D_area_entered(area):
queue_free()
pass

My bullet is a KinematicBody2D with an AnimatedSprite and a CollisionShape2D, and I added as a child an Area2D with a CollisionShape2D as a child to detect Alice who’s an Area2D

Hope I explained well enough and sorry if it’s bothering there

Rensae | 2020-05-08 19:22

whenever Tenshi creates a child, it’s name becomes @bullet@X

The @-sign indicates that a node was instanced through code (opposed to adding it manually in the editor). You can pass an (optional) second argument to add_child to disable this default behavior though: add_child(bullet, true)

However, the number in the end will stay, as node names (on the same tree-layer) have to be unique. Otherwise a node-path wouldn’t be unambiguous anymore and you couldn’t use it to refer to a distinct node in the tree.

Also, when my bullet hit Alice, Alice doesn’t take any damage

That’s a direct consequence of the naming rules: your code checks body.name == bullet, which will evaluate to false if the name is “@bullet@” or “bullet2”. I’d recommend you add your bullet-scene to a group. Then you can check if a body is in that group instead of checking for the bodies name.

if body.is_in_group("bullets"):
    stats.health -= $"../../Character/Tenshi".atk

Note that group names are case-sensitive, so “Bullets” and “bullets” are two distinct groups (if you create them).

njamster | 2020-05-08 19:48

Oh I see… that’ll be a problem for me then

I wanted to have the same bullet for every character with only the name who would change, but I guess It’ll not be a possiblity, I’ll not be too lazy and change my groups

Really, thanks a lot, I hope I’ll not run into any other problems there and thanks for your time to explain those things to me !

But the problem is still there with groups, it’s like the bullet disappear even before Alice detects it… Since it doesn’t print anything.

Rensae | 2020-05-08 20:00