Jump to content

Trouble with Custom Events


Recommended Posts

Posted (edited)

Hey folks, I'm writing a custom Event and the MethodProvider 'execute' method doesn't provide my event to the engine for execution. I expect "Running event." to be output in the OSBot client logger; however, nothing is output.

// Events.java
public class MyEvent extends Event {
    @Override
    public int execute() {
      log("Running event."); // Debug info.
      return -1; // Immediately stops.
    }
}

// MyScript.java
//
// in @onStart
this.myEvent = new MyEvent();
//this.myEvent.exchangeContext(getBot()); **See follow-up**

// in @onLoop
execute(this.myEvent);
return 20000; // to avoid repeated executions.

Follow-up:

I shouldn't call exchangeContext in @onStart because Event is a subclass of MethodProvider. That said, when I remove that call, I now run into a different issue where "Running event." is continuously output to the OSBot client logger rather than once. I expected it to be output once because I return -1 in myEvent.execute. Does anyone know how to solve that?

Follow-up:

If I set the return value from -1 to 2000, then it behaves as expected (outputting "Running event." every 2000ms to OSBot client logger). So, I *think* my question is, how do I properly flag that my event is ready for termination?

Edited by TheJacob
Posted (edited)

Solution:

In order to properly flag a subclass of Event for termination, you must call "setFinished". Complete code:

// MyScript.java
//
// @onLoop
execute(new MyEvent());

// MyEvent.java
public class MyEvent extends Event {
  public MyEvent() { }
  public int execute() {
    // Do stuff
    // ...
    // Ready to finish.
    setFinished();
    return 0;
  }
}

Thank you @Gunman for referring me to this thread below as API Docs for execute confused me (lead me to believe a negative number returned would terminate the Event).

Edited by TheJacob
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...