Comparison breaking because of low number?

Posted by Faedara on Mon 22 Nov 2010 01:55 PM — 8 posts, 35,689 views.

#0
This is my timer as it is now:


<timers>
  <timer enabled="y" second="0.10" offset_second="0.00"    send_to="12"
group="AutoSipperAlpha" >
  <send>if GetVariable ("sip_hp") &gt; GetVariable ("cur_hp") and GetVariable ("sip_bal") == "1" then
  Send ("Sip Health")
  SetVariable ("sip_bal", "0.5")
  EnableTimer ("sip_bal_check", true)
end</send>

  </timer>
</timers>


This is the set of variables it's currently using:
sip_hp = 1512.8
cur_hp = (1-1891)


This system works perfectly fine until cur_hp drops below 1000. Is there a reason for this, and is there a workaround? Or am I missing an entirely different problem. Also, does anyone here know how I would get into the MUSH clan on Achaea?
#1
Additional notes: the timer starts working again as soon as health is back over 1000

And yes, I'm that girl you all love to hate. I think I'm setting the record for most annoying questions/problems/failures on the front page at one time.
#2
I think you are comparing strings not numbers.

If you are using Lua, just do tonumber(GetVar.....) > tonumber(GetVar.....)
Australia Forum Administrator #3
Faedara said:

And yes, I'm that girl you all love to hate. I think I'm setting the record for most annoying questions/problems/failures on the front page at one time.


Not at all. It is great to see intelligent questions asked, it shows you are attempting to solve your own problems (which is more than some people do). You also post enough code, and explain the problem, to assist in helping you.
Australia Forum Administrator #4
Anyway, I agree with Seriley - string comparisons give strange results with numbers. For example:


print ("1000" < "900") --> true


For fleeting variables (your HP change all the time, right?) you hardly need MUSHclient variables. Use Lua variables, which can hold numbers.

In another post I did a tonumber on the ATCP variables, so then stick with the Lua version.

For example:


hp, max_hp               = tonumber (stats.health),     tonumber (stats.maxhealth)


OK, just use hp and max_hp - no need to make things harder by turning them into MUSHclient variables with SetVariable, and then turning them back with GetVariable, and then doing a tonumber on them. Why go through all the pain?
USA #5
I can't find anything else particularly wrong here, so I think Seriley has it. Here's a helper function you can add to your script file (or into a plugin's script section) to make it slightly more obvious:

function GetNumericVariable(name)
  return tonumber(GetVariable(name))
end


Faedara said:
Also, does anyone here know how I would get into the MUSH clan on Achaea?

Sure, poke me (if I'm actually online) or anyone in the CLHELP MUSH clan scroll. If your Achaea name is different from here, it would help to say what it is so we can look for you too.
#6
Alright, in that order...

Thanks Seriley, I'll post again if that fixes the problem. I forget that Variables aren't exactly what you put into them.

Thanks Nick, I do try my best to understand what I'm doing, I feel it's better to ask a hundred questions and move a step at a time than take a hundred steps and then whine about how nothing worked and I have to start from the beginning.

Lua variable would only work for one of those number variables I need to have (cur_hp), because (max_hp), (sip_hp), and (sip_per) should save over closing the client. Imagine logging in and you're suddenly in the middle of a fight, but your AutoSipper no longer has your variables, so you have to manually heal and fight when you got the Sipper to avoid such situations.
Also, this is the Alpha version of my sipper, so nothing is in Script yet. In fact, it's all triggers and timers right now. I need to build a stable working version based on what I know before I build this scripted version based on what I'm learning.

Thank you all for the help, I'm going back to work on it.
Australia Forum Administrator #7
Faedara said:

Imagine logging in and you're suddenly in the middle of a fight, but your AutoSipper no longer has your variables, so you have to manually heal and fight when you got the Sipper to avoid such situations.


Well that is exactly why you serialize your Lua variables at world open, and world save.

Template:post=4960
Please see the forum thread: http://gammon.com.au/forum/?id=4960.


On world startup, you convert your saved MUSHclient variables into Lua variables. At world save, you convert them back so they are saved for next time. Meanwhile, for the hours in-between you have the flexibility of using Lua variables. The serialization process described in that thread keeps the "type" of the variables, so a Lua number stays a number.