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
➜ Lua
➜ SetVariable and accessing variable
SetVariable and accessing variable
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Reyth
(4 posts) Bio
|
Date
| Tue 06 Jul 2010 06:38 PM (UTC) |
Message
| I'm making a basic sipper for Achaea, and I've got a trigger that looks like this:
SetVariable("currenthealth", "%1")
SetVariable("currentmana", "%2")
SetVariable("currentstate", "%3")
if @currenthealth < @siphealth and @sipbalance==1 then
if @sm==1 and @currentmana < @sipmana then
Send("sip mana")
SetVariable("sipbalance", "2")
Note(@sipbalance)
else
Send("sip health")
SetVariable("sipbalance", "2")
Note(@sipbalance)
end
end
if @currentmana < @sipmana and @sipbalance==1 then
Send("sip mana")
SetVariable("sipbalance", "2")
end
For some reason, when it triggers and sends "sip health" then sets sipbalance to 2, it still notes sipbalance as 1. It seems that whenever I SetVariable then access the same variable inside a trigger, the variable hasn't actually changed yet. Is this normal, or am I missing a checkbox somewhere?
Thanks, this is my first post, but everyone on here has already helped me a lot indirectly :).
| Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Tue 06 Jul 2010 06:43 PM (UTC) |
Message
| First off, in Lua, numbers and strings cannot be directly compared. So when you use SetVariable("sipbalance", "2"), you're setting it to the string "2", but you later compare it to a number, so that won't work.
But to answer your main question:
Quote: It seems that whenever I SetVariable then access the same variable inside a trigger, the variable hasn't actually changed yet.
If I remember correctly, variable expansion occurs when the trigger is "entered" by MUSHclient. So the string @sipbalance is expanded only once. If you want to get the always up-to-date version, you need to use the GetVariable function. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Reyth
(4 posts) Bio
|
Date
| Reply #2 on Tue 06 Jul 2010 07:01 PM (UTC) |
Message
| Thanks, I'll try that right away. For some reason, it works even though I'm comparing it to a number. I'll let you know how GetVariable works out in a moment. | Top |
|
Posted by
| Reyth
(4 posts) Bio
|
Date
| Reply #3 on Tue 06 Jul 2010 07:13 PM (UTC) |
Message
| GetVariable seems to fix it, but I think I need to make sure my flow is correct. I did get the number/string disagreement error you mentioned now, so I simply put tonumber before every GetVariable. Will this cause me problems later? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #4 on Tue 06 Jul 2010 07:37 PM (UTC) |
Message
| It shouldn't cause you trouble, as long as you are consistent. The MUSHclient variable system only allows strings, so you have to convert numbers to strings going in (using tostring) and strings to numbers going out (using tonumber). Now, you might not actually need numbers, in which case you could just use strings everywhere. (I'm not sure if you're using it as a counter, or as a state indicator.) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #5 on Tue 06 Jul 2010 08:45 PM (UTC) |
Message
|
David Haley said: convert numbers to strings going in (using tostring)
Lua coerces numbers to strings on the fly when circumstances allow it, so you don't need to use tostring in this case.
@Reyth - If you're using the variable more than once, I'd use tonumber() just once and assign it to a local variable:
-- totally arbitrary example
local myvariable = tonumber(GetVariable("foo"))
if myvariable > 1 then
SetVariable("foo", myvariable + 1)
end
I can see you probably wanting to do this with the sipbalance variable. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #6 on Tue 06 Jul 2010 09:05 PM (UTC) |
Message
|
Quote: Lua coerces numbers to strings on the fly when circumstances allow it
Well, if we're getting technical about it ;) no. For example, strings are not coerced for equality testing (which is a problem that was run into in this thread).
Also, in this instance, it is more likely the C API binding for SetVariable that does the conversion explicitly with lua_isstring and lua_getstring (both of which accept numbers and convert to strings).
The manual section:
http://www.lua.org/manual/5.1/manual.html#2.8
explains in detail when coercion takes place during arithmetic operations.
The above said: I mentioned explicit conversion for the sake of, well, explicitness, so that consistency is not only implied but also visual. So yes, I told a little white lie. :-) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #7 on Tue 06 Jul 2010 09:51 PM (UTC) |
Message
| This has a subtle flaw:
SetVariable("currentmana", "%2")
...
if @currentmana < @sipmana and @sipbalance==1 then
MUSHclient expands out things like @currentmana *before* executing the script. Thus the change to currentmana (from wildcard 2) is too late.
What would work in this particular case is this:
if %2 < @sipmana and @sipbalance==1 then
In this case the %2 is expanded at the start, but that is OK, as it doesn't change during the script.
There is something to be said for sticking to Lua variables, eg.
currentmana = %2
sipmana = tonumber (GetVariable ("sipmana"))
...
if currentmana < sipmana then
Now you don't need to worry as we aren't chopping and changing between MUSHclient variables and Lua variables. Of course the remarks about comparing numbers to strings still apply. If you know %2 is a number and not a string, you don't need to quote it.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #8 on Tue 06 Jul 2010 11:15 PM (UTC) |
Message
|
David Haley said:
Quote: Lua coerces numbers to strings on the fly when circumstances allow it
Well, if we're getting technical about it ;) no. For example, strings are not coerced for equality testing (which is a problem that was run into in this thread).
Which wasn't the specific scenario that I was alluding to. I also specifically said "when circumstances allow it".
David Haley said: Also, in this instance, it is more likely the C API binding for SetVariable that does the conversion explicitly with lua_isstring and lua_getstring (both of which accept numbers and convert to strings).
Well, yes. If you use lua_getstring on a number, it automatically converts it to a string for you. I'm not sure how that invalidates my point.
David Haley said: The above said: I mentioned explicit conversion for the sake of, well, explicitness, so that consistency is not only implied but also visual. So yes, I told a little white lie. :-)
For the sake of completeness, I thought I'd mention the implicit coercion as well. I wasn't arguing against you, I was supplementing your insight. ;) |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #9 on Wed 07 Jul 2010 03:51 AM (UTC) |
Message
| I wasn't trying to "invalidate" your point. I was making explicit (hehe) the somewhat vague condition of "when circumstances allow it" (why addition and not equality, after all?) or scenarios that are "alluded to" rather than given specifically. I think it's ok to be slightly hand-wavy when speaking in broad terms (or when one gives a condition that is stronger than necessary, such as always explicitly converting strings to integers), but if you get down to the buckets and bolts it's important to be as exact as possible. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | 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.
31,354 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top