Enums

An enum groups a set of related named constants under a single type. Enums are widely used in BattleScript to name the states, modules and events used by the event-driven state model, which keeps that code readable and consistent.

Declaring an enum

Declare an enum with the enum keyword followed by a name and a block of members. Members are separated by a comma or semicolon, and a trailing separator is allowed.

enum StateEnum {
	STARTUP = "STARTUP",
	HOME = "HOME",
	IN_GAME = "IN_GAME",
	DEAD = "DEAD",
	GAME_OVER = "GAME_OVER",
}

Member values

A member may be given an explicit value — a string, number or boolean — or be left without one. Different members in the same enum can use different value types.

enum MessageType {
	UNKNOWN = "unknown",   // string value
	COMMAND = 1,           // number value
	REQUEST = true,        // boolean value
	RESPONSE,              // no explicit value
}

Using enum members

Access a member with dot notation, EnumName.MEMBER. This is the idiomatic way to refer to states, modules and events.

enum BulletType {
	DAMAGE = 0,
	HEALING = 1,
}

state StateEnum.IN_GAME {
	module ModuleEnum.IR {
		event EventEnum.IN (EventData evt) => {
			if (evt.data.bulletTypeId == BulletType.DAMAGE) {
				battlecore::player.takeDamage(evt.data.hitValue);
			}
		}
	}
}

Related Topics