Author Topic: [Python] Working with events  (Read 1812 times)

0 Members and 1 Guest are viewing this topic.

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
[Python] Working with events
« on: September 19, 2018, 02:23:41 PM »
0
Hello! I'm trying to figure out the best way to use an event to search for enemy players and then run a function when it finds one. I thought I'd read the API and am doing it right, but my logic must be flawed.

Code: [Select]
def emergencyRecall(ID):
    if ID != Self()
        #FindType(0x0F06, Backpack()) #invis pot
        #UseObject(FindItem())
        #Wait(100)
        recallHome()

SetEventProc(evDrawGamePlayer, emergencyRecall)

I'm getting NameError: name 'evDrawGamePlayer' is not defined.

Any advice? Thanks!!

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #1 on: September 19, 2018, 03:14:13 PM »
0
You need to use quotes, cause an event name is a string
There is no evdrawgameplayer in events. There was, but was deprecated.
Code: [Select]
EVENTS_NAMES = (
    'eviteminfo', 'evitemdeleted', 'evspeech', 'evdrawgameplayer',
    'evmoverejection', 'evdrawcontainer', 'evadditemtocontainer',
    'evaddmultipleitemsincont', 'evrejectmoveitem', 'evupdatechar',
    'evdrawobject', 'evmenu', 'evmapmessage', 'evallowrefuseattack',
    'evclilocspeech', 'evclilocspeechaffix', 'evunicodespeech',
    'evbuffdebuffsystem', 'evclientsendresync', 'evcharanimation',
    'evicqdisconnect', 'evicqconnect', 'evicqincomingtext', 'evicqerror',
    'evincominggump', 'evtimer1', 'evtimer2', 'evwindowsmessage', 'evsound',
    'evdeath', 'evquestarrow', 'evpartyinvite', 'evmappin', 'evgumptextentry',
    'evgraphicaleffect', 'evircincomingtext', 'evmessengerevent',
    'evsetglobalvar', 'evupdateobjstats'
)

Code: [Select]
# evDrawGamePlayer: deprecated, used no longer (use evDrawObject).
# evDrawObject : [ID : Cardinal] mobiles only

HUMANS = 0x0190, 0x0191

def emergency(serial):
    if serial == Self() and GetType(serial) in HUMANS:
        return
    SetEventProc('evDrawGamePlayer', None)  # turn off the handler
    recall_home()

SetEventProc('evDrawObject', emergency)  # set the handler before leaving a safe zone

Edit: change or to and.
« Last Edit: September 19, 2018, 07:00:47 PM by ZeroDX »

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #2 on: September 19, 2018, 03:43:06 PM »
0
Ahh that makes more sense! How do you stop whatever the script is doing to allow the event to play correctly? Like actually recall, and then eventually reset it.

Edit: I'll paste the code I'm trying to run so you can understand what I'm trying to do.

Quote
def emergencyRecall(serial):
    HUMANS = 0x0190, 0x0191

    if serial != Self() or GetType(serial) in HUMANS:
        return
    SetEventProc('evDrawObject', None)  # turn off the handler
    FindType(0x0F06, Backpack()) #invis pot
    UseObject(FindItem())
    Wait(100)
    ClientPrint('Player near!')
    recallHome()

I put the enable handler where you mentioned.

Edit 2: I don't think I understand how the event is supposed to work. If I run up on my miner with another character it doesn't even print 'Player near!'.
« Last Edit: September 19, 2018, 05:43:23 PM by sharpie »

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #3 on: September 19, 2018, 07:47:14 PM »
0
an example of functional programming

Code: [Select]
import datetime

HUMANS = 0x0190, 0x0191  # humans graphic
PICKAXE = 0x0E86  # a pickaxe graphic

foundSomeone = False


def evDrawObjectHandler(serial):
    global foundSomeone  # now this function can change this variable, not only read
    AddToSystemJournal('Character found!', serial=serial, graphic=GetType(serial), sep=' ', end='.')
    if serial != Self() and GetType(serial) in HUMANS:
        foundSomeone = True
        SetEventProc('evDrawObject', None)  # disable the handler
        FindType(0x0F06, Backpack()) #invis pot
        UseObject(FindItem())
        Wait(100)
        ClientPrint('Player near!')
        recallHome()


def MineTile(t, x, y, z):
    msgEnd = 'nothing to mine|mining in rock|reach the target|far away'
    msgTry = 'put|fail'
    while 1:
        WaitTargetTile(t, x, y, z)
        UseType2(PICKAXE)
        start = datetime.datetime.now()
        WaitJournalLine(start, '|'.join(msgEnd, msgTry), 5000)
        if InJournalBetweenTimes(msgEnd, start, datetime.datetime.now()) >= 0:
            return


def main():
    while 1:
        SetEventProc('evDrawObject', evDrawObjectHandler)
        recallToMine()
        mx, my = GetX(Self()), GetY(Self())
        tiles = GetStaticTilesArray(mx-2, my-2, mx+2, my+2, WorldNum(), range(1339, 1359))  # find all mining spots near the character
        for tile in tiles:
            MineTile(*tile)
            if foundSomeone:
                return  # exit
        recallHome()
       

if __name__ == '__name__':
    main()

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #4 on: September 19, 2018, 08:37:25 PM »
0
So does execution jump to the event handler and stop executing the other code? Or does the code keep working?

Your code looks like when the handler is executed, the loop doesn't break. Is this true?

Right now, with my script set up the same way, it still freezes once it sees any kind of character. There are monsters nearby that are totally stopping my script in its tracks!
« Last Edit: September 19, 2018, 10:18:27 PM by sharpie »

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #5 on: September 20, 2018, 12:26:51 AM »
0
for each script thread, an event hander will works in the same thread. Each stealth-function checks new events and runs handlers if required, when a handler will returns a control, the function will resumes. While handler is running, another events may comes, so if your handler is more than 2-3 lines, you need to unset it.

