How do you add An event to My extension?

Hello everyone.
I am developing a new exstension
and I need to add An event block to IT.
I've searched Every where on the internet.
But i can't find anything on how to add An event block To my exstension.
Does Anyone know how to add this?
Thanks.

okay, let me explain..

events blocks are the same as other java methods.

  • access modifier = public
  • return type = void

example of events


@SimpleEvent(description = "DEVELOPER DOCS")
public void MyEventName()    {
    // add your code here.
    EventDispatcher.dispatchEvent(this, "MyEventName");
}

@SimpleEvent(description = "DEVELOPER DOCS")
public void MyOtherEvent(int param1, int param2)    {
    // add your code here.
    EventDispatcher.dispatchEvent(this, "MyOtherEvent", param1, param2);
}

you can call Event as a normal java method call.

5 Likes

you forgot to add void between public and MyEventName

2 Likes

thank you man

2 Likes

Thanks
May i also know if it's possible to call An event from a void.
And how to do that?
Thanks

Maybe you mean like this :

@SimpleFunction(description = "")
public void AddData(String text, int number)    {
    MyOtherEvent(text, number);
}

@SimpleEvent(description = "DEVELOPER DOCS")
public void MyOtherEvent(String param1, int param2)    {
    // add your code here.
    EventDispatcher.dispatchEvent(this, "MyOtherEvent", param1, param2);
}
1 Like

An important note regarding events: You should always invoke the EventDispatcher.dispatchEvent method on the main thread. So if your code calling the event method isn't running on the main thread, you need to call form.runOnUiThread() and pass it a runnable that invokes the event.

5 Likes

Thanks everyone for helping!
I stil have a few questions.

  1. how do i add a variable to an event and give it a value?

do i need to use a code to get the users blocks or not?
thanks

see this example

Yes but how do i give the variabeles a value?

I would suggest that you review section 4 of the "How to add a component" document. Everything there applies to extensions.

1 Like

Thanks.
But that still doesn't really explains how to add variabeles.
Like this:
20210506_195317
OR how to give IT a value.

Events are the same as the normal java method if you know how the local variable works in java's method then it is not difficult for you.

I don't know what exactly you want to do with this variable.

let me take an example for you...

suppose you want to make a simple math extension that has multiple methods(purple blocks) and some events(yellow blocks).

now all methods made with @SimpleFunction annotation and all events made with @SimpleEvent annotation.

now this extension has one method called Divide which takes two arguments from the user and generates output lets code it...


@SimpleFunction(description = "DEVELOPER DOCS")
public void Divide(double number1, double number2)    {
    try    {
        double result = number1/number2;
        Result(result);
    } catch (ArithmeticException e)    {
        ErrorOccurred("Divide By 0 Error");
    }
}

// now let us create two events

@SimpleEvent(description = "DEVELOPER DOCS")
public void Result(double result)   {
    EventDispatcher.dispatchEvent(this, "Result", result);
}

@SimpleEvent(description = "DEVELOPER DOCS")
public void ErrorOccurred(String errorMessage)   {
    EventDispatcher.dispatchEvent(this, "ErrorOccurred", errorMessage);
}

let's look at blocks

component_method
component_event (1)
component_event

now when you call

component_method

with number1 = 12 and number2 = 2, it will automatically call this event with your result 6

component_event

but when you call it number1 = 12 and number2 = 0 it will automatically call this event with an error message

component_event (1)

Now come at your question

In your question, you are asking how to add speed variable which looks similar to result or errorMessage in our case, they are local variables of a method.

1 Like

I can not explain more than that, because extension development with java is big topic to explain.

please do hard work on java and then start developing extension.

Yes that's Exactly what i am trying to do Thanks To everyone for helping me!

1 Like

I hope you understand it if you want source code then tell me i will post.