Triggering events from game loops #11156
Replies: 3 comments 4 replies
-
Going to tag @alice-i-cecile since she's been helping me on topics related to ECS :) |
Beta Was this translation helpful? Give feedback.
-
So, you're ultimately trying to model a state machine here. You want to be in either Like you said, you could use states here, but they're a bit heavy, and don't take effect immediately. There are three patterns that I tend to use for this:
|
Beta Was this translation helpful? Give feedback.
-
You need a "state machine" when you're going from something non event based (checking that an asset is loaded) to event based (sending an event). In the specific case of the code example you gave, Bevy is already sending an event for you ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a topic worthy of discussion because it's occurred to me over and over again, and I think plenty of others have ran into it as well. By all means, it's an opinionated topic and have multiple solutions but I'm curious as to what people think the best practice is.
A common scenario in any game is to trigger a one time "event". E.g. timer timed out, some progress is completed, or you want to transition to another state. AFAIK, to goto solutions include sending a Bevy Event, triggering a one shot system, or transitioning to another state. In all cases the scenario involves a one-time action that should not be repeated for frames afterwards.
However, the systems for which these events are triggered from are often repeated every frame. Consider the following system where you check whether if an asset is done loading, and send out an event once loading completes:
This works fine until you realize that there's no mechanism stopping it from repeatedly sending the same
AssetLoadingDoneEvent
. so there's a few solutions that come to mind:custom_asset.is_some()
evaluates tofalse
. There are cases for which this approach works, but in this example, removing the loaded asset defeats the original purpose.So I'm curious what the community thinks. Surely, this is a common thing everybody runs into, but AFAIK there's no canonical solution to. Each one of the three approaches above have its draw backs, and I don't know what the answer is.
Beta Was this translation helpful? Give feedback.
All reactions