ATCP demo scripts

Share your scripts and packages with other Mudlet users.
User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: ATCP demo scripts

Post by Heiko »

CharVitals(event, arg)

You should use simple tracing to find out where you error is i.e. some code; echo("trace#1\n") some more code; echo("trace#2").
This is usually the fastest way to get to your problem which usually is about finding uninitialzed or wrongly used variables.

Ilithyia
Posts: 43
Joined: Wed Mar 10, 2010 11:04 pm

Re: ATCP demo scripts

Post by Ilithyia »

I'm sorry. I'm a super-newb when it comes to this stuff. Could someone point me to a resource on tracing (even if it's just mentioning Google search terms - 'tracing lua' doesn't come up with much that's useful.)? Is 'trace' actual code or just terminology for a method?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: ATCP demo scripts

Post by Heiko »

Tracing is just a common debugging method. You print some notes in your script to see how far it goes until some error happens and execution stops.

echo("trace#1\n")
some code
echo("trace#2\n")
some more code
echo("trace#3\n")

Then you simply watch your screen and see where the script stops e.g. if you get:

trace#1
trace#2

Then you know that the error must happen after echo("trace#2\n") because echo("trace#3\n") isn't executed.
NOTE: In general there are 2 main types of errors in Lua: 1.) compile time errors (easy to debug because they are usually syntax errors) and 2.) runtime errors (harder to debug because the code does compile and is syntacitcally correct, but when it is actually run it causes some error at runtime that usually has to do with wrongly initialized variables, wrong parameters etc..)) Lua just stops script execution whenever a runtime error occurs. That's why tracing is effective to see how far it gets until it stops.

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: ATCP demo scripts

Post by naftali »

the way to call this event directly would be "raiseEvent("CharVitals",atcp.CharVitals)" or if you prefer instead of referencing atcp.CharVitals you can write your own string so long as it matches the rex.

When I get frustrated with such a thing I often just start putting "echo("boing")" into my script and moving it gradually down - when I stop seeing boing, I've passed the (first) error. If I don't then the script is working, just not like I want it to.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: ATCP demo scripts

Post by Heiko »

naftali wrote:the way to call this event directly would be "raiseEvent("CharVitals",atcp.CharVitals)"
No, that way you raise a CharVitals event yourself and that's not what you want.
To call an event handler function directly you'll have to do a direct function call instead of raising an event e.g.

CharVitals( ("CharVitals",atcp.CharVitals) ) if your event handler function for the event CharVitals is called CharVitals( event, arg ).

Ilithyia
Posts: 43
Joined: Wed Mar 10, 2010 11:04 pm

Re: ATCP demo scripts

Post by Ilithyia »

Using this tracing scheme:

Code: Select all

status = {}

function CharVitals(event, arg)
      local r = rex.new( "NL:(\\d+)/100 H:(\\d+)/(\\d+) M:(\\d+)/(\\d+) E:(\\d+)/(\\d+) P:(\\d+)/(\\d+) N:(\\d+)/(\\d+) W:(\\d+)/(\\d+)" )
      if r:match(atcp.CharVitals) then
         status.next_level,
         status.current_health, status.max_health, 
         status.current_mana, status.max_mana, 
         status.current_ego, status.max_ego, 
         status.current_power, status.max_power, 
         status.current_endurance, status.max_endurance, 
         status.current_willpower, status.max_willpower = r:match(atcp.CharVitals)
      end

siphealth = tonumber(status.max_health)*0.75
sipmana = tonumber(status.max_mana)*0.75
sipego = tonumber(status.max_ego)*0.75
berryhealth = tonumber(status.max_health)*0.50
berrymana = tonumber(status.max_mana)*0.50

currenthealth = tonumber(status.current_health)
currentmana = tonumber(status.current_mana)
currentego = tonumber(status.current_ego)

display(status)

echo("\nsiphealth = " .. siphealth)
echo("\nsipmana = " .. sipmana)
echo("\nsipego = " .. sipego)
echo("\nberryhealth = " .. berryhealth)
echo("\nberrymana = " .. berrymana)

echo("\ncurrenthealth = " .. currenthealth)
echo("\ncurrentmana = " .. currentmana)
echo("\ncurrentego = " .. currentego)
echo("\n\nhealbalance = " .. healbalance)
echo("\nberrybalance = " .. berrybalance)

if healbalance == 1 then
echo("\ntrace #1\n")
      if currenthealth < siphealth then
echo("\ntrace #2\n")
            send("sip health")
echo("\ntrace #3\n")
            healbalance = 0.5
echo("\ntrace #4\n")
      elseif currentmana < sipmana then
echo("\ntrace #5\n")
            send("sip mana")
echo("\ntrace #6\n")
            healbalance = 0.5
echo("\ntrace #7\n")
      elseif currentego < sipego then
echo("\ntrace #8\n")
            send("sip bromides")
echo("\ntrace #9\n")
            healbalance = 0.5
echo("\ntrace #10\n")
      end
end
end
It seems the problem starts with this line:

Code: Select all

if currenthealth < siphealth then
Perhaps currenthealth and siphealth requires initializing? I thought that was essentially done earlier in the script when I set those variables equal to the status table values.

