Flecs v4.0
A fast entity component system (ECS) for C & C++
|
Flecs Script is a runtime interpreted DSL for creating entities and components that is optimized for defining scenes, assets and configuration. In a nutshell, Flecs Script is to ECS what HTML/JSX is to a browser.
Some of the features of Flecs Script are:
$var + 10
)if $var > 10
)To learn Flecs Script, check out the Tutorial!
This section goes over the basic syntax over Flecs Script.
An entity is created by specifying an identifier followed by a scope. Example:
An entity scope can contain components and child entities. The following example shows how to add a child entity:
Note how a scope is also added to the child entity.
To create anonymous entities, use the _
identifier:
Entity names can be specified using a string. This allows for entities with names that contain special characters, like spaces:
String names can be combined with string interpolation (see below) to create names that are computed when the script is evaluated:
A tag can be added to an entity by simply specifying the tag's identifier in an entity scope. Example:
Pairs are added to entities by adding them to an entity scope, just like tags:
Components are specified like tags, but with an additional value:
For a component to be assignable with a value, it also needs to be described in the reflection framework.
A component can also be added without a value. This will create a default constructed component. Example:
Components can be defined in a script:
Components can be pairs:
When referring to child entities or components, identifiers need to include the parent path as well as the entity name. Paths are provided as lists of identifiers separated by a dot (.
):
To avoid having to repeatedly type the same paths, use the using
statement (see below).
To create a singleton component, use $
as the entity identifier:
Multiple singleton components can be specified in the same scope:
An entity can be created with a "kind", which is a component specified before the entity name. This is similar to adding a tag or component in a scope, but can provide a more natural way to describe things. For example:
This is equivalent to doing:
When using the entity kind syntax, the scope is optional:
If the specified kind is a component, a value can be specified between parentheses:
When the entity kind is a component, a value will always be assigned even if none is specified. This is different from component assignments in a scope. Example:
Applications can specify the following builtin kinds which provide convenience shortcuts to commonly used features:
Scripts can natively specify inheritance relationships between entities, which is useful in particular for prefabs. Example:
When specifying inheritance, the scope is optional:
This is equivalent to doing:
By default entity hierarchies are created with the ChildOf
relationship. Other relationships can also be used to create hierarchies by combining a pair with a scope. Example:
Scripts can contain expressions, which allow for computing values from inputs such as component values, template properties and variables. Here are some examples of valid Flecs script expressions:
The following sections describe the different features of expressions.
The following operators are supported in expressions, in order of precedence:
Symbol | Description | Example |
---|---|---|
! | Logical NOT | !10 |
* | Multiplication | 10 * 20 |
/ | Division | 10 / 20 |
% | Modulus | 10 % 3 |
+ | Addition | 10 + 20 |
- | Subtraction/negative | 10 - 20 , -(10, 20) |
<< | Bitwise left shift | 10 << 1 |
>> | Bitwise right shift | 10 >> 1 |
> | Greater than | 10 > 20 |
>= | Greater than or equal | 10 >= 20 |
< | Less than | 10 < 20 |
<= | Less than or equal | 10 <= 20 |
== | Equality | 10 == 20 |
!= | Not equal | 10 != 20 |
& | Bitwise AND | 2 & 6 |
\| | Bitwise OR | 2 \| 4 |
&& | Logical AND | true && false |
\|\| | Logical OR | true \|\| false |
The following table lists the different kinds of values that are supported in expressions:
Value kind | Type | Example |
---|---|---|
Integer | i64 | 42 , -100 , 0 , 0x1A |
Floating Point | f64 | 3.14 , -2.718 , 1e6 , 0.0 |
String | string | "Hello, World!" , "123" , "" |
Multiline string | string | `Hello World ` |
Entity | entity | spaceship , spaceship.pilot |
Enum/Bitmask values | from lvalue | Red , Blue , Lettuce \| Bacon |
Composites | from lvalue | {x: 10, y: 20} , {10, 20} |
Collections | from lvalue | [1, 2, 3] |
Initializers are values that are used to initialize composite and collection members. Composite values are initialized by initializers that are delimited by {}
, while collection initializers are delimited by []
. Furthermore, composite initializers can specify which member of the composite value should be initialized. Here are some examples of initializer expressions:
Initializers must always be assigned to an lvalue of a well defined type. This can either be a typed variable, component assignment, function parameter or in the case of nested initializers, an element of another initializer. For example, this is a valid usage of an initializer:
while this is an invalid usage of an initializer:
When assigning variables to elements in a composite initializer, applications can use the following shorthand notation if the variable names are the same as the member name of the element:
Flecs script supports interpolated strings, which are strings that can contain expressions. String interpolation supports two forms, where one allows for easy embedding of variables, whereas the other allows for embedding any kind of expression. The following example shows an embedded variable:
The following example shows how to use an expression:
To prevent evaluating expressions in an interpolated string, the $
and {
characters can be escaped:
The type of an expression is determined by the kind of expression, its operands and the context in which the expression is evaluated. The words "type" and "component" can be used interchangeably, as every type in Flecs is a component, and every component is a type. For component types to be used with scripts, they have to be described using the meta reflection addon.
The following sections go over the different kinds of expressions and how their types are derived.
Unary expressions have a single operand, with the operator preceding it. The following table shows the different unary operators with the expression type:
Operator | Expression Type |
---|---|
! | bool |
- | Same as operand. |
Binary expressions have two operands. The following table shows the different binary operators with the expression type. The operand type is the type to which the operands must be castable for it to be a valid expression.
Symbol | Expression type | Operand type |
---|---|---|
* | other (see below) | Numbers |
/ | f64 | Numbers |
+ | other (see below) | Numbers |
- | other (see below) | Numbers |
% | i64 | i64 |
<< | other (see below) | Integers |
>> | other (see below) | Integers |
> | bool | Numbers |
>= | bool | Numbers |
< | bool | Numbers |
<= | bool | Numbers |
== | bool | Values |
!= | bool | Values |
& | other (see below) | Integers |
\| | other (see below) | Integers |
&& | bool | bool |
\|\| | bool | bool |
For the operators where the expression type is listed as "other" the type is derived by going through these steps:
f64
i64
For equality expressions (using the ==
or !=
operators), additional rules are used:
2 == true
evaluate to true.Type expressiveness is determined by the kind of type and its storage size. The following tables show the expressiveness and storage scores:
Type | Expressiveness Score |
---|---|
bool | 1 |
char | 2 |
u8 | 2 |
u16 | 3 |
u32 | 4 |
uptr | 5 |
u64 | 6 |
i8 | 7 |
i16 | 8 |
i32 | 9 |
iptr | 10 |
i64 | 11 |
f32 | 12 |
f64 | 13 |
string | -1 |
entity | -1 |
Type | Storage Score |
---|---|
bool | 1 |
char | 1 |
u8 | 2 |
u16 | 3 |
u32 | 4 |
uptr | 6 |
u64 | 7 |
i8 | 1 |
i16 | 2 |
i32 | 3 |
iptr | 5 |
i64 | 6 |
f32 | 3 |
f64 | 4 |
string | -1 |
entity | -1 |
The function to determine whether a type is implicitly castable is:
If either the expressiveness or storage scores are negative, the operand types are not implicitly castable.
Lvalues are the left side of assignments. There are two kinds of assignments possible in Flecs script:
The type of an expression can be influenced by the type of the lvalue it is assigned to. For example, if the lvalue is a variable of type Position
, the assigned initializer will also be of type Position
:
Similarly, when an initializer is used inside of an initializer, it obtains the type of the initializer element. In the following example the outer initializer is of type Line
, while the inner initializers are of type Point
:
Another notable example where this matters is for enum and bitmask constants. Consider the following example:
Here, Red
is a resolvable identifier, even though the fully qualified identifier is Color.Red
. However, because the type of the lvalue is of enum type Color
, the expression Red
will be resolved in the scope of Color
.
Expressions can call functions. Functions in Flecs script can have arguments of any type, and must return a value. The following snippet shows examples of function calls:
Currently functions can only be defined outside of scripts by the Flecs Script API. Flecs comes with a set of builtin and math functions. Math functions are defined by the script math addon, which must be explicitly enabled by defining FLECS_SCRIPT_MATH
.
A function can be created in code by doing:
Methods are functions that are called on instances of the method's type. The first argument of a method is the instance on which the method is called. The following snippet shows examples of method calls:
Just like functions, methods can currently only be defined outside of scripts by using the Flecs Script API.
A method can be created in code by doing:
The following table lists builtin core functions in the flecs.script.core
namespace:
Function Name | Description | Return Type | Arguments |
---|---|---|---|
pair | Returns a pair identifier | id | (entity , entity ) |
The following table lists builtin methods on the flecs.meta.entity
type:
Method Name | Description | Return Type | Arguments |
---|---|---|---|
name | Returns entity name | string | () |
path | Returns entity path | string | () |
parent | Returns entity parent | entity | () |
has | Returns whether entity has component | bool | (id) |
The following table lists doc methods on the flecs.meta.entity
type:
Method Name | Description | Return Type | Arguments |
---|---|---|---|
doc_name | Returns entity doc name | string | () |
doc_uuid | Returns entity doc uuid | string | () |
doc_brief | Returns entity doc brief description | string | () |
doc_detail | Returns entity doc detailed description | string | () |
doc_link | Returns entity doc link | string | () |
doc_color | Returns entity doc color | string | () |
To use the doc functions, make sure to use a Flecs build compiled with FLECS_DOC
(enabled by default).
The following table lists math functions in the flecs.script.math
namespace:
Function Name | Description | Return Type | Arguments |
---|---|---|---|
cos | Compute cosine | f64 | (f64) |
sin | Compute sine | f64 | (f64) |
tan | Compute tangent | f64 | (f64) |
acos | Compute arc cosine | f64 | (f64) |
asin | Compute arc sine | f64 | (f64) |
atan | Compute arc tangent | f64 | (f64) |
atan2 | Compute arc tangent with two parameters | f64 | (f64, f64) |
cosh | Compute hyperbolic cosine | f64 | (f64) |
sinh | Compute hyperbolic sine | f64 | (f64) |
tanh | Compute hyperbolic tangent | f64 | (f64) |
acosh | Compute area hyperbolic cosine | f64 | (f64) |
asinh | Compute area hyperbolic sine | f64 | (f64) |
atanh | Compute area hyperbolic tangent | f64 | (f64) |
exp | Compute exponential function | f64 | (f64) |
ldexp | Generate value from significant and exponent | f64 | (f64, f32) |
log | Compute natural logarithm | f64 | (f64) |
log10 | Compute common logarithm | f64 | (f64) |
exp2 | Compute binary exponential function | f64 | (f64) |
log2 | Compute binary logarithm | f64 | (f64) |
pow | Raise to power | f64 | (f64, f64) |
sqrt | Compute square root | f64 | (f64) |
sqr | Compute square | f64 | (f64) |
ceil | Round up value | f64 | (f64) |
floor | Round down value | f64 | (f64) |
round | Round to nearest | f64 | (f64) |
abs | Compute absolute value | f64 | (f64) |
The following table lists the constants in the flecs.script.math
namespace:
Function Name | Description | Type | Value |
---|---|---|---|
E | Euler's number | f64 | 2.71828182845904523536028747135266250 |
PI | Ratio of circle circumference to diameter | f64 | 3.14159265358979323846264338327950288 |
To use the math functions, make sure to use a Flecs build compiled with the FLECS_SCRIPT_MATH
addon (disabled by default) and that the module is imported:
Templates are parameterized scripts that can be used to create procedural assets. Templates can be created with the template
keyword. Example:
The script contents of an template are not ran immediately. Instead they are ran whenever an template is instantiated. To instantiate an template, add it as a regular component to an entity:
Templates are commonly used in combination with the kind syntax:
Templates can be parameterized with properties. Properties are variables that are exposed as component members. To create a property, use the prop
keyword. Example:
Template scripts can do anything a regular script can do, including creating child entities. The following example shows how to create an template that uses a nested template to create children:
The module
statement puts all contents of a script in a module. Example:
The components.transform
entity will be created with the Module
tag.
The using
keyword imports a namespace into the current namespace. Example:
The using
keyword only applies to the scope in which it is specified. Example:
A using
statement may end with a wildcard (*
). This will import all namespaces matching the path. Example:
When you're building a scene or asset you may find yourself often repeating the same components for multiple entities. To avoid this, a with
statement can be used. For example:
This is equivalent to doing:
With statements can contain multiple tags:
With statements can contain component values, specified between parentheses:
Scripts can contain variables, which are useful for often repeated values. Variables are created with the const
keyword. Example:
Variables can be combined with expressions:
In the above examples, the type of the variable is inferred. Variables can also be provided with an explicit type:
Variables can be used in component values as shown in the previous examples, or can be used directly as component. Example:
Additionally, variables can also be used in combination with with
statements:
A script can use the value of a component that is looked up on a specific entity. The following example fetches the width
and depth
members from the Level
component, that is fetched from the Game
entity:
To reduce the number of component lookups in a script, the component value can be stored in a variable:
The requested component is stored by value, not by reference. Adding or removing components to the entity will not invalidate the component data. If the requested component does not exist on the entity, script execution will fail.
Parts of a script can be conditionally executed with an if statement. Example:
Parts of a script can be repeated with a for loop. Example:
The values specified in the range can be an expression:
When creating entities in a for loop, ensure that they are unique or the for loop will overwrite the same entity:
To avoid this, scripts can either create anonymous entities:
Or use a unique string expression for the entity name:
A scope can have a default component, which means entities in that scope can assign values of that component without having to specify the component name.
There are different ways to specify a default component. One way is to use a with
statement. Default component values are assigned with the =
operator, and don't need a {}
surrounding the value. Example:
Another way a default components are derived is from the entity kind. If an entity is specified with a kind, a DefaultChildComponent
component will be looked up on the kind to find the default component for the scope, if any. For example:
A common use of default components is when creating structs. struct
is a component with member
as default child component. Example:
Note how member
is also used as kind for the children. This means that children of x
and y
derive their default child component from member
, which is set to member
. This makes it easy to create nested members:
Multiple statements can be combined on a single line when using the semicolon operator. Example:
The comma operator can be used as a shortcut to create multiple entities in a scope. Example:
This allows for a more natural way to describe things like enum types:
This section goes over how to run scripts in an application.
To run a script once, use the ecs_script_run
function. Example:
Alternatively a script can be ran directly from a file:
If a script fails, the entities created by the script will not be automatically deleted. When a script contains templates, script resources will not get cleaned up until the entities associated with the templates are deleted.
A script can be ran multiple times by using the ecs_script_parse
and ecs_script_eval
functions. Example:
If a script fails, the entities created by the script will not be automatically deleted. When a script contains templates, script resources will not get cleaned up until the entities associated with the templates are deleted.
Managed scripts are scripts that are associated with an entity, and can be ran multiple times. Entities created by a managed script are tagged with the script. When script execution fails, the entities associated with the script will be deleted. Additionally, if after executing the script again an entity is no longer created by the script, it will also be deleted.
To run a managed script, do:
To update the code of a managed script, use the ecs_script_update
function:
When a script contains templates, script resources will not get cleaned up until the entities associated with the templates are deleted.