makspll / bevy_mod_scripting

Bevy Scripting Plugin
Apache License 2.0
402 stars 31 forks source link

Multiple Argument Hooks #129

Open christianlarrabure opened 1 month ago

christianlarrabure commented 1 month ago

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?

makspll commented 1 month ago

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