0 votes

Okay, so I have this error that’s driving me nuts as it’s a common error and yet I don’t know how to fix it. It goes something like this:

Attempt to call function 'get_children' in base 'null instance' on a null instance.

Now typically, an error like this means that I can’t get a node properly due to a typo but, I don’t think that’s the case. The IDE told me that the error because with this line of code:

for r in shotgun_blast.get_children():
    r.cast_to.x = rand_range(spread,-spread)
    r.cast_to.y = rand_range(spread,-spread)

However, this line of code only had an error my ready(): function which looks like this in:

func _ready():
    max_ammo = ammo
    randomize()
    for r in shotgun_blast.get_children():
        r.cast_to.x = rand_range(spread,-spread)
        r.cast_to.y = rand_range(spread,-spread)

Elsewhere, this code runs perfectly well like when I used it in the firing_shotgun():

func firing_shotgun():
    if Input.is_action_pressed("fire") and not ammo <= 0:
        if using == false:
            using = true
            ammo -= 1
            anim_player.playback_speed = firing_speed
            anim_player.play(firing_anim)
            for r in shotgun_blast.get_children():
                r.cast_to.x = rand_range(spread,-spread)
                r.cast_to.y = rand_range(spread,-spread)
                if r.is_colliding():
                    bullet_holes(r)
                    if r.get_collider().is_in_group("enemy"):
                        r.get_collider().health -= 50
            yield(anim_player,"animation_finished")
            using = false
    elif using == false and not anim_player.is_playing():
        anim_player.playback_speed = 1
        anim_player.play("idle")
    ammo_count.text = "Ammo:" + String(ammo)

The fact that the error only shows up in the Ready() function and firingshotgun() implies that I do have proper access to the node shotgunblast. Could you please him me?

Godot version Godot 3.2
in Engine by (22 points)

If your scene misses a resource then it won't be properly loaded

1 Answer

0 votes

This implies that Your node shotgun blast is not ready at the moment of main script ready(). What do You do with this variable shotgun_blast ? Is it referencing one node entire time or different nodes being created on the go ? DO you introduce shotgunblast as "onready" ?

by (7,449 points)

I did introduce shotgunblast with onready var at the beginning of the script. Could that be the problem?

No, it is good.
So there must be an issue with replacing the reference to shotgun blast. These two piecies of code - with ready() and firingshotgun() - do they come from single script ?
If Yes, show me the code of instancing and queuing free shotgun blast.
If no - we will need to take a look at both scripts, where variables are introduced and their scene_tree scheme.

"These two piecies of code - with ready() and firingshotgun() - do they come from single script ?"

Yes.

"If Yes, show me the code of instancing and queuing free shotgun blast."

I'm not sure if I'm understanding this correctly. I don't have a code for that. All I did was get the node for the "shotgun blast" at the beginning of the script. It's actually a spatial node filled with a bunch of raycasts. I was following a Garbaj Youtube tutorial on the subject entitled "Shogun(Godot Tutorial)", here is a link to it if it helps:

https://www.youtube.com/watch?v=9spikY2VhbY

This is really weird. It totally sounds like typo or mistake in scene hierarchy, but nothing looks wrong here. Can you printscreen your scene tree hierarchy ?

Also, are You sure other parts of code work well ? Like shotgun actually shots, and it works when ready() function is disabled ?

And You introduce it as:

onready var shotgun_blast = $head/Camera/shotgun_blast

?

This should be like this. If You really properly introduced this variable, like above, then this is some kind of madness :) You might still try to debug it by referencing the node directly, not using shortcuts, so replace all shotgun_blasts with above hardcode path.

Edit : I notice head is big caps in tutorial, your scene has small ;) Didn;t You mess it up ? ;)

I actually introduce it like this:

onready var shotgunblast = $"../head/Camera/shotgunblast"

The reason why I do this is because all the code is for a weapon script that I want to be reuseable when making any other type of weapons; I could just extend it to other scripts. Here is the other half of the node structure:

https://imgur.com/B9b7KNV

The bass script is attached to the Gun Node. What confuses me the most is that I have all sort of code for different type of weapons(knife,pistol,shotgun, etc) and all the others are working just fine. However, only half of the shotgun code seems to work. I'm confused.

I don't think that I need to name all the variable the same way it was named in the tutorial.

No, This is incorrect reference. It does not lead to shotgunblast at all. I don't see how are you going to reuse it like this. This is "hardcode" reference and it is never elastic.
".." before "head" indice get
parent(). So you are trying to call getparent().getnode("head"), which does not exist. Try pasting my reference and see if it works. The reason error only shows up in ready() is because it doesn't have a chance to reach later parts of code.

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.