Skip to content
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

Skip systems that require a non-existing resource instead of panicking. #1604

Closed
Victov opened this issue Mar 9, 2021 · 4 comments
Closed
Labels
A-ECS Entities, components, systems, and events C-Docs An addition or correction to our documentation C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@Victov
Copy link

Victov commented Mar 9, 2021

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

Right now (release 0.4.0), when a system is registered that requires are resource. and that resource is not yet inserted, then you get a panic. I propose that the scheduler would simply skip systems whose resource needs are unfulfilled instead of panicking.
This would make it easy to keep systems registered, even though they might occasionally not be useful. For example:

I have a permanently registered network polling system, which requires a Socket resource.
In the main menu the user must select Join or Host, and after this choice a Socket is added as resource.

What solution would you like?

Let the scheduler skip systems whose resource needs are not met.

What alternative(s) have you considered?

Only run the required systems in specific app states where you know that the resource will be available. However, this is not ideal, since the app states are app-specific, which would disallow me to bundle my networking logic in a separate module or crate and make a bevy Plugin out of it.

Additional context

I was expecting this behaviour since I wanted to make something similar to amethyst_networking for bevy. In amethyst_networking you simple include a NetworkBundle which registers all sorts of polling systems for you. They automatically kick into action when you insert the resource for the socket manually.

I saw that in 0.5 a rewrite of the resources is coming, so excuse me if this issue is already solved for 0.5!
Really enjoying Bevy so far. Thanks a lot :)

@DJMcNab
Copy link
Member

DJMcNab commented Mar 9, 2021

In this case, you should use the API from #1494

That is, use the Option<Res<T>> system parameter.

@jakobhellermann
Copy link
Contributor

Since #1494 you can write

fn system(resource: Option<Res<Resource>>) {
  let resource = match resource {
    Some(res) => res,
    None => return,
  };

  // continue
}

which solves your problem without silently failing in case you forget to insert a resource.

@Victov
Copy link
Author

Victov commented Mar 9, 2021

Ah that's great! Thanks.

Just out of curiosity, how could I have known this? I believe I checked the examples and learning material quite thoroughly, so I'm surprised now that this feature exists.

Edit: I just noticed that this feature is mega super fresh. Not-even-in-master fresh. Which definitely explains why it's not mentioned in any learning material or examples.

@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Mar 9, 2021
@alice-i-cecile
Copy link
Member

@Victov the PR was merged 18 hours ago, so it's very understandable that you didn't see this. It does need better documentation though.

In the meantime, the Discord is very active and has a ton of informal development activity and help. Issues like this are very helpful, but you can also try asking on Discord and we can point you to solutions and ongoing development work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Docs An addition or correction to our documentation C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

No branches or pull requests

5 participants