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.
Message
Send and receive messages between systems using the message system.
Introduction
Messages (called events in the underlying Bevy engine) allow systems to communicate without direct coupling. One system writes messages, another reads them. Messages are consumed once — they don't persist across frames.
from pybevy.prelude import *Message Types
Define message types by subclassing Message. Each message carries data that the receiving system can act on.
class DealDamage(Message):
amount: float = 0.0
class DamageReceived(Message):
amount: float = 0.0Sending Messages
Use MessageWriter to send messages. Here we deal damage every second.
_timer = [0.0]
def deal_damage_system(time: Res[Time], writer: MessageWriter[DealDamage]) -> None:
_timer[0] += time.delta_secs()
if _timer[0] > 1.0:
_timer[0] = 0.0
writer.write(DealDamage(amount=25.0))
print("Dealt 25 damage!")Receiving Messages
Use MessageReader to read incoming messages. Messages are automatically cleared after being read.
def receive_damage_system(reader: MessageReader[DealDamage]) -> None:
for message in reader.read():
print(f"Received {message.amount} damage!")Running the App
@entrypoint
def main(app: App) -> App:
return (
app
.add_plugins(DefaultPlugins)
.add_message(DealDamage)
.add_systems(Update, (deal_damage_system, receive_damage_system))
)
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.
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.