Code Monkey home page Code Monkey logo

bevy-cheatbook's Introduction

Unofficial Bevy Cheat Book

Click here to read the book!

Concise practical reference to the Bevy game engine.

Covers useful syntax, features, programming patterns, and solutions for common game development tasks.

Designed to be easy to read, straight to the point, using simple language to focus on the important information.

Support Me

If you like this book, please consider supporting me via GitHub Sponsors, Patreon, Bitcoin.

I'd like to keep improving and maintaining this book, to provide a high-quality independent learning resource for the Bevy community.

Your donation helps me work on such freely-available content. Thank you! โค๏ธ

License

All code in the book is provided under the MIT-0 License. At your option, you may also use it under the regular MIT License.

The text of the book is provided under the CC BY-NC-SA 4.0.

Exception: If used for the purpose of contribution to the "Official Bevy Project", the entire content of the book may be used under the MIT-0 License.

"Official Bevy Project" is defined as:

The MIT-0 license applies as soon as your contribution has been accepted upstream.

GitHub Forks and Pull Requests created for the purposes of contributing to the Official Bevy Project are given the following license exception: the Attribution requirements of CC BY-NC-SA 4.0 are waived for as long as the work is pending upstream review (Pull Request Open). If upstream rejects your contribution, you are given a period of 1 month to comply with the full terms of the CC BY-NC-SA 4.0 license or delete your work. If upstream accepts your contribution, the MIT-0 license applies.

Contributions

Please file GitHub Issues for any wrong/confusing/misleading information, as well as suggestions for new content you'd like to be added to the book.

Non-trivial Pull Requests are not welcome. File issues instead.

See the Contributing page for all the details.

bevy-cheatbook's People

Contributors

64kramsystem avatar billyb2 avatar chpio avatar djavorszky avatar dvogel avatar freder avatar grumpymetalguy avatar gzzo avatar h3fang avatar hymm avatar inodentry avatar jameshovet avatar jamiesprax avatar jbutterwick avatar johanhelsing avatar joshuataylor avatar mirenbhakta avatar mirkorainer avatar mo8it avatar niklasei avatar romainmazb avatar samwatts98 avatar skylerak avatar snorrwe avatar thebluefish avatar tshepang avatar wmbest2 avatar worbunkel avatar xentripetal avatar zebthewizard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bevy-cheatbook's Issues

Checking performance

It would be great if there were tips on measuring performance, to see where most time is spent.

  • Something like a flamegraph?
  • Measuring individual systems?
  • GPU vs CPU, where is the bottleneck?

Thanks for the great book!

UI chapter (please help if you have suggestions)

It would be nice to have some introductory section / tutorial on how to make UIs with bevy.

If anyone has any suggestions for what it should include or how it might be structured, please leave comments in this issue.

Performance pitfalls

