Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | klaatu.barada |
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!!")
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.
exuin | 2022-03-09 03:45