Variable in subclass is null while Parent is not

Can the duplicate() function cause this issue?, so i duplicate the skills and put in in the PlayerInventory but sometimes the variable from subclass is nul

image

class_name CombatItem extends Area2D

@export var skillName : GlobalEnums.SkillName
@export var weaponName : GlobalEnums.WeaponName
@export var skillType : GlobalEnums.SkillType
@export var skillDescription = ""
@export var skillStatsType : Array[GlobalEnums.SkillStatsType] = [GlobalEnums.SkillStatsType.Damage]

var id = 0
var damage : float = 1.0
var knockbackStrength = 0
var player

func _ready():
	if has_node("Hitbox"):
		get_node("Hitbox").connect("area_entered", _on_hitbox_area_entered)
		get_node("Hitbox").connect("area_exited", _on_hitbox_area_existed)		
	SignalBus.connect("player_died", PlayerDieDisableObject)
	SignalBus.connect("world_ready", OnWorldReadyHandler)

func OnWorldReadyHandler():
	player = Global.world.localPlayer
	print("PARENT'S PLAYER: " + str(player))
class_name Skill extends CombatItem

var amount : int = 1

var canExplode = false
var isUsed = false

var currentSkillCooldown : float = 0.5
var skillCooldown : Cooldown = Cooldown.new(0, 0)
var flySpeed : FlySpeed
var isPlayerDie = false

func PlayerDieDisableObject():
	visible = false
	process_mode = Node.PROCESS_MODE_DISABLED

func _process(delta):
	print("SUBCLASS'S PLAYER: " + str(player))
	SkillBehavior(delta)

Because _process() can be called before the child nodes are ready and have called their _ready().
Guard it by checking if player.is_node_ready(): so you guarantee that’s the case.

1 Like

thanks for replying but i still don’t know how to do it, where should i use that is_node_ready() function?, the process keep printing the “player” variable is null

Should i do like this?

func _ready():
	if has_node("Hitbox"):
		get_node("Hitbox").connect("area_entered", _on_hitbox_area_entered)
		get_node("Hitbox").connect("area_exited", _on_hitbox_area_existed)		
	SignalBus.connect("player_died", PlayerDieDisableObject)
	SignalBus.connect("world_ready", OnWorldReadyHandler)

func OnWorldReadyHandler():
	while(true):
		if Global.world.localPlayer.is_node_ready() == true:
			player = Global.world.localPlayer
			break
			
	print("PARENT'S PLAYER: " + str(player))

result: i can’t, it didn’t work

No, in the “Skill” class.
Instead of

func _process(delta):
	print("SUBCLASS'S PLAYER: " + str(player))
	SkillBehavior(delta)

write

func _process(delta):
	if player.is_node_ready():
		print("SUBCLASS'S PLAYER: " + str(player))
		SkillBehavior(delta)

that’s all

The player variable of the subclass is null, i can’t call function on it

oh, then

func _process(delta):
	if player != null and player.is_node_ready():
		print("SUBCLASS'S PLAYER: " + str(player))
		SkillBehavior(delta)
1 Like

Actually i just fixed it, i don’t know if you’ll understand it, my code is a mess but here’s the fix, because the Skill subclass scrippt wasn’t created yet, it was only created when the player chooses a skill, so i need to check it when the script is created, not when the world node is ready.

#In parent class
	if Global.world != null:
		if Global.world.localPlayer != null:
			player = Global.world.localPlayer

Ah, that also makes some sense. I don’t know what calls OnWorldReadyHandler() but I assumed it was called once when the world node was ready. shrug

1 Like

yes you’re right, I should check when the script is created, not when the world is ready, they’re completely different