Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | xPheRe | |
Old Version | Published before Godot 3 was released. |
Hi there! I’m new to the forums, trying Godot for my pet project.
I’m trying to port some code which makes heavy usage of the state pattern and subclassing, but I’m not fluent enough with Godot to reach my goal, I hope you can help me.
Here’s a oversimplified example which fails:
extends KinematicBody2D
var status = Status.idle()
class Status:
static func idle():
return StatusIdle.new()
static func jump():
return StatusJump.new()
class StatusIdle:
extends Status
class StatusJump:
extends Status
func _ready:
...
It fails no matter the order of declaration or usage of these classes with a Parser Error: Identifier not found
.
My question is: Is there a way to allow for such constructions in Godot?
Thanks!
Yeah because StatusIdle
hasn’t been defined before you use it. You need something like a forward declaration in C++. What are you porting from?
duke_meister | 2016-04-18 23:35
But GDScript does not support forward declaration, does it? Is there any workaround?
BTW, I’m porting from JS.
Thanks.
xPheRe | 2016-04-19 00:14
Not as far as I know but then I haven’t been using it for long and haven’t heavily used the class system. So yeah I don’t have a solution unfortunately, apart from redesign your state machine in a Godot way.
duke_meister | 2016-04-19 00:17