Jump to content

Trouble with Custom Events


TheJacob

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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