Subclassing and forward declaration

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By xPheRe
:warning: 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

:bust_in_silhouette: Reply From: flosincapite

As far as I can tell, there’s no way to do forward declaration. JS (ES6) lets you cheat because it’s an interpreted, dynamically-typed language. So there’s no general solution to your problem, since JS just allows you to do stuff that Godot doesn’t.

However, you can solve this particular case by abstracting the factory functionality out of your Status class into a separate factory class. If you want to support the same syntax, you can then pass the factory to your Status class’s constructor.

class Status:
    func _init(factory):
        self._factory = factory
    func idle():
        return self._factory.idle()

class StatusIdle:
    extends Status

class StatusFactory:
    static func idle():
        return StatusIdle.new()

But you can probably just depart a bit from the JS version and use the factory class instead of Status itself for object initialization.

class Status:
    pass

class StatusIdle:
    extends Status

class StatusFactory:
    static func idle():
        return StatusIdle.new()

Thanks for your answer, I think gdscript 2 now supports any order in declaration, but this answer still holds true for 3.*

xPheRe | 2022-12-25 10:19