0 votes

Hello!
I am trying to reorganize my code using inheritance and I can't seem to call the child function.
My project has a class Weapon with basic information about mundane weapons and each weapon has a separete scene that extends from Weapon, so when the Player fires, the Weapon class should call the child.function() but "Attempt to call function 'shoot' in base 'null instance' on a null instance." appears. Anyway, here is some of the code:

extends Spatial
class_name Weapon
#Variables....
var current_weapon = 1

onready var ruffian_revolver_s = get_tree().get_root().find_node("Ruffian_Revolver_S")
onready var hunting_shotgun_s = get_tree().get_root().find_node("Hunting_Shotgun_S")

func _ready():
pass
func _process(_delta):
if Input.is_action_just_pressed("primary_fire"):
    if current_weapon == 1:
        ruffian_revolver_s.shoot()
            elif current_weapon == 2:
        hunting_shotgun_s.shoot()

My custom gun scene code:

extends Weapon
class_name Ruffian_Revolver_S
#Variables...
func _ready():
weapon_name = "Ruffian_Revolver"
weapon_type = "Revolver"
recoil = 0.2
weapon_damage = 50
fire_rate = 0.32
clip_size = 6
reload_rate = 3
current_ammo = clip_size

func _physics_process(delta):
pass
func shoot():
if current_ammo > 0 and not reloading:
    animation_player.play("Ruffian_Revolver_Shoot")
    get_raycast()
    spawn_muzzle_flash()
    can_fire = false
    current_ammo -= 1

    yield(get_tree().create_timer(fire_rate),"timeout")
    can_fire = true
elif not reloading:
    print("Reload!!")
Godot version 3.4.2.stable.mono
in Engine by (12 points)

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

This means that whatever variable should be pointing to the Weapon, is pointing to nothing. The error lies in the code that's creating the Weapon.

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.