+1 vote

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!

in Engine by (26 points)
edited by

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?

But GDScript does not support forward declaration, does it? Is there any workaround?
BTW, I'm porting from JS.
Thanks.

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.

1 Answer

+1 vote
Best answer

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()
by (46 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.