Im making a very simple state machine whith a parent node which executes all the code and a child node which stores all "global" variables (related to that instance) and changes its parent code when the parent itself prompts to do so.
for changing the code i use get_parent().script = load(Script_resource_path)
, and it crashes the game without an error on the inspector or the command window. Is that a bug or im missing something?
Node structure:
Enemy.KinematicBody(Enemy_unloaded.gd)
Variables.Node(Variables.gd)
# Enemy_unloaded.gd
extends KinematicBody
class_name EnemyBase
func _ready():
print("Enemy unloaded")
$Variables.change_state("Overworld")
# Variables.gd
extends Node
export var StateDictionary = {
"Unloaded" : "res://Script/Enemy/Enemy_unloaded.gd",
"Overworld" : "res://Script/Enemy/Enemy_overworld.gd",
#and so on...
}
func change_state(new_state_name):
get_parent().script = load(StateDictionary[new_state_name])
# Alternative method. Crashes too
#get_parent().set_script(load(StateDictionary[new_state_name]))
get_parent().set_process(true)