⚠️ Beta State

PyBevy is in an early and experimental stage. The API is incomplete, subject to breaking changes without notice, and you should expect bugs. Many features are still under development.

Change Detection

Detect when components or resources have been modified.

Introduction

PyBevy tracks when components and resources are modified, using Bevy's built-in change detection. This lets you write systems that only act when data has actually changed, avoiding unnecessary work.

from pybevy.prelude import *

Components and Resources

@component
class Health(Component):
    value: float = 100.0
 
 
@resource
class GameScore(Resource):
    def __init__(self):
        self.value = 0
        self.prev_value = 0

Setup

def setup(commands: Commands) -> None:
    commands.spawn(Health(value=100.0))
    print("Spawned entity with Health=100")

Modification System

Periodically modify health to trigger change detection.

_timer = [0.0]
 
 
def modify_health(time: Res[Time], query: Query[Mut[Health]]) -> None:
    _timer[0] += time.delta_secs()
    if _timer[0] > 1.0:
        _timer[0] = 0.0
        for health in query:
            health.value -= 10.0
            print(f"Health changed to {health.value}")

Running the App

@entrypoint
def main(app: App) -> App:
    return (
        app
        .add_plugins(DefaultPlugins)
        .insert_resource(GameScore())
        .add_systems(Startup, setup)
        .add_systems(Update, modify_health)
    )
 
if __name__ == "__main__":
    main().run()

Running this example

Use PyBevy's hot reload feature to run and develop this example. If you don't have PyBevy installed, check out the Quick Start guide.

$pybevy watch change_detection.py

The code will reload automatically when you make changes to the file.


From Python to Rust

Notice how the core concepts in the code—Commands, Assets, App, and Systems—are identical to the original Bevy example?

This is the power of pybevy! It lets you learn Bevy's powerful, data-driven architecture in friendly Python.

When your project grows and you're ready for maximum, native performance, you'll already know the concepts to start writing systems in Bevy Engine with Rust.