pub trait ScriptHost: Send + Sync + 'static + Default + Resource {
    type ScriptContext: Send + Sync + 'static;
    type ScriptEvent: ScriptEvent;
    type ScriptAsset: CodeAsset;
    type APITarget: Send + Sync + 'static;
    type DocTarget: DocFragment;

    // Required methods
    fn load_script(
        &mut self,
        script: &[u8],
        script_data: &ScriptData<'_>,
        providers: &mut APIProviders<Self>
    ) -> Result<Self::ScriptContext, ScriptError>;
    fn setup_script(
        &mut self,
        script_data: &ScriptData<'_>,
        ctx: &mut Self::ScriptContext,
        providers: &mut APIProviders<Self>
    ) -> Result<(), ScriptError>;
    fn handle_events<'a>(
        &self,
        world_ptr: &mut World,
        events: &[Self::ScriptEvent],
        ctxs: impl Iterator<Item = (ScriptData<'a>, &'a mut Self::ScriptContext)>,
        providers: &mut APIProviders<Self>
    );
    fn register_with_app_in_set(app: &mut App, set: impl FreeSystemSet);
    fn register_with_app_in_base_set(app: &mut App, set: impl BaseSystemSet);

    // Provided method
    fn run_one_shot(
        &mut self,
        script: &[u8],
        script_name: &str,
        entity: Entity,
        world: &mut World,
        event: Self::ScriptEvent
    ) -> Result<(), ScriptError> { ... }
}
Expand description

A script host is the interface between your rust application and the scripts in some interpreted language.

Required Associated Types§

source

type ScriptContext: Send + Sync + 'static

the type of the persistent script context, representing the execution context of the script

source

type ScriptEvent: ScriptEvent

the type of events picked up by lua callbacks

source

type ScriptAsset: CodeAsset

the type of asset representing the script files for this host

source

type APITarget: Send + Sync + 'static

the type representing the target of api providers, i.e. the script engine or the script context itself

source

type DocTarget: DocFragment

the type of each doc fragment

Required Methods§

source

fn load_script( &mut self, script: &[u8], script_data: &ScriptData<'_>, providers: &mut APIProviders<Self> ) -> Result<Self::ScriptContext, ScriptError>

Loads a script in byte array format, the script name can be used to send useful errors.

source

fn setup_script( &mut self, script_data: &ScriptData<'_>, ctx: &mut Self::ScriptContext, providers: &mut APIProviders<Self> ) -> Result<(), ScriptError>

Perform one-off initialization of scripts (happens for every new or re-loaded script)

source

fn handle_events<'a>( &self, world_ptr: &mut World, events: &[Self::ScriptEvent], ctxs: impl Iterator<Item = (ScriptData<'a>, &'a mut Self::ScriptContext)>, providers: &mut APIProviders<Self> )

the main point of contact with the bevy world. Scripts are called with appropriate events in the event order

source

fn register_with_app_in_set(app: &mut App, set: impl FreeSystemSet)

Registers the script host with the given app, and attaches handlers to deal with spawning/removing scripts in the given System Set.

Ideally place after any game logic which can spawn/remove/modify scripts to avoid frame lag. (typically CoreSet::Post_Update)

source

fn register_with_app_in_base_set(app: &mut App, set: impl BaseSystemSet)

Registers the script host with the given app, and attaches handlers to deal with spawning/removing scripts in the given Base System Set.

Ideally place after any game logic which can spawn/remove/modify scripts to avoid frame lag. (typically CoreSet::Post_Update)

Provided Methods§

source

fn run_one_shot( &mut self, script: &[u8], script_name: &str, entity: Entity, world: &mut World, event: Self::ScriptEvent ) -> Result<(), ScriptError>

Loads and runs script instantaneously without storing any script data into the world. The script id is set to u32::MAX.

Implementors§