Jump to content

Best Practice for using multiple loops?


Recommended Posts

Posted

Hello, 

I want to try my hands at more complicated scripts now that I've made one, but I'm very unsure of how to go about separating out the logic for different actions or states that I expect my bot to encounter. I'd like to have a separate class for each state that my bot will exist in, but I'm not sure how to relate that to the main onLoop method inherited from Script

 

Any suggestions?

Posted

I did something similar in my open source quest bot here:

 

The first thing you should know is that when exiting the main loop you aren't updating the game variables until the loop cycles again. That means you can't execute all your game logic in another loop. You will need to continue this original loop at the end of your method execution.

As for creating different classes are you trying to execute different logic in these classes? If so you might be better off just using methods rather than passing state back and fourth between classes. When you instantiate a class you're creating an object which has its own identity, state, and behavior.

What I do in my script:

Each quest is a class and they all have a template (an abstract class called QuestObject) with a method called "run()".

In the main loop I use polymorphism to pass whatever quest is running to a QuestObject.

Using this object (that can be any quest) I execute the "run()" method which is mandatory for all quests.

Inside the "run()" method I execute any logic for that specific quest.

The "run()" method returns a number to the main loop (0 - 3 usually).

In the main loop I use an integer variable called "status" that holds the returned value from the quest "run()" method.

I run a switch method based on the "status" variable.

1 -> continue running the "run()" method

2 -> the quest is finished so execute the next quest

3 -> there was an error, stop the script.

Then the loop continues.

 

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...