0
\$\begingroup\$

I'm trying to integrate the idea of an event queue with a game loop that creates entities and responds to input. Is it better practice to submit entity reaction functions to the queue during input processing, or is there a way to generically broadcast events like text submission to the event queue? What would the class structures of entity and event queue look like to support the latter?

Specifically I'm looking for a response that includes code (any language) or pseudo code. Similar answers lack the specificity I need to make sense of how fields on the structs or classes enable registration of listeners, broadcasting, and handling of events. pending function array approach

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You need this: 1. Game Programming Patterns - Command 2. Game Programming Patterns- EventQueue \$\endgroup\$
    – Di Zhang
    Jun 10, 2019 at 3:14
  • \$\begingroup\$ Those links are actually what sparked this question, sorry for leaving that bit out! What section of these documents do you find is the most instructive in defining the registration part of the event queue's listeners or responding function calls? \$\endgroup\$
    – Li Brary
    Jun 10, 2019 at 3:37

1 Answer 1

1
\$\begingroup\$

This question was given attention on a discord server and several very helpful blocks of rust code were provided.

The most important to me was seeing in code the creation of an event listener function. In rust the following closure was provided by azrael in the amethyst discord server.

let player_entity = unimplemented!("fill this in");
let my_event_listener = |world: &mut World, event: &InputEvent| {
    if event.attack_pressed {
        let players = world.write_storage<Player>();
        let mut player_component = players.get_mut(player_entity).unwrap();
        player_component.set_state(Attacking);
    }
}

here we demonstrate a even listener that has the ability to change the player entity to indicate its new state after the event for an attack button getting pressed is triggered.

the event listener would go in a collection separate to the event queue (i suspect you want to add/remove the event listeners separately)

now a demonstration of the actual connection of events to listeners is demonstrated. The events variable would be populated in input processing or game state changes, and then shared with each listener that we iterate over in the bottom of the game loop.

loop {
    // receive events from windowing system, and perhaps other system.
    let events = // ...
    // these may also be inserted into the world, and maybe the ECS systems also add more events.

    // run systems
    dispatcher.update(&mut world)

    // event listeners
    for event_listener in event_listeners.iter() {
        event_listener(world, events);
    }
}

All in all, this represents one concrete pattern for registering listeners with an event queue.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Can you elaborate on what that code does and how it solves the problem described in the question? \$\endgroup\$
    – Philipp
    Jun 10, 2019 at 15:07
  • \$\begingroup\$ updated to reflect more of the actual functionality and why it fit my issue. \$\endgroup\$
    – Li Brary
    Jun 10, 2019 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .