The possible values for ServerState are defined as constants. Enums in this language do not require a special keyword to generate successive constant values automatically.
By using a dictionary, values of ServerState can be printed out or converted to strings:
If we have a value of type int, we cannot pass it to transition - the compiler will complain about type mismatch. This provides some degree of compile-time type safety for enums.
Here’s an example of how our main function and state transition function might look:
The transition function emulates a state transition for a server; it takes the existing state and returns a new state.
To run this program, ensure you place the script inside a Node, attach it and run the scene. You should see the output states printed in the console.