I have a scene, Player.tscn
. Near the top of Player.tscn
, I have "class_name Player
"
Now, when instantiating the Player
, I have, as far as I see it, two options:
player = Player.new()
or
player = load("res://Player.tscn").instance() as Player
Now, the first version seems best to me...but it clearly isn't. If I use .new()
, it claims that it has no children, and any method calls that attempt to get to its children (.get_texture()
on a Sprite
, e.g.), produces things like "Attempt to call function 'get_texture' in base 'null instance' on a null instance
", because apparently Player
has a no children, something which a call to .get_child_count()
confirms (i.e. it returns 0).
Of course, doing it the second way, everything works fine. But why? Why can't I just use .new()
if I've registered it as a class using class_name
?
I've asked the question on Stack Overflow as well, but then I realized I've got a much better chance of an answer here. I also read this recent thread, but as it was sort of answering the opposite (though very related) question, it didn't help much.