Cant use def!?!?

Asked by danne95

I wanted a bot for a game and I found one on a forum.
And it seems like my sikuli can handle def command(or whatever it is). When i run it sikuli closes in like 1 sec then sikuli pop ups again leaving no error.
This is how some of the code looks:

def letsmine():
    if mineiron == True:
        if gamezone.exists(iron):
            gamezone. click(iron, 0)
            wait(waittime)
            levelup()
    if mineiron == True:
        if gamezone.exists(iron2):
            gamezone. click(iron2, 0)
            wait(waittime)
            levelup()
    if mineiron == True:
        if gamezone.exists(iron3):
            gamezone. click(iron3, 0)
            wait(waittime)
            levelup()

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
danne95
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

a def on its own does nothing. It is only a piece of prepared code to be run by calling it.
Normally it is used to run the same workflow with different parameters and/or at different places in main workflow.

To run a def you have to call it like:
letsmine()

--- optimized version 1
assuming, that levelup() might change mineiron and/or more than one iron might exist in parallel

def letsmine():
    if mineiron and gamezone.exists(iron, 0):
            click(gamezone.getLastMatch())
            wait(waittime)
            levelup()
    if mineiron and gamezone.exists(iron2, 0):
            click(gamezone.getLastMatch())
            wait(waittime)
            levelup()
    if mineiron and gamezone.exists(iron3, 0):
            click(gamezone.getLastMatch())
            wait(waittime)
            levelup()

--- optimized version 2
assuming, that mine iron is not changed and only one iron exists in game zone at one time

def letsmine():
    if mineiron and
        (gamezone.exists(iron, 0) or
         gamezone.exists(iron2, 0) or
         gamezone.exists(iron3, 0) ):
        click(gamezone.getLastMatch())
        wait(waittime)
        levelup()

the variables mineiron, iron, iron2, iron3 and waittime must be defined before calling lets mine

Revision history for this message
danne95 (bf-heroes1337) said :
#2

Thanks alot man it works :D

Revision history for this message
danne95 (bf-heroes1337) said :
#3

When i added this i got this error message:
[error] Stoppad
[error] Ett fel uppstod på rad 221
[error] Felmeddelande: Traceback (most recent call last):
 File "C:\Users\Daniel\AppData\Local\Temp\sikuli-tmp892078388341814867.py", line 221, in
 if noore == 1: #if no ore founded call room changer module.
NameError: global name 'noore' is not defined

It says i have not defined noore but noore should not be defined. HELP :D

The code i added:

#Variables
global lastroom #Variable using moving to right room. Dont edit
global currentroom #Variable using moving to right room. Dont edit
global noore #Variable using moving to right room. Dont edit

def mine5_19(): #Room swap module
    global lastroom
    global currentroom
    global noore
    if gamezone.exists(pic, 0):
        currentroom = 1
        gamezone.click(pic, 0)
        lastroom = 1

    if gamezone.exists(pic, 0):
        currentroom = 2
        if lastroom == 1:
            gamezone.click(pic, 0)
            lastroom = 2
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 3
        if lastroom == 2:
            gamezone.click(pic, 0)
            lastroom = 3
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 4
        if lastroom == 3:
            gamezone.click(pic, 0)
            lastroom = 4
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 5
        if lastroom == 4:
            gamezone.click(pic, 0)
            lastroom = 5
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 6
        if lastroom == 5:
            gamezone.click(pic, 0)
            lastroom = 6
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 7
        if lastroom == 6:
            gamezone.click(pic, 0)
            lastroom = 7
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 8
        if lastroom == 7:
            gamezone.click(pic, 0)
            lastroom = 8
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 9
        if lastroom == 8:
            gamezone.click(pic, 0)
            lastroom = 9
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 10
        if lastroom == 9:
            gamezone.click(pic, 0)
            lastroom = 10
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

    if gamezone.exists(pic, 0):
        currentroom = 1
        if lastroom == 10:
            gamezone.click(pic, 0)

while ui.exists(pic, 0): #while this image on screen it keep looping this.
    letsmine() #Start mining module
    fight() #run fight module
    if closebutton.exists(pic, 0): #Check if fight is over
        fightover() #call fightover module and close it.
    else:
        if noore == 1: #if no ore founded call room changer module.
            mine5_19()
        pass

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Uuups, you should have a look at lists and loops - makes code much shorter and more flexible ;-)

You only said, that noore is global. But nowhere it is getting a value like noore=1.

BTW: declaring a variable as global is only needed inside a def if you change its contents inside the def

x = 1 # x is global

def func():
    print x # x is still global

def func1():
    x = 5 # this x is local to the def
    print x

def func2():
    global x
    x = 2
    print x

print x # 1
func() # 1
print x # 1
func1() # 5
print x # 1
func2() # 2
print x # 2

