Is it possible to set a parameter for EventList, so UnityEvents can get them too?

I just created a EventList as stated in the docs, but wondered if it’s possible to define them with a parameter. In the screenshot the UnityEvent is created, but there is no paramter (e.g. string, int, etc…). Wild guess is, this may be me again, not knowing TypeScript too well :sweat_smile:

Original Post on Discord

by user 474974683163394049

TS code is:

by user 474974683163394049

You would need to do that using @type annotation and defining the unitx type you want to be generated or not rely on codegen and add it manually

Manually would be by excluding it from being generated by adding @nonSerialized (you can find info about annotations in the docs about the component compiler)

Works like a charm, thx :blush:

by user 474974683163394049


by user 474974683163394049

Perfect :slightly_smiling_face:

Added an example here for future reference:

(and just fixed it, your syntax is correct haha)

What’s the proper syntax to then subscribe another component’s method to that event?
I’ve added the following to a ComponentA
//@type UnityEngine.Events.UnityEvent<string> @serializable(EventList) onStuffChange: EventList;
Then I’m firing the event at some point with
In another ComponentB, I have a reference to a ComponentA and a method I want to subscribe
checkChange(change: string): void {
}How do I do that? Trying this.componentA.onStuffChange.addEventListener(checkChange);` gives me an error
“Cannot read properties of undefined”

by user 732122195614105653

Where exactly does it throw that error?

in the console, upon start (when it hits .onStuffChange.addEventListener(checkChange))

by user 732122195614105653

Yes could you copy/share the whole error? Ideally with stacktrace and lines :slightly_smiling_face:

You can also screenshot the info in the browser console

You can open the console in e.g. chrome using F12

Ok, for some reason, the EventList, although serialized, was undefined.
I guess empty EventList just get stripped?
Anyway, forcing initialization fixed it.
//@type UnityEngine.Events.UnityEvent<string>
public onStuffChange: EventList = new EventList;

by user 732122195614105653

No they should still be exported :thinking:

I also had forgot to put this before the method name for addEventListener parameter

by user 732122195614105653

Somehow related, am I doing this wrong?

    myStr: string;

    start(): void {
        console.log(this.myStr); //--> prints the value I have set in Unity's inspector

    checkChange(change: string): void {
        console.log(change); //--> prints the value I'm getting from the event OK
        console.log(this.condition); //--> prints undefined ??

*by user 732122195614105653*

Btw if you add 3 ` the code is easier to read (ideally adding ts too ^^)