Skip to content

Commit

Permalink
resource events
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Nov 25, 2024
1 parent fe9e2ef commit 1e61d00
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- [Curves (WIP)](./resources/curve.md)
- [Custom Resource](./resources/custom.md)
- [Asset Hot Reloading](./resources/hot_reloading.md)
- [Events](./resources/events.md)
- [Engine](./engine/engine.md)
- [Graphics Context](./engine/graphics_context.md)
- [Window Management](./engine/windows.md)
Expand Down
19 changes: 19 additions & 0 deletions src/code/snippets/src/resource/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fyrox::{asset::event::ResourceEvent, asset::manager::ResourceManager};
use std::sync::mpsc::channel;

// ANCHOR: subscribe_to_events
pub fn subscribe_to_events(resource_manager: &ResourceManager) {
let (sender, receiver) = channel();
resource_manager.state().event_broadcaster.add(sender);

while let Ok(event) = receiver.try_recv() {
match event {
ResourceEvent::Loaded(_) => {}
ResourceEvent::Reloaded(_) => {}
ResourceEvent::Added(_) => {}
ResourceEvent::Removed(_) => {}
}
}
}

// ANCHOR_END: subscribe_to_events
1 change: 1 addition & 0 deletions src/code/snippets/src/resource/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod custom;
pub mod events;
pub mod model;
pub mod sound;
pub mod state;
Expand Down
24 changes: 12 additions & 12 deletions src/code/tutorials/fps/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions src/code/tutorials/rpg/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/resources/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Events

Resource manager is able to notify its subscribers about specific events of resources. There are four kinds of resource
events:

1) `Loaded` - occurs when a resource was fully loaded without any errors.
2) `Reloaded` - occurs when a resource was already fully loaded, but was reloaded by an explicit request.
3) `Added` - occurs when a resource was just added to the resource manager. This event is fired right after a resource
was requested from the manager.
4) `Removed` - occurs when a resource was removed from the resource manager. This event is fired when the resource
manager removes and unloads an unused resource.

To subscribe for resource events use the event broadcaster:

```rust
{{#include ../code/snippets/src/resource/events.rs:subscribe_to_events}}
```

0 comments on commit 1e61d00

Please sign in to comment.