Revision history for this message
danne95 (bf-heroes1337) said :
#5

I do not understand. I did not write this code so I do not understand that much. Cant I just change something in my code instead of replace it?

Revision history for this message
RaiMan (raimund-hocke) said :
#6

The piece of code, you are presenting here loops around
--- calling letsmine()
--- calling fight()
--- check fight is over
--- if not, change room

so some where in the other functions noore must get a value.

If you send me all the stuff zipped, I can have a look at it if you want.

Revision history for this message
danne95 (bf-heroes1337) said :
#7

Ok, here's my script: http://www.mediafire.com/?8852n282bd8i2xd

Thanks alot for helping me

Revision history for this message
RaiMan (raimund-hocke) said :
#8

Sorry, you have implemented bot "versions" of letsmine().

I have commented out the harming stuff at line 210.

So noore was really never touched before.

download: http://dl.dropbox.com/u/42895525/My_Miner.sikuli.zip

Revision history for this message
danne95 (bf-heroes1337) said :
#9

so shall I delete this?

"""
# this function overrides the one before
# and never sets noore
def letsmine():
    if mineiron == True:
        if mineiron and (minezone.exists(iron, 0) or minezone.exists(iron2, 0) or minezone.exists(iron3, 0) ):
            click(minezone.getLastMatch())
            toolcheck()
            wait(waittime)
            levelup()
"""

Revision history for this message
danne95 (bf-heroes1337) said :
#10

can you take a look at this scrip to. It's for another job in the game i play and its the same problem as I posted in the beginning of this post: http://www.mediafire.com/?bpwmi2lnsiidetb

Revision history for this message
RaiMan (raimund-hocke) said :
#11

@ comment #9

using the block comment (""" before first line and """ after the last line of the stuff currently not needed) this part of the script is not active at the moment. I leave it up to you to delete it.

Revision history for this message
RaiMan (raimund-hocke) said :
#12

@ comment #10
The script is syntactically ok.
I cannot judge what it does, but it should principally work, if the regions defined in the beginning of the script match on the screen and Sikuli finds the images.

Revision history for this message
danne95 (bf-heroes1337) said :
#13

I still get the noore error :(

Revision history for this message
RaiMan (raimund-hocke) said :
#14

Ok, must be so ;-)

around line 189, you have to add global noore (as I mentioned in BTW at comment #4), since noore is set in lets mine().

should look like this:

def letsmine():
    global noore
    if mineiron == True:

Mind the indentation, must be the same as with the following if.

If you get indentation errors: faq 1800

Sorry, if I seem to be brutal: But if you want to manage such scripts (that are far from being programmed robust), you need some knowledge about how to write Python scripts.

Revision history for this message
danne95 (bf-heroes1337) said :
#15

Ok thanks for the help ;D.
Can you send me a link to a good site where i kan learn to write Python script?

Revision history for this message
RaiMan (raimund-hocke) said :
#16

I cannot give you any recommendations, since I never looked into such stuff.

If you have any knowledge in any programming/scripting language try to google

mylanaguage versus python

or

python tutorial

Revision history for this message
george (georgepust) said :
#17

go step by step danne thats what i do at the moment but still couldn't fix the problem :(

iron = Pattern("1330030896672.png").similar(0.74)
iron2 = Pattern("1329980545422.png").similar(0.89)
copper = Pattern("nA.png").similar(0.91)
copper2 = Pattern("1329980670115.png").similar(0.89)
bronze = Pattern("1330030708790.png").similar(0.90)
bronze2 = Pattern("1330030666167.png").similar(0.95)

gamezone = Region(0,0,1280,800)
myself = ""L.png""
waittime = 10

mineiron = True
minecopper = True
minebronze = True
minetin = True

global lastroom
global currentroom
global noore

def mine9_m23)():
    global lastroom
    global currentroom
    global noore
if gamezone.exists(Pattern("Q.png").similar(0.87), 0):
        currentroom = 1
        gamezone.click("1330056004176-1.png", 0)
        lastroom = 1
        wait (2)

if gamezone.exists(Pattern("1330057093507.png").similar(0.88), 0):
        currentroom = 2
        if lastroom == 1:
            gamezone.click(Pattern("1330056261522.png").similar(0.83), 0)
            lastroom = 2
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

if gamezone.exists(Pattern("Fr.png").similar(0.83), 0):
        currentroom = 3
        if lastroom == 2:
            gamezone.click("1330056367548.png", 0)
            lastroom = 3
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

if gamezone.exists(Pattern("1330057294326.png").similar(0.87), 0):
        currentroom = 4
        if lastroom == 3:
            gamezone.click("1330056435973.png", 0)
            lastroom = 4
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1

