Less than/More than statements.

Post Reply
Altair
Posts: 2
Joined: Wed Aug 05, 2009 1:45 pm

Less than/More than statements.

Post by Altair »

I'm in the process of playing with Mudlet, and whilst making a simple auto sipper, I've discovered that using and combination of "less than" or "more than" symbols in my if statements kills the entire loop.

To explain better; First my system captures the values of my maximum health/mana, and then works out suitable levels to sip at. Then it will capture the current values of health/mana from my prompt. Once this has been completed it will then run through the first scan, making sure that I am able to sip the health elixir and that I have balance. All of this works correctly, the issue comes when I attempt to compare my current health, with the level I want to sip at.

I use the following function:

Code: Select all

function SipScan()
  echo("Running SipScan: Sip Health set at: " .. Prompt[SipHealth] .. " Current Health is: " .. Prompt[CurrentHealth])
  if Prompt[SipHealth] >= Prompt[CurrentHealth] then
    echo("\nNeed to Sip Health")
  else
    echo("\nNo need to sip")
  end
end
Which outputs the following:
H:3186 M:3888 E:100% W:100% B:100% [csdb eb]Running Can Sip Function
CanSip Is True
Running SipScan: Sip Health set at: 4460.4 Current Health is: 3186
H:3186 M:3888 E:100% W:100% B:100% [csdb eb]
I have attempted swapping the place of "Prompt[CurrentHealth]" and "Prompt[SipHealth]" (Which are my two variables) and the sign that compares them, to no avail.

It does, however all work when I change it to an inequality as such:

Code: Select all

function SipScan()
  echo("Running SipScan: Sip Health set at: " .. Prompt[SipHealth] .. " Current Health is: " .. Prompt[CurrentHealth])
  if Prompt[SipHealth] ~= Prompt[CurrentHealth] then
    echo("\nNeed to Sip Health")
  else
    echo("\nNo need to sip")
  end
end
Outputs:
H:3186 M:3888 E:100% W:100% B:100% [csdb eb]Running Can Sip Function
CanSip Is True
Running SipScan: Sip Health set at: 4460.4 Current Health is: 3186
Need to Sip Health
H:3186 M:3888 E:100% W:100% B:100% [csdb eb]
Any ideas? Suggestions?

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

Re: Less than/More than statements.

Post by Heiko »

if Prompt[SipHealth] ~= Prompt[CurrentHealth] then
This has nothing to do with the <= or >= operators in Lua. Your promblem most likely stems from a simple Lua syntax error.
Prompt[CurrentHealth] means: The value of the table element defined by the value of the variable CurrentHealth. This doesn't make sense unless you have defined zilllions of keys. What you'd probably want is Prompt["CurrentHealth"] which will get you the value of the key "CurrentHealth" in the table Prompt.

Another pitfall is the automatic conversion between strings and numbers in Lua. Use the tonumber( myVar ) function to make sure that Lua interprets your variable as a number and not as a string if you use the >= and <= operators and run into problems. A typcial case where this is necessary is when you parse information from the built in Mudlet tables matches[n] or multimatches[n][m]. The reason is that Mudlet passes the values as strings to Lua because Mudlet has no clue wheather a regular expression context has to be interpreted as a number or text. Consequently, the user has to exlicitely convert numbers (e.g. from patterns like (\d+) ) with code such as this: myHealth = tonumber( matches[2] )
or if tonumber( matches[2] ) >= maxHealth then ...

Post Reply