pub trait State<E = TetraError> {
// Provided methods
fn update(&mut self, ctx: &mut Context) -> Result<(), E> { ... }
fn draw(&mut self, ctx: &mut Context) -> Result<(), E> { ... }
fn event(&mut self, ctx: &mut Context, event: Event) -> Result<(), E> { ... }
}
Expand description
Implemented by types that contain game state and provide logic for updating it and drawing it to the screen.
Error Handling
The methods on State
allow you to return a Result
, either explicitly or via the ?
operator. If an error is returned, the game will close and the error will be returned from
the Context::run
call that was used to start it. This allows you to propagate errors
back to main
for reporting/logging.
The error type defaults to TetraError
, but this can be overridden by adding a type parameter
to your State
implementation (e.g. State<MyError>
).
Examples
The hello_world
example
demonstrates a minimal implementation of the State
trait.
The error_handling
example demonstrates how custom error types can be used to implement more robust error handling.
Provided Methods§
sourcefn update(&mut self, ctx: &mut Context) -> Result<(), E>
fn update(&mut self, ctx: &mut Context) -> Result<(), E>
Called when it is time for the game to update.