+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 (21 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.

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.