Bevy 笔记 ongoing
在开发阶段快速编译
WARNING
从 bevy 0.13 版本起, dynamic_linking 存在问题, 因此 不建议 使用此 feature.
bevy 提供了名为 dynamic_linking 的 feature, 可以大幅降低编译时间.
WARNING
不应该在 release 模式编译时启用此 dynamic_linking.
sh
cargo run --features bevy/dynamic_linking建议在 VSCode 中可以创建 .vscode/tasks.json 文件, 并把以上命令包装成一个 task.
之后就可以使用 Ctrl+Shift+P 输入命令 Tasks: Run Task 然后选择这个 task 执行.
json
{
"version": "2.0.0",
"tasks": [
{
"label": "quick run",
"type": "shell",
"command": "cargo run --features bevy/dynamic_linking"
}
]
}TIP
也可以借助 VSCode Task 来快速执行 ./examples 或 ./tests 路径下的代码.
毕竟在 VSCode 中输入 task 的名称时会实时搜索, 可以避免手敲命令时偶尔输入错误的问题.
检测 system 是否合法
在编写注释中的代码示例时, 建议使用:
rs
bevy::ecs::system::assert_is_system(my_system);将 system 的返回值打印出来
可以借助 bevy 封装的实用工具以及 IntoSystem 相关的函数来打印 system 的返回值.
bevy_utils/src/lib.rs
rs
// ...
/// Calls the [`tracing::info!`] macro on a value.
pub fn info<T: Debug>(data: T) {
tracing::info!("{:?}", data);
}
/// Calls the [`tracing::debug!`] macro on a value.
pub fn dbg<T: Debug>(data: T) {
tracing::debug!("{:?}", data);
}
/// Processes a [`Result`] by calling the [`tracing::warn!`] macro in case of an [`Err`] value.
pub fn warn<E: Debug>(result: Result<(), E>) {
if let Err(warn) = result {
tracing::warn!("{:?}", warn);
}
}
/// Processes a [`Result`] by calling the [`tracing::error!`] macro in case of an [`Err`] value.
pub fn error<E: Debug>(result: Result<(), E>) {
if let Err(error) = result {
tracing::error!("{:?}", error);
}
}
// ...rs
use bevy::log::{Level, LogPlugin};
use bevy::prelude::*;
use bevy::utils::{dbg, error, info, warn};
fn main() {
App::new()
.add_plugins(LogPlugin {
level: Level::DEBUG,
..Default::default()
})
.add_systems(Update, data_output.map(dbg))
.add_systems(Update, data_output.map(info))
.add_systems(Update, warning_output.map(warn))
.add_systems(Update, warning_output.map(error))
.run();
}
fn data_output() -> String {
"date".to_owned()
}
fn warning_output() -> Result<(), String> {
Err("error".to_owned())
}