#error #bevy #result #gamedev #bevy-ui #game

bevy_anyhow_alert

使用Bevy轻松处理系统错误;让您的系统返回Result

3个版本 (破坏性更新)

0.3.0 2024年7月15日
0.2.0 2024年7月2日
0.1.0 2024年7月1日

#1298 in 游戏开发

Download history 327/week @ 2024-07-01 122/week @ 2024-07-15 1/week @ 2024-07-22 18/week @ 2024-07-29

每月157次下载

MIT/Apache

300KB
119

bevy_anyhow_alert

Crates.io Docs

此crate提供了一种扩展trait,用于帮助在Bevy中进行系统和应用级别的错误管理。使用bevy_ui_mod_alerts的re-export进行轻度配置。主要好处:您的系统可以返回Result(甚至Result<T, Vec<E>>)!

A video snippet of the "toasts" example, where some animated toasts spawn in the bottom right corner.

如何使用

编写您的系统时,返回两种接受类型之一

  • Result<T, E>
  • bevy_anyhow_alert::ResultVec<T, E>:是Result<T, Vec<Error>>的别名

然后调用my_system.anyhow_alert()my_system.anyhow_alerts!当结果为Err时,您将看到Toast UI元素出现(假设有一个相机)。

let mut app = App::new();
// ...
app.add_system(fire_error.anyhow_alert());
// ..
app.run();

您可以在整个应用程序中定义任何类型的错误。它们必须实现DebugDisplay,如果您在错误类型上派生thiserror::Error,则尤其容易。

#[derive(Debug, Error)]
#[error("testing!")]
pub struct MyError;

fn fire_error(inputs: Res<ButtonInput<KeyCode>>) -> anyhow::Result<()> {
    if inputs.just_pressed(KeyCode::Space) {
        Err(anyhow::Error::new(MyError))
    } else {
        Ok(())
    }
}

依赖关系

~38–74MB
~1.5M SLoC