Exporting nodes not in scene tree

Godot Version

4.6

Question

I want to know if its possible to have an export variable to accept nodes not in scene yet.

I have a skill system and i decided it would be easier to break a skill down into individual components that inherit from a class base module, so i could build skills from just changing some things on editor and one skill brain, with an array module list to accept modules

I tried making it like this

@export var module_list:Array[BaseModule] = [BaseModule]

But it gave me an error saying it couldnt have an element BaseModule in an array type Array[BaseModule] which iam assuming it mean i cant put base module in there because its not in the scene tree. I know i can just not declare the type of array but iam trying to use static typing.

This array is used later in a for loop to use the new function and then add child in the skill brain script

1 Like

Use Resource instead of Node.

2 Likes

Use resources as @dragonforge-dev said, but remember you can’t add nodes to them iirc cuz they’re outside of scene tree

1 Like

BaseModule is a type. You declared the array to contain objects of this type, not the type itself. So:

@export var module_list:Array[BaseModule] = [BaseModule.new()]
3 Likes

How i would use resources for skill parts i already use resources for stats such as speed damage etc

But i dont know how i would use resources for skill parts.

I only use node because of physic process which i use to components work.

Per example i have a skill component for movement with a function move

func move(delta): 
     skill.global_position += direction * speed * delta

Then i would call move in the movement component physics process.

Ok, then let’s back up.

Why isn’t the node in the scene yet?

How did you come to the decision to use an @export variable?

I use export because i want to be able to craft skills mid game but since i dont have an ui for that yet so iam using export to test the modular skill system before adding the ui. Thats why the nodes arent in

I want to be able to be able to write the component name in the editor or drag it in from resouce folder instead of having to open script and typying it in.

Since iam making the skill at run time i would need nodes at run time

Actually i think i can use packed scenes for that i will try

Still not following why, but here are two options:

PackedScene

This option preloads the skill when the scene loads, even if you don’t use it.

@export var skill_scene: PackedScene

func make_skill() ->void:
	var skill = skill_scene.instantiate()
	add_childe(skill)

Path

This option doesn’t load the skill until you need it.

@export_file_path("*.tscn") var skill_path: String
	var skill = load(skill_path).instantiate()
	add_childe(skill)