PROMETHIA-27 / dependency_injection_like_bevy_from_scratch

Learn how to do dependency injection like bevy engine, from scratch!
MIT License
45 stars 5 forks source link

Chapter Suggestion #7

Open Ike-l opened 1 month ago

Ike-l commented 1 month ago

Hello! I would love to see a chapter dedicated to how bevy handles "non resources" i.e "Queries", "Events".

To showcase queries you would need some sort of "World" system in place, not sure how feesible it is to write it from scratch or use a library like hecs

PROMETHIA-27 commented 1 month ago

Events are just a wrapper over a resource if I remember correctly, so I'd probably just leave those as an exercise for the reader.

Queries might be interesting to cover. Thank you for the suggestion!

Ike-l commented 1 month ago

Where do you learn about these topics, directly from the source code? If other resources are being used can you share them? Thanks

PROMETHIA-27 commented 1 month ago

Mostly, I read the source code and talked with bevy users/contributors/maintainers in the discord. Besides that I just had a long background programming and an interest in programming languages and the limits of what can be done with them.

Ike-l commented 1 month ago

Sorry to bother you again. I have an idea for how to handle events but it seems needlessly complicated compared to your previous comment and i couldnt quite work out some faults. here is my idea: Do i make "IntoEvent", "Event" and "EventParam" traits like for systems. Store a type id of a struct that implements Event as a key and a vec of Any Type (or type which the Event has as input?) for the value in a hashmap. Then i modify the impl_system macro to change from "Res"/"ResMut" to an "EventWriter"/"EventReader" (somehow know when to, maybe check if i have an EventWriter before giving the Res/ResMut) Have the EventWriter hold a mutable reference to the value in the hashmap and when write is called that value is pushed to the vec Then in the macro it will call the corresponding system with the EventReader param (somehow?)

I tried looking through the source code but it was quite tricky and i have been working on events for a couple days so i would like a hint to guide me a bit better. Any hint is much appreciated, thanks.

PROMETHIA-27 commented 1 month ago

The way Bevy events work, if I recall correctly, is to create a normal resource called something like EventQueue, which can be accessed normally like Res<EventQueue>. Then they create two new SystemParams called EventReader and EventWriter, which under the hood just access Res<EventQueue> and work with it in a particular way. The reader borrows all not-yet-consumed events since about 2 frames ago (at least since I last checked; i think the logic has become more complex w.r.t. FixedUpdate) and the writer pushes a new event into the queue. I recommend reading the bevy source for it directly to get a better understanding, it's a fairly simple part of the ECS: https://github.com/bevyengine/bevy/blob/main/crates/bevy_ecs/src/event/reader.rs

Ike-l commented 1 month ago

Thanks! I have implemented my version, though im sure there are plenty of bugs to fix... If you ever want to see the code base to show examples in your book feel free to ask (its basically yours anyways lol)!