bevyengine / bevyengine/bevy

Allow Option params in systems

Open
#7,516 15 comments 4 reactions 0 assignees View on GitHub
A-ECS C-Docs C-Feature S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
4d 8m
Merged PRs (30d)
147

Description

## What problem does this solve or what need does it fill?

I often create functions that get called once as startup systems that I later on may want to reuse somewhere else in my code. For example, a `spawn_weapon` function that I might want to use at the start of the game, but obviously may end up wanting to use later in the game as well.

## What solution would you like?

I want to have the option to place an Option type within a system. By default, the Bevy ECS would place a "None" value into the parameter, allowing me to have the option to provide default functionality.

Later on I can use that function but place "Some" instead to have custom functionality.

So, for example:

```rust
use bevy::prelude::*;

// ...

fn spawn_weapon(
mut commands: Commands,
rooms: Res,
spawn_location: Option,
) {
let transform = match spawn_location {
Some(location) => transform.from_translation(location),
None => rooms.current().default_weapon_spawn_location(),
}

commands.spawn(WeaponBundle {
transform,
..Default::default()
});
}
```

## What alternative(s) have you considered?

The most straightforward way to do this currently is to create a utility function (`spawn_weapon`) and then the actual startup system (`spawn_weapon_on_startup`).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.