Entity behaviors in Lua, so that new enemies and patterns need no recompilation.
More...
|
| void | bind_components (ScriptState &lua, ecs::Registry ®istry) |
| | Gives the scripts of a state the common components of a registry.
|
| |
| std::span< const std::string_view > | component_names () noexcept |
| | Names of the components scripts can use.
|
| |
| std::span< const std::string_view > | component_fields (std::string_view component) noexcept |
| | Fields of a component, as scripts name them.
|
| |
| AEGIS2D_SCRIPT_EXPORT bool | emit (ScriptState &lua, std::string_view name, std::span< const EventField > fields={}) |
| | Sends an event to the scripts of a state.
|
| |
| AEGIS2D_SCRIPT_EXPORT std::string_view | name () noexcept |
| | Name of the module, used to check that it is linked.
|
| |
| AEGIS2D_SCRIPT_EXPORT std::string | lua_version () |
| | Version of the Lua the module runs, as Lua itself names it.
|
| |
| AEGIS2D_SCRIPT_EXPORT std::string | to_string (const ScriptError &error) |
| | Formats an error as a log line shows it.
|
| |
Entity behaviors in Lua, so that new enemies and patterns need no recompilation.
Built on servers and clients alike, the behaviors running where the game is simulated. Lua and sol2 are private to the module: no Lua header reaches the games.
Gives the scripts of a state the common components of a registry.
Adds a global table ecs to the state. Entities are numbers (ecs::Entity::id()), and components are named by their type:
ecs.get(entity, "Transform") gives the entity's component, or nil;
ecs.add(entity, "Velocity", {x = -120}) gives it one, its fields set from the table (the others at their defaults), and returns it; an existing one is kept and updated;
ecs.has(entity, name), ecs.remove(entity, name), ecs.alive(entity);
ecs.entities("Transform", "Velocity") lists the entities that have them all.
A component is read and written by field: transform.x = transform.x + 1. Vectors are flattened (Transform.x, Sprite.width, Sprite.r), each field of a component is in component_fields(). A component held by a script is found again at each access, so it never points to freed memory: once its entity or the component is gone, reading or writing it is an error of the script, as is a field that does not exist or a value of the wrong type. The registry must outlive the state's use of ecs.
Inline on purpose: the registry finds its pools by typeid, which differs between the engine's libraries and the game with libc++, so the code that reaches the registry is compiled where this is called.
function update(dt)
for _, entity in ipairs(ecs.entities("Transform", "Velocity")) do
local transform, velocity = ecs.get(entity, "Transform"), ecs.get(entity,
"Velocity") transform.x = transform.x + velocity.x * dt
end
end
)");
lua.call("update", 1.0 / 60.0);
A Lua state for the game's scripts, sandboxed.
Definition state.hpp:52
bool call(std::string_view function)
Calls a global function without arguments.
bool run(std::string_view code, std::string_view chunk="script")
Runs Lua code.
void bind_components(ScriptState &lua, ecs::Registry ®istry)
Gives the scripts of a state the common components of a registry.
Definition components.hpp:278
- Parameters
-
| lua | State to give the components to. |
| registry | Registry of the entities, kept by reference. |
| AEGIS2D_SCRIPT_EXPORT bool aegis2d::script::emit |
( |
ScriptState & |
lua, |
|
|
std::string_view |
name, |
|
|
std::span< const EventField > |
fields = {} |
|
) |
| |
Sends an event to the scripts of a state.
Every listener a script added with events.on(name, function(event) ... end) is called, in the order they were added, with a table of the fields. It runs as a script: under the state's budgets, and an error, logged with its line, stops neither the game nor the other listeners. Scripts send events to each other with events.emit(name, {...}) and stop listening with events.off(id).
- Parameters
-
| lua | State of the scripts. |
| name | Name of the event, such as "WaveCleared". |
| fields | Its fields. |
- Returns
true if every listener ran to its end (or there is none); false if one failed, or if a script of the state is running (an event is not sent from inside one).