-
For some reason, I can't seem to get a script's to be triggered in game. I'm also finding it impossible to identify how to fix it. The code below produces #[derive(Debug, Default, Resource)]
pub struct Scripts {
pub inner: HashMap<i32, Handle<ScriptAsset>>,
}
pub struct ScriptingPlugin;
impl Plugin for ProgPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
LuaScriptingPlugin::default(),
ScriptFunctionsPlugin
));
app.init_resource::<Scripts>();
app.add_event::<ScriptCallbackEvent>();
app.add_systems(Startup, load_scripts);
app.add_systems(FixedUpdate, (
trigger_scripts,
(event_handler::<Run, LuaScriptingPlugin>).after(trigger_scripts),
));
app.add_systems(bevy::app::FixedPostUpdate, debug_scripts);
register_functions_available_to_lua(app);
}
}
callback_labels!(
Run => "run",
);
pub fn trigger_scripts(mut events: EventWriter<ScriptCallbackEvent>) {
bevy::log::debug!("triggering scripts");
events.send(ScriptCallbackEvent::new_for_all(Run, vec![]));
}
pub fn load_scripts(asset_server: Res<AssetServer>, mut scripts: ResMut<resources::MobileScripts>) {
let script = r#"
print("hello from lua");
function run()
print("it works!")
end
"#;
let script = script.as_bytes().to_owned().into_boxed_slice();
let path = "memory:://entity-name/entity-1234/1.lua";
let asset = ScriptAsset {
content: script.clone(),
asset_path: AssetPath::parse(path),
};
let script_id = 12345;
let handle: bevy::asset::Handle<ScriptAsset> = asset_server.add(asset);
scripts.inner.insert(script_id, handle.clone());
CreateOrUpdateScript::<LuaScriptingPlugin>::new(path.into(), script, None);
}
pub fn debug_scripts(events: EventReader<ScriptCallbackEvent>, mut scripts: ResMut<resources::MobileScripts>) {
bevy::log::debug!("events missed: {}", events.len()); // Always > 0
bevy::log::debug!("scripts loaded: {scripts:#?}");
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, Quick thing first, I dont see you actually attaching the script to an entity or marking it as static, this would prevent the event handler from sending events to it. See https://makspll.github.io/bevy_mod_scripting/Summary/running-scripts.html#attaching-scripts. As for debugging, try running with |
Beta Was this translation helpful? Give feedback.
Hi,
Quick thing first, I dont see you actually attaching the script to an entity or marking it as static, this would prevent the event handler from sending events to it.
See https://makspll.github.io/bevy_mod_scripting/Summary/running-scripts.html#attaching-scripts.
As for debugging, try running with
bevy_mod_scripting_core=trace
logging, this should tell you what's happening under the hood