Perspective - Creating a custom event on component

Is there an example of how to create a custom event on a custom perspective component?

I’ve been trying to follow another thread on how to do this:
https://forum.inductiveautomation.com/t/creating-a-component-event/30403/2
However, the JsonSchema class fails to parse the example in the following post, which causes my module to fail to load:
https://forum.inductiveautomation.com/t/creating-a-component-event/30403/9

I’m running on Ignition 8.0.10, fresh install.
I’ve moved the events key out to a separate json file, which the JsonSchema.parse fails to load:

{
    "type": "object",
    "events": [
        {
            "name": "onCustomClick",
            "description": "This event is fired when a Component is clicked.",
            "schema": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "The name of the component."
                    }
                }
            }
        }
    ]
}


This is the code setting up the component:

package org.example.common.component.display;

import java.util.List;

import com.inductiveautomation.ignition.common.jsonschema.JsonSchema;
import com.inductiveautomation.perspective.common.api.ComponentDescriptor;
import com.inductiveautomation.perspective.common.api.ComponentDescriptorImpl;
import com.inductiveautomation.perspective.common.api.ComponentEventDescriptor;
import org.example.common.RadComponents;

public class RadManager  {
    public static String COMPONENT_ID = "example.controller.manager";
    public static JsonSchema SCHEMA = JsonSchema.parse(RadComponents.class.getResourceAsStream("/radmanager.props.json"));
    public static JsonSchema EVENT_SCHEMA = JsonSchema.parse(RadComponents.class.getResourceAsStream("/radmanager.event.json")); // Line that causes the error

    public static ComponentEventDescriptor EVENT_DESCRIPTOR = new ComponentEventDescriptor("onCustomClick", EVENT_SCHEMA);

    public static ComponentDescriptor DESCRIPTOR = ComponentDescriptorImpl.ComponentBuilder.newBuilder()
        .withPaletteCategory(RadComponents.COMPONENT_CATEGORY)
        .withPaletteDescription("A component to holding custom events.")
        .withId(COMPONENT_ID)
        .withModuleId(RadComponents.MODULE_ID)
        .withSchema(SCHEMA) //  this could alternatively be created purely in Java if desired
        .withPaletteName("Rad Manager")
        .withDefaultMetaName("radManager")
        .withEvents(List.of(EVENT_DESCRIPTOR))
        .shouldAddToPalette(true)
        .withResources(RadComponents.BROWSER_RESOURCES)
        .build();
}

Any help would be appreciated.

I think the error you’re seeing is suggesting that RadComponents.class.getResourceAsStream("/radmanager.event.json") is returning null, which probably means the file is either not packaged in your module or is in the wrong location.

You’re right, I physically called it /radmanager.event.json.
After fixing that and reloading the module, I now get the following when a script on the event runs:

I invoke the event via:
that.props.componentEvents.fireComponentEvent("onCustomClick", { name: "test123" });

I can see the custom event now on the component, and the script I added to the event is:

def runAction(self, event):
	"""
	Method that will run whenever the selected event fires.

	Arguments:
		self: A reference to the component that is invoking this function.
		event: An empty event object.
	"""
	system.perspective.print("Hello World")

So what might EDGE_SCRIPT_BYPASS refer to? I’m running on a demo version of the full Ignition (not edge).

Thanks.

You said you’re on a fresh 8.0.10 install, but you can verify that and verify that your modules are all 8.0.10?

This error suggests you’re not, as mentioned by Paul in this post: Perspective Component Script Actions not working (Ignition 8.0.9)

My bad, I used the 8.0.9 installer (I’m developing across too many machines). This was a fresh install though. Haven’t messed with the base modules.
I’ve now uninstalled, deleted the leftover Ignition files, and installed 8.0.10.

Seems to work now, thanks!

Side question - why doesn’t the argument for the event show up on the generated comments?

Loading a schema as JSON isn’t quite sufficient to parse the extra details of the context (for reasons I’m not sure of, we have an internal method that does the whole process from a JSON file, but don’t expose it.
As a third party author, you need to make your own event descriptor(s) for as many custom events as you want to add. You can either create the JsonSchema manually in code, or use the same parsing methodology. Just take every object inside your events array and (for simplicity) move them into their own files. Then load these resources as well as your main component schema, and register your event descriptors. I added more detail to the previous thread you referenced with the solution to that OP’s issue.