Named Integer States
Lesson 37 of 48 in Coddy's GDScript for C# Developers course.
C# enums are distinct integral value types, although casts can still create unnamed numeric values. GDScript enums supply named integer constants; omitted values increment from the preceding value. A named enum is accessed through its script class or local name. Do not assume an int parameter validates membership automatically. Check external codes and define invalid-state behavior explicitly. The teaching values differ from the exercise state machine and do not disclose its transitions.
Complete C# comparison. Compile all listed C# files together; Program.Main is the entry point.
C# file Mode.cs:
public enum Mode { Low=4, High }C# file Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
public static class Program {
public static void Main() {
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine((int)Mode.High);
}
}C# output:
5GDScript example file demo_modes.gd:
class_name DemoModes
extends RefCounted
enum Mode { LOW = 4, HIGH }GDScript example file main.gd:
extends Node
func _ready() -> void:
print(DemoModes.Mode.HIGH)GDScript output:
5Challenge
EasyDefine JobState enum Phase {WAITING=0,RUNNING=1,DONE=2}. Static advance(value:int)->int advances 0 to 1 and 1 to 2, leaves 2 at 2, and returns -1 for every other integer.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Your code goes in job_state.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.
Try it yourself
extends Node
func contract_ok(data: Array) -> bool:
if JobState.Phase.WAITING != 0 or JobState.Phase.RUNNING != 1 or JobState.Phase.DONE != 2 or JobState.advance(9) != -1 or JobState.advance(1) != 2: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for C# Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command Count5Objects and State
Scripts as ClassesInitializationInstance MethodsRecap: Independent BasketsProperty Setters8Boundaries and Lifetimes
Named Integer StatesNull and Type TestsSorting CopiesReference LifetimesRecap: Sorted TotalsRecap: Ordered CommandsPractice on your own: Online GDScript compiler