⚠️ 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.

Timers

Use Timer for recurring and one-shot timed events.

Introduction

Timers trigger actions after a duration. TimerMode.REPEATING fires repeatedly, TimerMode.ONCE fires once. Call tick() each frame to advance the timer.

from pybevy.prelude import *

Countdown Resource

@resource
class Countdown(Resource):
    def __init__(self):
        self.percent_trigger = Timer(4.0, TimerMode.REPEATING)
        self.main_timer = Timer(20.0, TimerMode.ONCE)

Timer System

def countdown(time: Res[Time], cd: ResMut[Countdown]) -> None:
    dt = time.delta_secs()
    cd.main_timer.tick(dt)
    cd.percent_trigger.tick(dt)
 
    if cd.percent_trigger.just_finished():
        if not cd.main_timer.is_finished():
            print(f"Timer: {cd.main_timer.fraction() * 100.0:.0f}% complete")
        else:
            cd.percent_trigger.pause()
            print("Main timer finished!")

Running the App

@entrypoint
def main(app: App) -> App:
    return (
        app
        .add_plugins(DefaultPlugins)
        .insert_resource(Countdown())
        .add_systems(Update, countdown)
    )
 
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 timers.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.