Python Forum
Trouble with threading and reading variable from a different script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with threading and reading variable from a different script
#11
(Apr-26-2023, 02:53 PM)deanhystad Wrote: I don't understand why you insist on running with threads when the problem clearly cries out for you to do one thing at a time. You are either walking or attacking, one at a time. You do not do them at the same time. The only thing you do all the time is healing. So I see only two thrreads and really no need for two.

Thats not entirely true. The bot should not "walk" while its attacking, thats true, but it should be constantly checking for targets while walking, and if found, proceed with the attacking function. If thats impossible to achieve the way I am trying to do it now, should I modify the walking function so it looks for targets and if found proceeds with attacking function? But then, how do I pause the progress of the walking script so that it doesn't start from the beginning when the target is killed?
Or should I go a step further and cram all of those functions in some nested if else statements so that I have only one walking+attacking function, together with healing one, but I feel like this will create a bunch of other difficulties.
Quote:Declare global functions in the global scope, before you start any of your tasks.
You mean I should write czekaj = False at the beginning before all functions? Will it not influence the results obtained from the "global one" from attacking function? Or should I abandon defining them as global in attacking alltogether and keep it as a simple czekaj available to the whole script? And then, will walking not look at the "top one" instead of the one changed by attacking function if they run in separate threads?

Quote:Nothing in the loop is checking the value of "czekaj", so it is going to run until wptcenter != minmap_center regardless of what attacking is doing.
I see, should I change it into
if wptcenter != minimap_center and czekaj is False:
with some additional elif for where only one of them is True, implying I keep the czekaj variable?

Quote:I don't know what you think this will do.
My bad. I was testing different options back when those were separate files, and it seems like I copied it together with that, and since it didn't impact the script, I didn't even notice them.
Reply
#12
Quote:Thats not entirely true. The bot should not "walk" while its attacking, thats true, but it should be constantly checking for targets while walking, and if found, proceed with the attacking function. If thats impossible to achieve the way I am trying to do it now, should I modify the walking function so it looks for targets and if found proceeds with attacking function? But then, how do I pause the progress of the walking script so that it doesn't start from the beginning when the target is killed?
Or should I go a step further and cram all of those functions in some nested if else statements so that I have only one walking+attacking function, together with healing one, but I feel like this will create a bunch of other difficulties.
Makes sense to me that walking would attack when it finds a target. Your desired behavior is walking pauses while attacking and resumes when attacking is done. That is easily done by having walking call attacking when it wants to attack, and attacking returns when the attack is complete. Maybe Walking should be renamed Rampage.

Quote:You mean I should write czekaj = False at the beginning before all functions? Will it not influence the results obtained from the "global one" from attacking function
it does influence anyone using czekaj, but only in good ways. Initializing czekaj before launcing any of your threads means it is defined before any of the thread functions are called. You got lucky that you launched Attacking first. If you had launched Walking first, it would crash because it expected to find a variable named czekaj, which didn't exist yet.

Another advantage of assigning the variable outside the functions is it makes it clear that this is a variable in the global scope. It also provides a convenient place for documenting the variable.
czekag = False  # Flag used to pause Walking thread while Attacking.
Reply
#13
(Apr-26-2023, 05:37 PM)deanhystad Wrote: Makes sense to me that walking would attack when it finds a target.

Ok, so I followed your advice. I've modified the attacking() function so that it only performs desired actions then "returns". Then I modified the walking() function in the easiest way possible (for testing purposes into nested if statements) so that it looks for the "empty" battlelist image to then perform its walking functions or turning attacking on (I've actually added 2 places where it looks for the image). I've removed the czekaj argument completely as it doesn't seem to be needed in the way the script works now. Now the bot kinda works but at the same time, not always.

