0 votes

This came about while I was setting up a state machine.

The state machine includes the line:
class_name StateMachine

and the various states (which are all children of the FSM all include this line:
var fsm: StateMachine

From what I've seen of other peoples' state machines, this is pretty common.

But when I first open godot, it throws the following error on all of of the states, and if I try to run it, it crashes with the same error message:

Parser Error: The identifier "StateMachine" isn't a valid type (not a script or clas), or couldn't be found on base "self".

After clicking back and forth between the nodes for a minute, the engine suddenly recognizes the class_name declaration and works just fine.

This leaves me with a weird situation -- I don't know if my code is broken, but it's not working well enough to share, because it's unreliable. Is there a trick to getting this to play nice? Is it a known bug? How can I get Godot/my game to play from a cold start without crashing with this error?

Thank you for any advice you can provide.

Godot version v3.2.2.stable.official
in Engine by (26 points)

Have you instantiated any instances of StateMachine? Or are you just using the type declaration?

var fsm: StateMachine doesn't instantiate an object, it just tells the editor that fsm has all the attributes of a StateMachine, so you get little boxes while editing scripts to select member properties or methods. You need to instantiate a StateMachine via fsm = StateMachine.new()

Thanks! I haven't but I will try it and see if it fixes this issue. Since I'm not super clear on the proper usage or on whether this has changed since the tutorial I followed was written, would instantiating be the right move for this: https://gdscript.com/godot-state-machine ?
It's basically what I used for my state machine.

Thank you for your help!

1 Answer

0 votes
Best answer

Okay I think I finally figured this out and want to add it in case anyone runs into this issue. It's been awhile since I was working on the game regularly (though hopefully that'll change now) so apologies when my terminology is off.

The big difference between the examples I was following and my own setup was that the examples were all single-level games, and mine has multiple levels, but is run from this global node that handles level loading/transitions, stuff like that. (In effect, each level of my game is a standalone game because I have no idea what I'm doing. Originally it'd been just one level, but I eventually made copies of level one, redid the layout etc, and changed the old main files into level1, level2, etc. Then I used global nodes to handle switching between levels, saving, stuff like that.)

But I'd originally declared the StateMachine class in a script inside level 1. So when I ran the game from this outer level, I think it didn't see the class declaration until I actually opened Level 1 in the editor and opened the relevant .gd file.

To fix this, I created a State.gd and a StateMachine.gd file to declare the classes and moved them up to the root. Then I adapted the individual instances in the levels to extend State and StateMachine. Now it's working reliably from a cold start.

This was one of those problems that's probably pretty easy to show someone, but hard to ask about, and I wish my terminology was more helpful here, but if you run into weird class issues, and your game has a weird layout like mine, maybe that's the issue.

Now I'm gonna go make some NPCs.

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