Skip to content

Resource-like scripts #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #150
makspll opened this issue Feb 5, 2025 · 1 comment · Fixed by #253
Closed
Tracked by #150

Resource-like scripts #249

makspll opened this issue Feb 5, 2025 · 1 comment · Fixed by #253

Comments

@makspll
Copy link
Owner

makspll commented Feb 5, 2025

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

@github-project-automation github-project-automation bot moved this to Planned in BMS Roadmap Feb 5, 2025
@makspll makspll changed the title resource scripts Resource-like scripts Feb 5, 2025
@Joakker
Copy link
Contributor

Joakker commented Feb 5, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
2 participants