It would be great to have a list of things that will wreck your performance, such that users can know patterns to avoid.

  • Example(s) of bad ordering of systems s.t. they depend on each other?
  • Systems all needing to use a single ResMut<T> (I haven't tried this but I guess it must be bad)

A section on things to avoid and perhaps things to do instead would be really great.

Saving and loading user settings

Show how to save/load settings (such as keybindings or graphics/window settings) to persist them across multiple runs of the game.

Currently, there is an implementation of something like this in Sotora. It uses a RON file, without any special I/O abstraction (which would be needed for WASM support, etc.).

There was some ongoing discussion about making a better i/o abstraction for save/load, to be integrated into bevy, but the cookbook should contain an example of something that works now in current Bevy.

pitfall: 2d camera and sprites not showing properly

Clarify that bevy's 2d camera bundle constructor creates a Transform that positions the camera at Z=999 (or something like that, close to the clipping plane). For 2d to behave as expected (Y axis up, looking towards -Z, background at Z=0, higher sprite layers at positive Z coorditnates), this must be preserved.

If users try to create their own transform for their 2d camera, and don't put it at this far-away Z coordinate, they might wonder why they can't see their sprites, or why sprites appear flipped, or why they need to use negative Z coordinates, etc...

Windowing (size, fullscreen, vsync, title, ...)

Add a cookbook page to show how to manage window parameters.

It should show both how to initialize them using the WindowDescriptor resource, as well as how to change them at runtime (for an existing window).

Advanced pattern: entities-as-events

I'm currently using this for extremely complex combat events in Fonts of Power.

The core idea here is that rather than emitting an event, you spawn an entity with a marker component, and then look for that in your "event-handling" systems. This has a few nice advantages:

  1. You can make lists of various "events" in a heterogenous fashion without needing to resort to trait objects. This is what forced me to switch in the first place.
  2. The "events" can be incredibly heterogenous, letting you add only the relevant fields to the specific event emitted.
  3. You can change the fields present on each event on the fly.
  4. You can perform complex operations with overlapping systems, touching only the events that have the right fields (in this case, components on your event-entity).
  5. No need for complex generics.

It also has a few critical disadvantages:

  1. Unlike true events, event-entities aren't spawned immediately.
  2. Ditto for adding and removing components.
  3. Much higher conceptual overhead in simple cases.
  4. Cleanup must be handled manually.

Improve change detection chapter clarity

Change detection should note that it checks if X has occurred since the last time the system ran, no matter how long it's been since then*.

  • for any sane use case at all.

Advanced patterns: Better info about Generic Systems

Talk about other uses of Generic Systems.

In combination with Traits?

In combination with Const Generics?


Please share information you know about use cases we should cover, if you are knowledgeable about this.

Debugging failed IntoSystem macro

Stopgap solution for: bevyengine/bevy#1519

IMO, the correct choice here is to have a page for "I can't add my function as a system!" or something of the like. It should explain each of the options that impl SystemParam, and call out common mistakes for each. As an example, missing &mut for Commands, missing & for components etc.

Add Scenes

Especially when it comes to making multiple levels.

Fixed Timestep

Cover the fixed timestep runcriteria / how to make systems run on a fixed timestep.

Page about Transforms

Make a page that addresses the common beginner questions about bevy's transforms, such as the orientation of the coordinate system for 2D and 3D, global/local transforms, and the math apis.

POLL: Do people consider the one-page Cheatsheet useful?

EDIT: closing this. Now is not the time for it. I will do this again later in the 0.5->0.6 bevy development cycle, when people have some experience with the new cheatbook revision #33

The Cheatsheet is a page that takes considerable effort to maintain. I am happy to continue maintaining it, if I know that people actually find it useful. Otherwise, I would like to remove it from the book.

The information on that page is redundant; everything on it is also covered elsewhere in the book, with prose and explanations. The goal of the cheatsheet was to be a convenience -- an ultra-concise single-page index, for people who already understand the concepts.

I need community feedback to decide on this. Please vote (using reacts) on this issue:

  • ๐Ÿ‘ if you would like it to be kept in the book
  • ๐Ÿ‘Ž if you wouldn't mind it being removed from the book

This poll is active until it is time to update the book for Bevy 0.6 / the next release of Bevy. I will then make a judgement whether to update the Cheatsheet for the new version or remove it. It will stay in the book until then.

Order of execution (both v0.4 and v0.5); system labels and dependencies (v0.5)

  1. Expand the Systems page in Bevy Basics to talk about parallelism/performance and order of execution.

If we do this before the 0.5 release, it should contain two sections, one for 0.4 and one for bevy git.

  1. Show the new .before()/.after() and .label() syntax.

This should be done in both the Cheatsheet and in the Systems page in Bevy Basics.

Instructions on setting up dev environment

The cheatbook should contain advice on how to set up debugging for various common IDEs, and highlight some common pitfalls when using a debugger on a game.

Here's a template that works for bevy development in VSCode

{
    "type": "lldb",
    "request": "launch",
    "name": "Debug example 'breakout'",
    "cargo": {
        "args": [
            "build",
            "--example=breakout",
            "--package=bevy"
        ],
        "filter": {
            "name": "breakout",
            "kind": "example"
        }
    },
    "args": [],
    "cwd": "${workspaceFolder}",
    "env": {
        "CARGO_MANIFEST_DIR": "${workspaceFolder}",
    }
}

Cookbook: GUI Button Handling

Add a cookbook page for handling multiple buttons across a complex UI.

The pattern to showcase should probably be based on a button identifier with system chains, as used in Sotora.

Page about working with GLTF in Bevy

Add a section describing how to work with asset labels to address sub-assets in GLTF files.

It should cover how to get individual meshes, scenes, textures, etc. that were loaded from a GLTF file.

It should cover both bevy-generated labels (Scene0, Primitive0, ...) and custom labels (when a GLTF file was exported with labels included in the file).

Advanced Pattern: avoiding frame delays / adding systems to bevy's PreUpdate/PostUpdate

Add a page to talk about how to avoid introducing 1-frame delays into your game / how to handle things with minimal latency.

Talk about why you might want to add systems to bevy's PreUpdate and PostUpdate stages.

  1. Spawning entities in PreUpdate, so Commands can be processed before the user's main update systems run, allowing them to work on the new entities.
  2. Cleanup in PostUpdate? (is this a good idea? discuss?)
  3. For 0.5: Bevy labels to be aware of (if you add a system to PostUpdate, maybe you want it to be before/after the transform propagate system? other important systems?)
  4. Input->Update->Output pipeline; read things in during PreUpdate (such as reading a network socket), process things in Update, write things out during PostUpdate (such as writing a network socket). With a network socket, this allows multiplayer updates to be handled in a single frame.
  5. Others??

Advanced pattern: overriding behavior

Motivating Example

(from Discord
I have a crit_system that processes crits, causing them to deal double damage. If my actor has the TripleCrits component, instead their crits do triple damage instead. This is handled in triple_crit_system. There are many possible ways in which the crit logic could be varied (e.g. a Keen component), and I'd like people to be able to add their own without needing to touch already written code.

The three obvious ways to do this are:

  1. Expand the crit_system to handle every case.
  2. Use Without<TripleCrits> in crit_system
  3. Overwrite the damage calculation performed in crit_system in triple_crit_system

There are two less obvious ways to do this:
4. Use With<DefaultCrits> in crit_system
5. Use With<{CritType::Default}> in crit_system

4 and 5 (if it works?) are the best choices here, but it seems a bit tricky to remember to always add the DefaultX components

When to Use This Pattern

  1. You want to override the behavior of a system for some entities.
  2. You need to override it in complex and varied ways, especially if this isn't known in advance (e.g. in mods).
  3. You don't want to bloat the behavior or complexity of your default system.
  4. The default behavior is expensive or has annoying side effects, making overwriting its final results painful.

When Not to Use This Pattern

  1. Your behavior variants are simple or few. Use an enum component instead.
  2. You always want to perform some of the default behavior. Split your systems, and use the intermediate result.

How to Use This Pattern

  1. Add the DefaultX component to every entity that should be processed by your default system.
  2. Add With<DefaultX> to your query in your default system.
  3. Add the DifferentX component to every entity whose behavior should be overridden in this way.
  4. Add With<DifferentX> component to your query in the overriding system.

Notes

Once archetype invariants land, they will be extremely useful to ensure correctness.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.