Jump to content

Best Practice for using multiple loops?


candlemagnifico

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

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