(Solved) [Lua] Can't go over if value is more than 999

Posted by gameshogun on Mon 21 Sep 2015 07:05 PM — 4 posts, 20,063 views.

Philippines #0
MUSHclient v4.99

Trigger

<triggers>
  <trigger
   enabled="y"
   group="warning"
   ignore_case="y"
   match="^[(?P&lt;ahp&gt;\d+)\/(?P&lt;fhp&gt;\d+) (?P&lt;amana&gt;\d+)\/(?P&lt;fmana&gt;\d+) (?P&lt;amove&gt;\d+)\/(?P&lt;fmove&gt;\d+) (?P&lt;axp&gt;\d+) \|(.*?)$"
   name="status_warning"
   regexp="y"
   script="status_warning"
   sequence="100"
  >
  </trigger>
</triggers>


LUA

function status_warning (name, line, wildcards)
  local ahp = wildcards.ahp
  local axp = wildcards.axp

  if ahp < "200" then
    ColourNote ("white", "red", "WARNING - HEALTH IS LOW!")
  end

  if axp < "500" then
    ColourNote ("white", "red", "CHANGE TO LEVELING EQUIPMENT NOW!!")
  end
end


TEST:

Actual: [120/1500 100/800 100/800 2847 | PET: 100/800hp | Q SU]
wildcards: [ahp/fhp amana/fmana amove/fmove axp | *


What's happening:
If the value is 4 digits and above, the if statement always fail. The maximum I can do in this case, is 999.

So, in the example above:
* if ahp < "200" works fine because ahp = 120
* if axp < "500" fails because axp = 2847, so ColourNote gets printed when it should not.

Is there something I'm missing re: LUA?

Thank you for the assistance.
Amended on Mon 21 Sep 2015 07:08 PM by gameshogun
USA #1
Are you actually converting those variables into numbers at some point? If not you're simply comparing strings and you'll never get the behavior you expect to see.

As demonstrated http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11249
Australia Forum Administrator #2
I agree with Meerclar.

Your code should probably be:


function status_warning (name, line, wildcards)
  local ahp = tonumber (wildcards.ahp)
  local axp = tonumber (wildcards.axp)

  if ahp < 200 then
    ColourNote ("white", "red", "WARNING - HEALTH IS LOW!")
  end

  if axp < 500 then
    ColourNote ("white", "red", "CHANGE TO LEVELING EQUIPMENT NOW!!")
  end
end


Strings and numbers compare quite differently. The string "1000" is less than the string "999". (1 is less than 9).
Philippines #3
Nick Gammon said:

I agree with Meerclar.

Your code should probably be:


function status_warning (name, line, wildcards)
  local ahp = tonumber (wildcards.ahp)
  local axp = tonumber (wildcards.axp)

  if ahp < 200 then
    ColourNote ("white", "red", "WARNING - HEALTH IS LOW!")
  end

  if axp < 500 then
    ColourNote ("white", "red", "CHANGE TO LEVELING EQUIPMENT NOW!!")
  end
end


Strings and numbers compare quite differently. The string "1000" is less than the string "999". (1 is less than 9).


Ah! That's it, "tonumber". And I thought "123" was the style in Lua.

Must start reading the Lua docs, it's getting interesting.

Thank you very much to both of you! *bow*