-
I have been wondering about the most idiomatic way to store a script in memory and occasionally trigger it. I'm slightly confused by the documentation and have found myself creating this system to get started: callback_labels!(
Run => "run",
);
pub fn register_script(asset_server: Res<AssetServer>, mut scripts: ResMut<resources::MobileScripts>) {
let script = r#"
print("script successfully loaded");
function run()
print("it works!")
end
"#;
let script = script.as_bytes().to_owned().into_boxed_slice();
let script_id = 12345;
let path = AssetPath::parse(&format!("mem:://entity-name/entity-1234/{script_id}.lua"));
let asset = ScriptAsset {
content: script.clone(),
asset_path,
};
let handle: bevy::asset::Handle<ScriptAsset> = asset_server.add(asset);
scripts.inner.insert(script_id, handle.clone());
CreateOrUpdateScript::<LuaScriptingPlugin>::new(path.into(), script, None);
} I can't help but think that this is the wrong way to do it. Is there a simpler path? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, i believe adding the |
Beta Was this translation helpful? Give feedback.
-
That's useful information - thank you! |
Beta Was this translation helpful? Give feedback.
Hi, i believe adding the
ScriptAsset
to the asset server is enough, you dont need to manually send the create command, the asset server will trigger aLoaded
event which will in turn trigger the script loading pipeline.