Another problem. I have an alias 'atcp' that calls this function. When I use the alias, the function runs through all the echoes to check the parsing and up to Trace #1 successfully. However, the script fails to fire at all when called by the event handler. Every time an event is raised, the debugger gets an error message and the function doesn't even fire through the echoes like it does when I call it through the alias. The Registered Event Handler is 'CharVitals'.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: ATCP demo scripts

Post by Heiko »

Then put some more traces to find out what makes the script stop when called by an event handler.

Ilithyia
Posts: 43
Joined: Wed Mar 10, 2010 11:04 pm

Re: ATCP demo scripts

Post by Ilithyia »

Alright, here's the new tracing scheme:

Code: Select all

status = {}
echo("\ntrace #1\n")

function CharVitals(event, arg)
echo("\ntrace #2\n")
      local r = rex.new( "NL:(\\d+)/100 H:(\\d+)/(\\d+) M:(\\d+)/(\\d+) E:(\\d+)/(\\d+) P:(\\d+)/(\\d+) N:(\\d+)/(\\d+) W:(\\d+)/(\\d+)" )
echo("\ntrace #3\n")
      if r:match(atcp.CharVitals) then
echo("\ntrace #4\n")
         status.next_level,
         status.current_health, status.max_health, 
         status.current_mana, status.max_mana, 
         status.current_ego, status.max_ego, 
         status.current_power, status.max_power, 
         status.current_endurance, status.max_endurance, 
         status.current_willpower, status.max_willpower = r:match(atcp.CharVitals)
echo("\ntrace #5\n")
      end

siphealth = tonumber(status.max_health)*0.75
echo("\ntrace #6\n")
sipmana = tonumber(status.max_mana)*0.75
echo("\ntrace #7\n")
sipego = tonumber(status.max_ego)*0.75
echo("\ntrace #8\n")
berryhealth = tonumber(status.max_health)*0.50
echo("\ntrace #9\n")
berrymana = tonumber(status.max_mana)*0.50
echo("\ntrace #10\n")
currenthealth = tonumber(status.current_health)
echo("\ntrace #11\n")
currentmana = tonumber(status.current_mana)
echo("\ntrace #12\n")
currentego = tonumber(status.current_ego)

display(status)

echo("\nsiphealth = " .. siphealth)
echo("\nsipmana = " .. sipmana)
echo("\nsipego = " .. sipego)
echo("\nberryhealth = " .. berryhealth)
echo("\nberrymana = " .. berrymana)

echo("\ncurrenthealth = " .. currenthealth)
echo("\ncurrentmana = " .. currentmana)
echo("\ncurrentego = " .. currentego)
echo("\n\nhealbalance = " .. healbalance)
echo("\nberrybalance = " .. berrybalance)

if healbalance == 1 then
echo("\ntrace #13\n")
      if currenthealth < siphealth then
echo("\ntrace #14\n")
            send("sip health")
echo("\ntrace #15\n")
            healbalance = 0.5
echo("\ntrace #16\n")
      elseif currentmana < sipmana then
echo("\ntrace #17\n")
            send("sip mana")
echo("\ntrace #18\n")
            healbalance = 0.5
echo("\ntrace #19\n")
      elseif currentego < sipego then
echo("\ntrace #20\n")
            send("sip bromides")
echo("\ntrace #21\n")
            healbalance = 0.5
echo("\ntrace #22\n")
      end
end
end
When I call the function through an alias, Trace #1 doesn't appear, but Traces #2-#13 appear, which implies there's a problem with the line

Code: Select all

if currenthealth < siphealth then
And maybe with line

Code: Select all

status = {}
However, when the script tries to fire when called by the event handler, nothing goes through. No traces.

From the debugger:

Code: Select all

new line arrived:

LUA: ERROR running script ATCP Sipper (ATCP Sipper) ERROR:
new line arrived:You have recovered equilibrium.

Trigger name=On Balance(You have recovered equilibrium.) matched.
LUA OK script On Balance (Trigger551) ran without errors
new line arrived:3156h, 4840m, 4632e, 10p, 13320en, 22439w ex-

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: ATCP demo scripts

Post by naftali »

you absolutely sure both of those are numbers and not strings?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: ATCP demo scripts

Post by Heiko »

status = {}
echo("\ntrace #1\n")


These 2 lines are not part of the event handler. They'll only be run if the script gets compiled i.e. when you save the script (click on save icon or chose some other script in which case it's autosaved)

Your handler is:

function CharVitals(event, arg)
(your code)
end

If your first echo inside the handler function - in your case that's echo #2 - doesnt fire than your handler doesnt listen to the correct event. You'll have to set up the event that you want your handler function to handle in the event handler box above the script. *AND* your event handler function has to be named exactly like the script itself (in your case CharVitals. Note, that correct spelling is important here.
Your event handler function that handles a CharVitals event can be called tomLikesCandy( event, arg ), if the script has the same name and handles the correct event.

If you cannot get it working now, post a screenshot of your trigger editor with the handler.
Either way, you are on a good way - and prolly this is a good example of how to debug event handlers in general.

Post Reply