Following scenario:
- KinematicBody2D with attached script "Player.gd".
- Classfile named "State.gd". Gets instanced by Player.gd by preloading as variable "state" and calling state.new() on it.
- Classfile named "Substate.gd". Gets instanced by State.gd the same way Player.gd did it with State.gd.
Player.gd delegates _input and _process computation to State.gd which in return delegates them to the Substate.gd by calling the corresponding functions and passing event/delta.
If I understand the docs correctly, I should be able to call functions within the instanced classes, yet I get errors like "Nonexistent function "func_name" in base Nil.
Here are code examples for better understanding what I am trying to do.
Player.gd
extends KinematicBody2D
onready var state = preload("State.gd").new()
func _input(event):
state.handle_input(event)
func _process(delta):
state.update(delta)
State.gd
onready var substate = preload("Substate.gd").new()
func handle_input(event):
substate.handle_input(event)
func update(delta):
substate.update(delta)
Substate.gd
var speed = 100
var velocity = Vector2()
var movement
func handle_input(event):
# set variables for directions (up, down, left, right)
# match event and set velocity according to it
# assign movement to velocity.normalized() * speed
func update(delta):
move_and_slide(movement)
From my understanding, this should work but I keep getting "nonexistent function" errors.
What is my mistake here or what have I misunderstood?