I'm trying to make a sort of dynamic action loader to add generic actions to nodes. Ex: in Global:
var ACTION_BY_ID = {
ACTION_ATTACK: load("res://scripts/actions/ActionAttack.gd"),
ACTION_MOVE: load("res://scripts/actions/ActionMove.gd")}
Then in a LoaderScript:
static func add_action_by_id(var actionId: int) -> Node2D:
var action: Action = Global.ACTION_BY_ID.get(actionId);
var actionNode = load(str("res://characters/actions/", action._get_name(), ".tscn")).instance();
Is it possible to do something like this? Loading a script in a dictionary and using it in a different script? The last line above throws error: "Attempt to call function 'instance' in base 'null instance' on a null instance."
Action (abstract; not to be instanced) would be something like this:
extends Node2D
class_name Action
static func _get_name() -> String:
return "ErrorAbstractAction";
And an actual extending Action:
extends "res://scripts/actions/Action.gd"
class_name ActionAttack
static func _get_name() -> String:
return "ActionAttack";