How to use EventList that accept arguments?

OS: Windows 11
Needle Engine: 3.51.0
Unity: 2022.3.15

Hi! I was wandering how can I invoke an EventList that has arguments and then how can I manage those arguments. Could you give an example using 3 classes ? Class A : Holds the EventList, Class B invokes the EventList and class C and D both listen that same EventList and manage in different ways the arguments of that EventList.

Thanks

Hello diegoeloko!

To utilize an EventList with arguments in Needle Engine, you can create the following three classes:

  1. Class A: Holds the EventList.
  2. Class B: Invokes the EventList.
  3. Class C and D: Listen to the same EventList and process its arguments in different ways.

Here’s an example implementation:

import { EventList, Behaviour, serializable } from "@needle-tools/engine";

// Class A - Holds the EventList
class EventHolder extends Behaviour {
    @serializable(EventList)
    public myEvent = new EventList<string>();

    constructor() {
        super();
        // Listeners from Class C and D
        this.myEvent.addEventListener(this.handleEventC);
        this.myEvent.addEventListener(this.handleEventD);
    }

    private handleEventC = (arg: string) => {
        console.log(`Class C handled the event with argument: ${arg}`);
    };

    private handleEventD = (arg: string) => {
        console.log(`Class D processed the argument differently: ${arg.toUpperCase()}`);
    };
}

// Class B - Invokes the EventList
class EventInvoker extends Behaviour {
    public eventHolder: EventHolder;

    constructor(eventHolder: EventHolder) {
        super();
        this.eventHolder = eventHolder;
    }

    public triggerEvent() {
        this.eventHolder.myEvent.invoke("Hello from EventInvoker!");
    }
}

// Class C - Custom handler
class EventListenerC extends Behaviour {
    public eventHolder: EventHolder;

    constructor(eventHolder: EventHolder) {
        super();
        this.eventHolder = eventHolder;
    }

    public logEvent() {
        console.log("Class C is ready to handle events.");
    }
}

// Class D - Custom handler
class EventListenerD extends Behaviour {
    public eventHolder: EventHolder;

    constructor(eventHolder: EventHolder) {
        super();
        this.eventHolder = eventHolder;
    }

    public performAction() {
        console.log("Class D is executing its action.");
    }
}

How it works:

  • EventHolder: Contains myEvent of type EventList<string>. It adds listeners from classes C and D during initialization.
  • EventInvoker: Triggers the myEvent event using invoke, passing a string argument.
  • EventListenerC and EventListenerD: These classes implement distinct behaviors to process the arguments when the event is invoked.

You can now instantiate these classes and call triggerEvent() from an instance of EventInvoker to see how classes C and D react.

If you need further clarification or adjustments based on your requirements, please let me know!

1 replies left - Get a PRO license for more AI replies.
thread_jxMO8C8TXHPedwFreuAGe2lO
Reply to this message to continue the conversation

I got an error that underlines the “string” data type and it says about ther class EventList : Expected 0 type arguments, but got 1. Do you know why ?

Hello diegoeloko!

The error you encountered indicates that EventList does not expect any type arguments. This is a change in how EventList was defined. Instead of specifying a type, you can directly work with EventList without generics.

Here’s how you can modify the code:

import { EventList, Behaviour } from "@needle-tools/engine";

// Class A - Holds the EventList
class EventHolder extends Behaviour {
    public myEvent = new EventList(); // No type argument

    constructor() {
        super();
        // Listeners from Class C and D
        this.myEvent.addEventListener(this.handleEventC);
        this.myEvent.addEventListener(this.handleEventD);
    }

    // Make sure to accept the argument type if necessary
    private handleEventC = (arg: string) => {
        console.log(`Class C handled the event with argument: ${arg}`);
    };

    private handleEventD = (arg: string) => {
        console.log(`Class D processed the argument differently: ${arg.toUpperCase()}`);
    };
}

// Class B - Invokes the EventList
class EventInvoker extends Behaviour {
    public eventHolder: EventHolder;

    constructor(eventHolder: EventHolder) {
        super();
        this.eventHolder = eventHolder;
    }

    public triggerEvent() {
        // Invoke the event with a generic argument that you can handle in listeners
        this.eventHolder.myEvent.invoke("Hello from EventInvoker!"); // Ensure you manage this argument properly
    }
}

Notes:

  • Remove the <string> from the EventList definition.
  • You may access and manage arguments in event listeners as needed, though you might want to consider the expected structure of events.

This should resolve the issue you’re facing. If you have further questions or need more details, feel free to ask!

0 replies left - Get a PRO license for more AI replies.
thread_jxMO8C8TXHPedwFreuAGe2lO
Reply to this message to continue the conversation

Hi, the bot’s original answer shows how to use Eventlist in Needle Engine 4. In Needle Engine 3 you need to remove the generic argument (<string>)

So, if I just declare as EventList without specify the type, can I call a function that is expecting param even if the EventList doesn’t has that type ? For example with the function handleEventC which expects an string .

Yes, you can just call this.myEvent.invoke("test")

Thanks marcel! Wishing you all the best in the end of the year seson :slight_smile: