Help me debug a function?

by Simimi

Back to Mechanic's Corner.

Unknown2007-07-23 13:28:57
You probably need to make a trigger that sets the balance to true, in response to that message that tells you that you are allowed to drink healing,mana,bromides again.
Unknown2007-07-23 13:44:52
How are you triggering the changing of the balances? You shouldn't rely on the "You take a sip..." type of message, for obvious reasons, and using messages like "The elixir heals and soothes..." doesn't stop sip spamming when you can't sip or just have a delay in between sending the command and sipping. Also, consider when you sip a purgative or something and nothing is cured. If you just use triggers, how will you know what you sipped and what balance was lost?

What I suggest is that you set the balance to "trying" in your script before sending the drink and set a failsafe timer that resets the balance to true if you don't sip within a second or so. If the timer fires and the balance is false, that means you saw the triggered message and everything worked fine. If it fires and the balance is "trying," however, then you failed or lagged and you might want to set the balance back to true and try sipping again.
Simimi2007-07-23 17:54:15
QUOTE(Zarquan @ Jul 23 2007, 08:44 AM) 427882
How are you triggering the changing of the balances? You shouldn't rely on the "You take a sip..." type of message, for obvious reasons, and using messages like "The elixir heals and soothes..." doesn't stop sip spamming when you can't sip or just have a delay in between sending the command and sipping. Also, consider when you sip a purgative or something and nothing is cured. If you just use triggers, how will you know what you sipped and what balance was lost?

What I suggest is that you set the balance to "trying" in your script before sending the drink and set a failsafe timer that resets the balance to true if you don't sip within a second or so. If the timer fires and the balance is false, that means you saw the triggered message and everything worked fine. If it fires and the balance is "trying," however, then you failed or lagged and you might want to set the balance back to true and try sipping again.


Well I set up the entire system to use that, since it seemed like a good idea. The autpsipper works, but only when health is dangerously low... as in, below half. Why does it only trigger then?

CODE
--*AUTOSIPPER
function autosipper ()
  if bals.potion == "curing" then
    return
  end

  if bals.potion == true then
    end
      if tonumber(var.health) <= (.50 * tonumber (var.maxhealth)) then
        Send ("drink health")
      --elseif --really bad deepwounds stuffs here, no idea what the lines and such for those are...or how to handle them, or what deepwounds --are even...
        --Send ("apply health to --wounded spot")
      elseif tonumber (var.mana) <= (.50 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.50 * tonumber (var.maxego)) then
        Send ("drink bromide")
      --elseif --more deepwounds stuff once I learn about it
        --Send ("deepwounds cure stuffs")
      elseif tonumber (var.health) <= (.65 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.65 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.65 * tonumber (var.maxego)) then
        Send ("drink bromide")
      --elseif .. --less bad deepwounds stuffs here
        --Send ("lessbad deepwounds stuffs")
      elseif tonumber (var.health) <= (.80 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.80 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.80 *  tonumber (var.maxego)) then
        Send ("drink bromide")
      elseif tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.90 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.90 * tonumber (var.maxego)) then
        Send ("drink bromide")    
      end
        
end;



ASIDE: I have a login trigger that sets various table values to "true" so that they start that way. This is fine and all, but is there a better way to do it?
Simimi2007-07-25 21:02:12
Well I got quite a bit more done, but the autosipper still does not wish to sip. I have the function triggering off the prompt and I reorganised the numbers in numerical order of importance (90 is at the top of the function now) and still nothing. Triggers are firing and moving balances around just fine... Ideas?
Unknown2007-07-25 21:28:36
Your numbers were fine in the order you had them, but I'm wondering about the "end" right after the "if bals.potion == true then" line. Could that be your problem?
Simimi2007-07-26 01:00:11
After removing said end it still does nothing, but the triggers are firing and everything...
CODE
function autosipper ()
  if bals.potion == "curing" then
    return
  end

  if bals.potion == true then
      if tonumber(var.health) <= (.90 * tonumber (var.maxhealth)) then
        Send ("drink health")
      --elseif --really bad deepwounds stuffs here, no idea what the lines and such for those are...or how to handle them, or what deepwounds --are even...
        --Send ("apply health to --wounded spot")
      elseif tonumber (var.mana) <= (.90 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.90 * tonumber (var.maxego)) then
        Send ("drink bromide")
      --elseif --more deepwounds stuff once I learn about it
        --Send ("deepwounds cure stuffs")
      elseif tonumber (var.health) <= (.80 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.80 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.80 * tonumber (var.maxego)) then
        Send ("drink bromide")
      --elseif .. --less bad deepwounds stuffs here
        --Send ("lessbad deepwounds stuffs")
      elseif tonumber (var.health) <= (.65 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.65 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.65 *  tonumber (var.maxego)) then
        Send ("drink bromide")
      elseif tonumber (var.health) <= (.50 * tonumber (var.maxhealth)) then
        Send ("drink health")
      elseif tonumber (var.mana) <= (.50 * tonumber (var.maxmana)) then
        Send ("drink mana")
      elseif tonumber (var.ego) <= (.50 * tonumber (var.maxego)) then
        Send ("drink bromide")    
      end
    end    
end
Unknown2007-07-27 09:40:28
If you still have the problem, here are some hints to find out what is wrong. I can't see what is wrong from the code.

What I do, most of the time, when a function does not work is the following:
- First, check if the function is called.
By adding a print("Function sipper called") , at the top of the function
- Then, check if the values are as I expected:
Change it to something like: print("Function sipper called, balance =" .. tostring(bals.potion) .. ", health = " .. tostring(var.health) .. "/" .. tostring(var.maxhealth))
- Lastly, check if the function arrives at the place where it should be:
Add a print at the place which I expect that should be executed. Probably, add these just before the send("drink health") lines...

You should always try to split up the possible reasons that it does not work. So, basically what I do is first making sure that the function is in fact being called. Then that the assumptions I have about variables that are available are true, and then that the code does what I expect it would do...

And when everything works I remove all the print statement.
Simimi2007-07-29 21:42:21
Sorry it took me so long to reply to this but thanks to your print functions, I totally fixed it! Thank you so much everyone! ONE STEP CLOSER~!