-
Notifications
You must be signed in to change notification settings - Fork 2
Enums
In TaffyScript, an enum is a named set of constant values. To declare an enum, write enum
followed by the enum name. Inside of a block, you write each constant name seperated by a comma. Each name gets assigned a value starting at 0, and then iterating by one. For example:
enum HorizontalAlignment {
Left, // = 0
Center, // = 1
Right // = 2
}
If you wish to assign a value to the name you can do so, and each successive name will increment based on that value:
enum State {
Walking = 5, // = 5
Running, // = 6
Crawling, // = 7
Laughing = 0, // = 0
Sleeping // = 1
}
As such, two names in the same enum can have the same value.
To use an enum, write the enum name, a dot, and then the enum value name like so:
var value = State.Walking;
Typically when writing enums, I use PascalCase for both the name and values, but this is not mandatory.
You can directly use enums defined in external .net libraries:
//In a c# project
namespace CSharpExample
{
public enum External
{
First,
Second
}
}
//In a TS project
using CSharpExample;
script main {
print(External.First);
}
It will automatically convert the enum value into a proper TS type. This means you can't import functions that take an enum parameter, though you might be able to in future versions.