Save a scene instance to a variable to access the .gd class members of its root Node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By takkun324

I believe I’m misunderstanding how an instanced-scene is utilized (here’s a quick little example project).

EDIT: OK, so the example works. Why wont my main project behave the same way?
SOLVED: Within my project, Character.gd I had _init( some_arg ): set and… I don’t know. Removing the argument fixed the problem. I also re-added the class name class_name Character without any issue. Hopefully this may save 2 days of troubleshooting for someone else. I’ll update my example to help illustrate the issue. On the up-side, I’ve read a lot more about the difference between .new() and .instance() ~ very helpful for in-code instantiation.


All I want to do is access char_name from RootScene.

What’s going on here?
If I create a scene starting with KinematicBody and attach a script like so…

class_name Character extends KinematicBody 
char_name = "Npc Boi"

…then I create and save a scene as follows…

Character
 |- CollisionShape
 |- Sprite3D
 |- RayCast

…then…

If I hover over “Character” it says “Type: Character.”
In the editor, I add an instance of this scene as a node into another scene…

RootScene
|- WorldScene
|- NPCS
     |- Character

…now…

In the editor, hovering over the scene~instance, it says Type: KinematicBody.

RootScene.gd
extends Node
var c : Character
func _ready():
	c = get_node("Character")

…now…

Godot says Trying to assign value of Type: KinematicBody even though the script of its root (“Character”) extends it. If I save it as type KinematicBody then var c will not exist.

How can I save a reference to my character from RootScene._ready() without adding a bloatware-container root?

We seem to lose the script~reference when i instance the Character scene. Weirder: In the editor, when I switch from “Local” to “Remote” than hovering over both instances (the tree and the instanced scene) will say “Type: KinematicBody”. If I change RootScene.gd var c : KinematicBody then, as expected, char_name is not accessable.