I am trying to instantiate a packed scene at runtime.
The root of the scene is my custom type that extends StaticBody3D.
However no matter what I do my main scene seems only to recognize it as a StaticBody3D type and not my custom one.
Casting to the right type always ends with null.
Is there proper way to do this?
I only need to initialize a few parameters for it.
My function that instantiate the scene:
func _dummy():
...
for h in high.keys():
var icon := ActiveObject.TEMPLATE.instantiate()
var ic = icon as ActiveObject
print(ic) # <-- this shows null
add_child(ic)
...
ic.set_data(high[h])
Iām looking at the docs and everything seems to be as it should be.
Is it possible that the type inference (:=) prohibits you from casting it to a derived type?
var icon := ActiveObject.TEMPLATE.instantiate()
Itās a shot in the dark, but try changing := to =.
Alternatively, you could just explicitly define the type of TEMPLATE; with your use case it should never not be ActiveObject. Correct me if Iām wrong on that.
I had a similar problem with Resources. It was fixed when i used āloadā instead of āpreloadā. I think its because preload loads the resource before your custom-classes are defined