Skip to main content

Crate suon_lua

Crate suon_lua 

Source
Expand description

Lua scripting for Bevy — attach scripts to entities, query the ECS from Lua, and fire triggers.

§Architecture

LuaPlugin
  ├── LuaRuntime (NonSend resource) — owns the mlua::Lua VM
  ├── ScriptRegistry (Resource)     — component/trigger accessors keyed by name
  └── world_cell                    — thread-local raw pointer bridging Rust ↔ Lua callbacks

§Quick start

use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use suon_lua::{LuaPlugin, LuaCommands, LuaScript};

#[derive(LuaComponent, Serialize, Deserialize)]
struct Health { value: i32 }

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, LuaPlugin))
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn((
                Health { value: 100 },
                LuaScript::new("function Entity:onTick()
                    local hp = self:get(Health)
                    hp.value = hp.value - 1
                end"),
            ));
        })
        .add_systems(Update, |mut commands: Commands, q: Query<Entity>| {
            for entity in &q { commands.lua_execute(format!("-- tick {}", entity.to_bits())); }
        })
        .run();
}

Re-exports§

pub use commands::Hook;
pub use commands::LuaCommands;
pub use commands::RunLuaHook;
pub use commands::RunLuaScript;
pub use lua_component::AppLuaExt;
pub use lua_component::LuaComponent;
pub use lua_component::WorldLuaComponentExt;
pub use runtime::ComponentAccessor;
pub use runtime::LuaRuntime;
pub use runtime::LuaScope;
pub use runtime::ScriptRegistry;
pub use runtime::TriggerAccessor;
pub use runtime::WorldLuaRuntimeExt;
pub use script::LuaScript;

Modules§

commands
Bevy Commands that queue Lua execution until the next command flush.
lua_component
Traits for exposing Bevy components to Lua scripts.
runtime
Core Lua runtime: LuaRuntime, LuaScope, ScriptRegistry, and accessors.
script
LuaScript component — stores Lua source attached to an entity.

Structs§

LuaPlugin
Bevy plugin that sets up the Lua scripting runtime.