+1 vote

Hi ! I can't figure out how to make things work, I tried a lot of things but I'm lost
enter image description here

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

in Engine by (86 points)
edited by

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().

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...

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

1 Answer

+1 vote
Best answer

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").

by (10,628 points)
selected by

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 ?


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


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...

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 @[email protected], 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:
        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):

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

whenever Tenshi creates a child, it's name becomes @[email protected]

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).

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.

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.