Skip to content

Resource-like scripts #249

Closed
@makspll

Description

@makspll

I.e. scripts which do get attached to entities, but simply always get executed on each callback regardless of entity.

Should be easier to add, perhaps with a command, AddStaticScript which will add the script ID to a collection of scripts to be considered at each event handler

Activity

changed the title [-]`resource` scripts[/-] [+]`Resource`-like scripts[/+] on Feb 5, 2025
Joakker

Joakker commented on Feb 5, 2025

@Joakker
Contributor

Here's how I'd do it

/// Create a new resource that holds the static scripts' ids
#[derive(Resource, Default, Deref, DerefMut)]
pub struct StaticScripts(Vec<ScriptId>);

/// The base ScriptingPlugin adds the resource
/// That way we don't need to repeat it for every language we want to impl
/// scripting for
impl Plugin for ScriptingPlugin {
    fn build(&self, app: &mut App) {
        app
            .init_resource::<StaticScripts>()
            ...
            ;
    }
}

/// A utility trait
pub trait AddStaticScript {
    fn add_static_script(&mut self, path: &str);
}

impl AddStaticScript for World {
    fn add_static_script(&mut self, path: &str) {
        self.resource_scope(|_, mut static: Mut<StaticScripts>| static.push(path.into())) ;
    }
}

/// Same for App, which fetches the world and calls the above method, and make a custom command that does the same in the apply method
moved this from Planned to Done in BMS Roadmapon Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Participants

    @makspll@Joakker

    Issue actions

      `Resource`-like scripts · Issue #249 · makspll/bevy_mod_scripting