-
Hi! I am brand new to Bevy, and fairly new to Rust (I mean, I've been trying to learn the language on and off for several years, but still consider myself a beginner). Anyway, I've been working through Qongzi's Minesweeper tutorial (https://dev.to/qongzi/bevy-minesweeper-part-8-4apn), and I've hit a roadblock at the end of Chapter 9, where there are a couple of exercises in working with states. So, I have a BoardPlugin containing code that looks like this:
That's pretty standard stuff, I guess. I have done a fair amount of GUI programming, so I'm familiar with event handlers and callbacks, and this appears to me like registering callbacks for some predefined event handlers (feel free to correct me if I'm thinking about this in the wrong way). So, assuming that is the case, I gather that there are internal events called But in the main module for the app I have this:
So, this is also clear: pressing the C or G key triggers a state change. But what has me utterly baffled is this: how does the Bevy engine know that changing the state from UPDATE:
... and the app still runs just fine. So it would appear that the specific values of the enum are completely arbitrary. So I'm ... not any less confused. What is it, exactly, that connects the state changes to the code that invokes the systems in the BoardPlugin? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
By calling
This state will now be saved in a resource You can call your State whatever you want and can have any number of values for it. You can even have multiple States with different types. You tell the engine (in your case via When changing a State: the engine will run all |
Beta Was this translation helpful? Give feedback.
By calling
add_state
on your Bevy App you are doing two things:AppState
) that is supposed to function as a StateThis state will now be saved in a resource
State<your type>
which you can request in systems (what you called callbacks) to read/change.You can call your State whatever you want and can have any number of values for it. You can even have multiple States with different types.
You tell the engine (in your case via
state.set
) to which value of a certain state you want to switch. This means that Bevy knows the next state and the previous one.When changing a State: the engine will run all
on_exit
systems …