#Issue with CustomEvents

1 messages · Page 1 of 1 (latest)

gilded moat
#

Hi I'm following the documentation of Declare a custom event type : https://engine.needle.tools/docs/scripting-examples.html#create-and-invoke-a-unityevent

1 - I have added this C# script to the Needle/Components.codegen file :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class MyCustomUnityEvent : MonoBehaviour
{
}

2- Then I copy-pasted both classes from the documentation


    // The next line is not just a comment, it defines 
    // a specific type for the component generator to use.

    //@type MyCustomUnityEvent
    @serializable(EventList)
    myEvent!: EventList;

    // just for testing - could be when a button is clicked, etc.
    start() {
        this.myEvent.invoke("Hello");
    }
}

export class CustomEventReceiver extends Behaviour {

    logStringAndObject(str: string) {
        console.log("From Event: ", str);
    }
}```

3- But when I add both scripts to an GameObject , the field of `myEvent` from `CustomEventCaller ` looks like this and I cannot add the the reference of `CustomEventReceiver ` :

Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development, and can be deployed anywhere. It is flexible, extensible, and collaboration and XR come naturally. Needle Exporter for Unity bridges the Unity Editor and the web runtime. It helps you to export your assets, animations, lightmaps and so on to the web. It is...

#

If I delete this line from CustomEventCaller : //@type MyCustomUnityEvent, then it looks like this :

#

But when I Invoke the method, nothing happens

#

Do you know what could be happening ?

lean ember
gilded moat
lean ember
#

Can you show an example where you have added a method to the event?

gilded moat
#

Srry for no answer but I made another example and it works almost fine. This is my example :
I have a Sphere with two scripts :
The first one :


    // The next line is not just a comment, it defines 
    // a specific type for the component generator to use.

    
    @serializable(EventList)
    myEvent!: EventList;


    update(): void {
        if(this.context.input.getKeyDown()=='a'){
            this.myEvent.invoke("Hello");
        }
    }
}```
The second one : 

export class CustomEventReceiver extends Behaviour {
logStringAndObject(str: string) {
console.log("From Event: "+str);
}
}

#

This is how the sphere looks in Unity :

#

This is the console's log :

#

So it seems that the event actually is beeing invoking but not with the argument of "Hello" which I'm passing in CustomEventCaller.

#

Otherwise if I write directly in Unity the argument for the method logStringAndObject, like this :

#

This is my log.

#

But I want the argument to be passed from the script CustomEventCaller, Do you know what could be wrong ? Thanks 🙂

lean ember