Dealing with async

Hey guys @Ryan @JellyBitz

I got some questions for you.

While writing this plugin I talked about before I encountered the issue of dealing with the thread.
I’m farily new to promises and asynchronous functions so I don’t really know how to proceed.

I want my plugin to stop the execution after using a return scroll until spawning in town.
I tryed using sleep() and I tryed async / await.

Sleep is not really an option here, and I can’t get any function to execute when declaring it with async.

Any tips on how to solve/work around this issue?

Here’s the link to the plugin so far: https://github.com/joranro1997/phBot-plugins/blob/main/SP%20Quest_testing.py

As a workaround I thought about making the recall a part of a separate button press, making the user have to press 2 buttons in order to get the quest turned in or accepted, but I’d rather have it work the other way.

I’d love to know how the bot handles recalling to town in this way. An API for the return to town function would also be great!

Theres an API already for using a return scroll use_return_scroll()

If you dont want to use sleep , the easiest way to me (maybe not to a real coder) is just seperate out all your else: if: (you should check out elif) into their own function and just use a threading timer. You could probably also do something with packets to detect when you spawn after using the return scroll but that seems unnecessary for this simple task.

You’ll need to synchronize events to lock your functions without sleep usage.
This is a quick sample to understand how it could be done:

from phBot import *
import threading
eventTeleported = threading.Event()

# Your function previously executed
def YourFunction():
	# ...
	# Pausing your code execution ...

	# Return scroll usage..
	log('Plugin: Using return scroll...')
	success = UseReturnScrollTask()
	if not success:
		log('Plugin: Error teleporting...')
		return
	log('Plugin: Teleported!')
	
	# Continue your code execution ...
	# ...

# Task for using a return scroll
def UseReturnScrollTask():
	# API return success?
	success = use_return_scroll()
	if success:
		global eventTeleported
		eventTeleported.wait()
	return success

# Called after every teleport
def teleport():
	log("Plugin: Teleported...")
	global eventTeleported
	eventTeleported.set()

# Just for testing purposes..
threading.Timer(5,teleport).start()
YourFunction()

@JellyBitz I tryed this and it worked fine, but the result is basically the same as if using sleep()
Both the game and the bot freeze for the ammount of time set to wait and no messages are able to be read in the bot console until it finishes waiting (even if the messages where logged before the wait)

This makes it look like the bot crashed and I fear it might cause the bot to not be using potions while teleporting, resulting in posible deaths. (not sure if the bot is using pots in the background while it’s waiting tbh).

Other than that, the return scroll api would have saved me a lot of time, plus using elif makes the code look better, so thanks @DeRidder14

I assume the only way to make the bot/game not freeze while waiting for the return scroll to cast is to capture the spawn packet as an event to trigger a function.

Alright never mind I found the solution.

All I had to do was create another thread and make the function run in the new thread, resulting in the game and bot not freezing.

Btw is there any quest API that I don’t know about (besides the get_quests function). Would be great to not have to inject every packet manually.