pub fn with_call_time_queries<R>(
lua: &Lua,
world: &World,
run: impl FnOnce() -> Result<R, LuaError>,
) -> Result<R, LuaError>Expand description
Install the call-time physics queries for the duration of one frame’s script run.
§Why a scope
Everything else in this API is a per-frame snapshot: Rust copies what it has into a table and Lua reads it. That works for “what collided this frame” and cannot work for a parameterised question — “what is the ground height at (x, z)” has no answer to precompute, because the engine does not know which (x, z) the script will ask about. Answering it means running a raycast while the script is calling, from a closure that holds the world.
The record said that was blocked: with mlua’s send feature, Lua::create_function demands
Fn(..) + Send + 'static, and &World is neither. That is true of Lua::create_function and
not of Scope::create_function, whose bound is F: Fn(..) + 'scope — no Send, no
'static. A scoped closure may borrow the world, and mlua invalidates it when the scope ends,
which is exactly the lifetime the borrow has.
So the frame looks like this: enter a scope, hand Lua a function that borrows the world, run the scripts, leave. Outside that window the function is gone rather than dangling — removed below, so a script that squirrelled the name away and called it later gets a plain “nil value” instead of an mlua error about a destructed callback.