bevy_mod_scripting icon indicating copy to clipboard operation
bevy_mod_scripting copied to clipboard

Multiple Argument Hooks

Open christianlarrabure opened this issue 1 year ago • 1 comments

Hey, I want to have some events that have no arguments and others that have other arguments. Right now my approach is this: `rust

    app.add_plugins(ScriptingPlugin)
        .add_systems(Update, run_scripts)
        .add_systems(Update, (do_update, load_scripts, forward_errors, on_player_connected))
        .add_event::<ExecuteScriptDto>()
        .add_script_handler::<LuaScriptHost<()>, 0, 0>(PostUpdate)
        .add_script_handler::<LuaScriptHost<LuaEntity>, 0, 0>(PostUpdate)
        .add_script_host::<LuaScriptHost<()>>(PostUpdate)
        .add_script_host::<LuaScriptHost<LuaEntity>>(PostUpdate)
        .add_api_provider::<LuaScriptHost<LuaEntity>>(Box::new(UntoldDawnAPI))
        .add_api_provider::<LuaScriptHost<LuaEntity>>(Box::new(LuaBevyAPIProvider))
        .add_api_provider::<LuaScriptHost<LuaEntity>>(Box::new(LuaCoreBevyAPIProvider))
        .add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
        .add_api_provider::<LuaScriptHost<()>>(Box::new(LuaCoreBevyAPIProvider))
        .add_api_provider::<LuaScriptHost<()>>(Box::new(UntoldDawnAPI));`

And then some events are:

rust fn do_update(mut lua_events: PriorityEventWriter<LuaEvent<()>>) { lua_events.send( LuaEvent { hook_name: "on_update".to_owned(), args: (), recipients: Recipients::All, }, 0, ) }

And others are:

rust fn on_player_connected( query: Query<Entity, Added<PlayerConnection>>, mut lua_events: PriorityEventWriter<LuaEvent<LuaEntity>>, ) { for item in query.iter() { lua_events.send( LuaEvent { hook_name: "internal_onPlayerConnected".to_owned(), args: LuaEntity::new(item), recipients: Recipients::All, }, 0, ) } }

However, this doesn't seem to work? Any ideas?

christianlarrabure avatar Sep 11 '24 07:09 christianlarrabure

Hey! It looks like you're using multiple separate script hosts, which should work but is not really how the library was intended to be used. Because the lua script host supports anything that converts into lua via IntoLuaMulti you should be able to have a single enum type on which you implement IntoLuaMulti yourself to get this behavior

makspll avatar Sep 11 '24 21:09 makspll

Closing due to inactivity

makspll avatar Feb 01 '25 19:02 makspll