def walking():
    minimap_center = (910, 103)
    images = ['engine/checkmark.png', 'engine/teststar1.png', 'engine/cross.png']
    while True:
        for image in images:
            if pyautogui.locateOnScreen(image, region=(857, 51, 115, 115), confidence=0.7) != None:
                print("I can see it", image)
                if pyautogui.locateOnScreen('engine/empty.png', confidence=0.7) == None:
                    print("attk1")
                    attacking()
                elif pyautogui.locateOnScreen('engine/empty.png', confidence=0.7) != None:
                    wptcenter = pyautogui.locateCenterOnScreen(image, confidence=0.7)
                    print(wptcenter)
                    pyautogui.click(wptcenter)
                    print("moved to", image)
                    time.sleep(1)
                    if wptcenter == minimap_center:
                        print("reached", image)
                        if image != 'lock.png':
                            print('wpt walk reached')
                            time.sleep(1)
                        else:
                            wptcenter = pyautogui.locateCenterOnScreen(image, confidence=0.7)
                            pyautogui.click(wptcenter)
                            time.sleep(2)
                            print('rope')
                            pyautogui.press('f12')
                            pyautogui.click(337,316) 
                            time.sleep(1)
                                
                    else:
                        while wptcenter != minimap_center:
                            if pyautogui.locateOnScreen('engine/empty.png', confidence=0.7) == None:
                                print("attk2")
                                attacking()
                            elif pyautogui.locateOnScreen('engine/empty.png', confidence=0.7) != None:
                                wptcenter = pyautogui.locateCenterOnScreen(image, confidence=0.7)
                                pyautogui.click(wptcenter)
                                print("clicking")
                                time.sleep(1)
The problem comes with the script finding the empty battlelist image. Sometimes it ignores the fact that there is no empty.png image (there are targets) and just walks. Sometimes it notices those targets but in the place where "print("attk2")" takes place, then it performs attacking function and continues. Rarely it manages to catch on that targets are there at "print("attk1")". It doesn't matter whether the battle list is empty or full at the beginning of the script, it randomly works, and randomly doesn't. I've tried different time.sleeps in between parts of the loop but that doesn't seem to help either.
Reply
#14
I don't understand you logic. I would expect the rampaging (walking and attacking) code to look something like this.
while True:
    find target
    if at target:
        attack
    else
        move toward target
Finding the target would look something like this:
target = None
target_distance = None
for image in target_images:
    if found image:
        distance = distance to target
        if target is None or target_distance > distance:
            target = image
            target_distance = distance
Reply
#15
(Apr-26-2023, 10:40 PM)deanhystad Wrote: I don't understand you logic. I would expect the rampaging (walking and attacking) code to look something like this.

I'm sorry, I think I explained the whole bot insufficiently.
Walker is not looking for an image of a target. Its job is to move around the game map, using waypoint images planted on the minimap (find waypoint image checkmark.png, click on it, the character then moves into that direction), thats why the walker is clicking on the image until it reaches the center of the minimap and looks for the next image(that means that specified destination was reached and it can continue).
At the same time, there is a chance that a monster will come from any direction while the character is walking. The easiest way to find if there are any monsters, is to look at the battlelist. (empty=no monsters, not empty=monster).
If its not empty, then comes the attacking function that presses the attack button, looks if the monster is attacked,if it is killed loots it and then continues.
In the previous versions, the walker was focused on looking for waypoint images on minimap only, while attacking script was focused on battlelist.
Now I modified it so that attacking just performs actions, while walker looks for image on minimap first, then checks the battlelist.

Your example would work, if I was simply looking for image of the target to attack, maybe it can work in my case aswell, but I don't think I know how to do it that way.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 432 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,865 May-03-2023, 08:22 AM
Last Post: billykid999
Question How can I import a variable from another script without executing it ThomasFab 12 7,839 May-06-2022, 03:21 PM
Last Post: bowlofred
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,131 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Trouble reading files using pyexcel codebeacon 2 2,204 Feb-08-2021, 05:53 AM
Last Post: codebeacon
  Python reading variable in another py file wrongly _vertig0 2 1,983 Nov-21-2020, 07:19 AM
Last Post: _vertig0
  Trouble with reading csv file and putting it into a file Milfredo 3 2,278 Sep-04-2020, 05:30 AM
Last Post: Milfredo
  Use dynamic variable from parallel running python script Sicksym 0 1,855 May-15-2020, 02:52 PM
Last Post: Sicksym
  [split] script: remove all "carriage return" from my json variable pete 2 2,814 May-05-2020, 03:22 PM
Last Post: deanhystad
  Trouble reading Excel file. Shembeginner 2 2,309 Apr-07-2020, 04:55 AM
Last Post: Shembeginner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020