Godot Version
I am on the Stable version of Godot 4.3
Question
Hi this might be a dumb question but I can’t really figure this out myself so I’m hoping to get some clarifications here.
In my game I have a Chara class with the following property.
var model_component: ModelComponent
In this case, ModelComponent refers to a simple class that only has the following.
extends Node3D
class_name ModelComponent
Now in the _ready() function of that Chara class I have, I set that property using this code.
func _ready():
for child in get_children():
if child is ModelComponent:
model_component = child
Then I have another class named Hero that extends the Chara class, and in the _ready() function of that new class I call the _ready() function of the Chara class with this :
func _ready():
super._ready()
Upon testing it the correct function gets triggered and thus the model_component property is assigned like I want to.
Unfortunately in Hero when I try to use that inherited reference (model_component) in another function it says the reference is null. Why?
Does your hero have a model component as a child? Can you show your scene tree?
Yes, sorry I should’ve specified that. I do have a hero scene with a CharacterController3D that has a hero.gd script which contains that Hero class and a Node3D as a child which is the ModelComponent.
Interesting, can you show the function where you get a null exception? Is there a chance it happens before _ready()
?
This is where the exception occurs (this is a placeholder for testing).
It does get played once before the Hero’s _ready() but even after putting a return at the start of the function and checking during runtime in the _process function the reference stays null.
In fact, even after the super._ready() call in Hero’s _ready() the reference stays null.
func _ready():
super._ready()
print(model_component)
This should print something if you have at least one valid ModelComponent as a child. This variable will certainly be invalid before _ready
, so never try to use it before then.
Pretty late reply, sorry about that but I have more news on the problem.
This does print the model component so the inheritance thing seems to be working fine.
But now I have encountered a new issue.
print(chara.model_component)
chara.play_anim("Wait", 0.3)
I have this in another script, chara is a variable of type Chara and is a reference to the Hero node.
What is absolutely baffling me is that the first line actually does work and prints the model component but when I call the function with chara and I do this in the function :
print(self.model_manager)
It prints null. Why?
Nevermind! I just made a silly typo, I wrote “model_manager” instead of “model_component”.