if gamezone.exists(Pattern("Fr.png").similar(0.83), 0):
        currentroom = 5
        if lastroom == 4:
            gamezone.click("1330056367548.png", 0)
            lastroom = 5
            wait (2)
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1
if gamezone.exists(Pattern("1330057093507.png").similar(0.88), 0):
        currentroom = 6
        if lastroom == 5:
            gamezone.click(Pattern("1330056261522.png").similar(0.83), 0)
            lastroom = 6
        else:
            if currentroom == lastroom:
                lastroom = lastroom - 1
if gamezone.exists(Pattern("Q.png").similar(0.87), 0):
        currentroom = 1
        if lastroom == 6:
   gamezone.click("1330056004176-1.png", 0)

def fightover():
    if gamezone.exists(Pattern("Close-2.png").similar(0.86), 0):
        gamezone.click(Pattern("Close-2.png").similar(0.86), 0)
        wait(2)
    else:
        pass

def fight(): #Fight module
    while gamezone.exists(Pattern("1330033988160.png").similar(0.76), 0):
        if gamezone.exists("IIFr.png", 0):
            if gamezone.exists(Pattern("1330034086473.png").similar(0.78), 0):
                gamezone.click(Pattern("1330034086473.png").similar(0.78), 0)
            gamezone.click("IIFr.png", 0)
            wait(1)
        else:
            if gamezone.exists(myself, 0):
                type(skill1) #hotkey for skill to kill resource protector.
                gamezone.hover(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20)) #Hover over the protector so we can see if it possible target it.
                if gamezone.exists(Pattern("1326907907791.png").similar(0.67), 0): # if we see this image while hoverin target that mean we cant target.:
                    type(skill2) #now lets try another skill with longer range or skill that doesnt need line of sight.
                    gamezone.hover(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20)) # hover over protector again and lets see if we can target it.
                    if gamezone.exists(Pattern("1326907907791.png").similar(0.67), 0): #Still not range or vision to target.
                        type(skill3)
                        if gamezone.exists("1330034753277.png", 0): #(resim degisti)
                            gamezone.click("1330034753277.png", 0) #(resim degisti)
                            wait(0.5)
                            gamezone.click("1330034845030.png", 0) #lets skip turn and wait protector come closer. (resim degisti)
                        else:
                            pass
                    else:
                        if gamezone.exists(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20), 0):
                            gamezone.click(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20), 0) #click ptotector with skill in hotkey 1
                            wait(1)
                            fightover()
                            wait(2)
                            pass
                else: #if we have vison and not seeing that not allowed image we gona do targeting.
                    if gamezone.exists(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20), 0):
                        gamezone.click(Pattern("1327510524930.png").similar(0.76).targetOffset(0,20), 0) #click ptotector with skill in hotkey 3
                        wait(1)
                        fightover()
                        wait(1)
                        pass
                        #(bunun altindaki bikac satir silindi)
def levelup():
    if gamezone.exists(Pattern("1326218415060-1.png").similar(0.84), 0): #Level up
        gamezone.click(Pattern("1326218415060-2.png").similar(0.84), 0) #Close level up window
    else:
        pass

def letsmine(): #Mining module
    global noore
    if mineiron == True:
        if gamezone.exists(iron):
           noore = 0
           gamezone. click(iron, 0)
           wait(waittime)
           levelup()
    if mineiron == True:
        if gamezone.exists(iron2):
            noore = 0
            gamezone.click(iron2, 0)
            wait(waittime)
            levelup()
    #COPPER
    if minecopper == True:
        if gamezone.exists(copper):
            noore = 0
            gamezone.click(copper, 0)
            wait(waittime)
            levelup()
    if minecopper == True:
        if gamezone.exists(copper2):
            noore = 0
            gamezone.click(copper2, 0)
            levelup()
            wait(waittime)
    #BRONZE
    if minebronze == True:
        if gamezone.exists(bronze):
            noore = 0
            gamezone.click(bronze, 0)
            wait(waittime)
            levelup()
    if minebronze == True:
        if gamezone.exists(bronze2):
            noore = 0
            gamezone.click(bronze2, 0)
            wait(waittime)
            levelup()
        else:
            noore = 1
            pass

        while gamezone.exists("1330043783741.png",0): #while this image on screen it keep looping this.(resim degisti/ne oldugunu bilmiyorum?)
    letsmine() #Start mining module
    fight() #run fight module
    if gamezone.exists(Pattern("Close-2.png").similar(0.86), 0):
        fightover()
         else:
        if noore == 1:
            mine9_m23()
        pass

---------------------
[error] Stopped
[error] Error message: IndentationError: ('unindent does not match any outer indentation level', ('C:\\Users\\XXXXX\\AppData\\Local\\Temp\\sikuli-tmp6309182323911585919.py', 84, 3, ' gamezone.click("1330056004176-1.png", 0)\n'))