In this code handlers will never be called
Code: [Select]
import time

def f(*args):
    pass

SetEventProc('evtimer1', f)
while 1:
    time.sleep(1)  # this is a non-stealth function

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #6 on: September 20, 2018, 02:36:36 AM »
0
That makes sense. Is there anyway to handle seeing 5-6 non human mobiles at once and it not break the script?

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #7 on: September 20, 2018, 03:40:09 AM »
0
it does not break, when the handler returns control, the script continues from the same line as the handler was called

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #8 on: September 21, 2018, 01:29:13 PM »
0
So, correct me if I'm wrong, but this should be working perfectly:

Code: [Select]
foundSomeone = False
HUMANS = [0x0190, 0x0191]

def evDrawObjectHandler(serial):
    global foundSomeone
    AddToSystemJournal(serial)
    AddToSystemJournal('Character found!', serial=serial, graphic=GetType(serial), sep=' ', end='.')
    if serial != Self() and GetType(serial) in HUMANS:
        foundSomeone = True
        SetEventProc('evDrawObject', None)  #disable the handler
        AddToSystemJournal('Stopped handler')
       
def main():
    while True:
        SetEventProc('evDrawObject', evDrawObjectHandler)
        if foundSomeone:
            AddToSystemJournal('Found someone!')
            return

main()

It's just looping and not finding anyone, the types are correct, I've double checked.

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #9 on: September 21, 2018, 02:22:20 PM »
0
Code: [Select]
def object_handler(serial):
    AddToSystemJournal('Character found!', serial=serial, graphic=GetType(serial), sep=' ', end='.')

def speech_handler(*args):
    AddToSystemJournal(*args)


def main():
    SetEventProc('evDrawObject', object_handler)
    SetEventProc('evSpeech', speech_handler)
    Connect()
    while 1:
        Wait(100)


if __name__ == "__main__":
    main()
Code: [Select]
01:20:15:950 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:15:952 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:15:953 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:15:953 [Demise 1]: Character found! serial=9767 graphic=400.
01:20:15:954 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:15:956 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:15:957 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:17:695 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:17:697 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:17:698 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:17:700 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:17:702 [Demise 1]: Character found! serial=9767 graphic=400.
01:20:17:703 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:17:704 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:17:706 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:17:707 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:17:708 [Demise 1]: Character found! serial=9767 graphic=400.
01:20:19:667 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:19:690 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:19:692 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:19:694 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:19:695 [Demise 1]: Character found! serial=9767 graphic=400.
01:20:19:697 [Demise 1]: Character found! serial=9767 graphic=400.
01:20:19:860 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:21:685 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:21:686 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:21:687 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:21:689 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:21:690 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:21:691 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:23:717 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:23:719 [Demise 1]: Character found! serial=127053 graphic=401.
01:20:23:720 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:23:721 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:25:714 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:25:716 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:25:717 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:25:719 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:25:720 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:27:743 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:27:744 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:27:756 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:27:758 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:29:812 [Demise 1]: Character found! serial=479295 graphic=401.
01:20:29:813 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:29:815 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:29:816 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:29:978 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:29:980 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:30:366 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:31:811 [Demise 1]: Character found! serial=479295 graphic=401.
01:20:31:813 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:31:814 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:31:816 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:32:299 [Demise 1]: Character found! serial=479291 graphic=400.
01:20:32:301 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:32:695 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:32:857 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:33:062 [Demise 1]: Character found! serial=1616794 graphic=400.
01:20:33:842 [Demise 1]: Character found! serial=479295 graphic=401.
01:20:33:844 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:35:844 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:35:846 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:37:909 [Demise 1]: Character found! serial=479295 graphic=401.
01:20:37:911 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:37:912 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:37:914 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:39:918 [Demise 1]: Character found! serial=479295 graphic=401.
01:20:39:920 [Demise 1]: Character found! serial=479292 graphic=400.
01:20:39:921 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:39:923 [Demise 1]: Character found! serial=51438 graphic=401.
01:20:41:975 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:41:976 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:41:989 [Demise 1]: Character found! serial=479289 graphic=400.
01:20:44:042 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:44:044 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:46:106 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:48:109 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:48:111 [Demise 1]: Character found! serial=52354 graphic=401.
01:20:50:162 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:50:164 [Demise 1]: Character found! serial=127054 graphic=400.
01:20:52:165 [Demise 1]: Character found! serial=50405 graphic=401.
01:20:52:166 [Demise 1]: Character found! serial=50405 graphic=401.

Offline sharpieTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • sharpie has no influence.
  • Gender: Male
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #10 on: September 22, 2018, 02:29:50 PM »
0
What is the purpose of the Wait(100)? I noticed it does not work if there isn't a wait.

When I add the event in my mining script, the script will only loop through 1-2 tiles before it just stands still doing nothing. If I disable the handler, it works flawlessly. I'm wondering if the two issues are related.

Offline ZeroDX

  • Stealth Developer
  • Moderator
  • *****
  • Posts: 19
  • Activity:
    0.4%
  • Reputation Power: 1
  • ZeroDX has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: [Python] Working with events
« Reply #11 on: September 24, 2018, 03:59:25 AM »
0
What is the purpose of the Wait(100)? I noticed it does not work if there isn't a wait.
It needs for saving some cpu time.
When I add the event in my mining script, the script will only loop through 1-2 tiles before it just stands still doing nothing. If I disable the handler, it works flawlessly. I'm wondering if the two issues are related.
Can you post the script?

Tags: