Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::script Namespace Reference

Entity behaviors in Lua, so that new enemies and patterns need no recompilation. More...

Namespaces

namespace  detail
 Implementation of bind_components(), compiled in the caller's code.
 

Classes

class  EventBridge
 Sends the gameplay events of a bus to the scripts of a state, while it lives. More...
 
struct  EventField
 A field of an event sent to scripts. More...
 
struct  ScriptError
 Where and why a script failed. More...
 
class  ScriptState
 A Lua state for the game's scripts, sandboxed. More...
 

Functions

void bind_components (ScriptState &lua, ecs::Registry &registry)
 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.
 

Detailed Description

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.

Function Documentation

◆ bind_components()

void aegis2d::script::bind_components ( ScriptState &  lua,
ecs::Registry &  registry 
)
inline

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.

script::bind_components(lua, registry);
lua.run(R"(
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 &registry)
Gives the scripts of a state the common components of a registry.
Definition components.hpp:278
Parameters
luaState to give the components to.
registryRegistry of the entities, kept by reference.

◆ component_fields()

std::span< const std::string_view > aegis2d::script::component_fields ( std::string_view  component)
inlinenoexcept

Fields of a component, as scripts name them.

Parameters
componentName of a component.
Returns
Such as x and y for "Transform"; empty for an unknown component.

◆ component_names()

std::span< const std::string_view > aegis2d::script::component_names ( )
inlinenoexcept

Names of the components scripts can use.

Returns
"Transform", "Velocity", "Sprite", "Animation", "Collider", "Health", "Damage", "Score" and "SineMotion".

◆ emit()

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
luaState of the scripts.
nameName of the event, such as "WaveCleared".
fieldsIts 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).

◆ lua_version()

AEGIS2D_SCRIPT_EXPORT std::string aegis2d::script::lua_version ( )

Version of the Lua the module runs, as Lua itself names it.

Read from _VERSION in a new Lua state, through sol2.

Returns
Such as "Lua 5.4".

◆ name()

AEGIS2D_SCRIPT_EXPORT std::string_view aegis2d::script::name ( )
noexcept

Name of the module, used to check that it is linked.

Returns
"script", a view of a static string.

◆ to_string()

AEGIS2D_SCRIPT_EXPORT std::string aegis2d::script::to_string ( const ScriptError &  error)

Formats an error as a log line shows it.

Parameters
errorError of a script.
Returns
"chunk:line: message", or "chunk: message" without a line.