Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ General
➜ Why doesn't this work?
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Curious2
(47 posts) Bio
|
Date
| Mon 02 Feb 2009 09:07 PM (UTC) |
Message
| It keeps saying I am trying to compare a string to number...
[string "Plugin"]:752: attempt to compare number with string
751 : local halfmana = stats.maxmana / 2
752*: if stats.mana > halfmana == false or
The values in the table are numbers not strings?
This should store them as numbers right?
_, _, stats.health, stats.maxhealth, stats.mana, stats.maxmana, stats.endur, stats.maxendur, stats.will, stats.maxwill, stats.lvl, stats.maxlvl = string.find (text, "H:(%d+)/(%d+) M:(%d+)/(%d+) E:(%d+)/(%d+) W:(%d+)/(%d+) NL:(%d+)/(%d+)")
Also are you able to have two stats tables in differen plugins? | Top |
|
Posted by
| Curious2
(47 posts) Bio
|
Date
| Reply #1 on Tue 03 Feb 2009 12:46 AM (UTC) |
Message
| Well...it is saving the values as strings and not numbers. Even if I use tonumber it still says the type is string...
How can I force them to save as numbers?
For example, I made a test strigger and in a script looped through and printed the type of each value and it showed
string
number
number
number
string
string
number
number
number
string
string
string
string
string
string
string
The strings are what is being saved by the string.find.
| Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Tue 03 Feb 2009 08:57 AM (UTC) |
Message
| string.find and string.match always return strings, regardless of the regular expression.
Just because you search for %d+ doesn't make string.find do a conversion into numbers. After all, something that matches %d+ might be:
459787947495673936760979573462558790262414
That wouldn't be storable in a number (at least, not without throwing away precision).
One approach is to save as temporary variables and then convert, eg.
local health, maxhealth, mana, maxmana, endur, maxendur, will, maxwill, lvl, maxlvl = string.match (text, "H:(%d+)/(%d+) M:(%d+)/(%d+) E:(%d+)/(%d+) W:(%d+)/(%d+) NL:(%d+)/(%d+)")
stats.health = tonumber (health)
stats.maxhealth = tonumber (maxhealth)
-- and so on
Note that string.match is similar to string.find, but doesn't return the the start and end positions. So if you are doing this:
_, _, a = string.find (str, "(%a+)")
You may as well do this instead, which is shorter and neater:
a = string.match (str, "(%a+)")
Another approach is to convert when required, eg.
local halfmana = stats.maxmana / 2
if tonumber (stats.mana) > halfmana == false or
Note that Lua realizes to convert something like "stats.maxmana / 2" into a number for you, so it didn't fail on that line.
Also I don't particularly like this, it looks a bit clumsy:
if stats.mana > halfmana == false then
Comparing "== true" or "== false" is rarely necessary. This looks better:
if not stats.mana > halfmana then
Or why not just put it the right way around?
if stats.mana <= halfmana then
Code like:
if stats.mana > halfmana == false then
... just makes me stop and wonder what is being tested for really. Whereas:
if stats.mana <= halfmana then
... is much more obviously testing if you have less than half your mana. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Curious2
(47 posts) Bio
|
Date
| Reply #3 on Wed 04 Feb 2009 05:06 AM (UTC) |
Message
| Haha well that code was from a plugin I downloaded from here. As far as the comparison, I am not sure why I did that because all of the others I put exactly like you have. Probably lack of sleep I guess. I feel retarded now. Forgive me. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
11,556 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top