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
➜ Variable Problem
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Orogan
(23 posts) Bio
|
Date
| Sun 09 Nov 2008 03:21 PM (UTC) |
Message
| if I got a variable a wich could have the value "glow"
I want to do something like:
SetVariable ((a), "1")
to set the variable glow to 1
Tryed using every thing I could think of but it just won't work.
How would I go about this? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Sun 09 Nov 2008 04:31 PM (UTC) |
Message
| Is "glow" meant to be a MUSHclient-space variable, or a Lua-space variable?
Also, it is helpful if you describe in more detail why it's not working; error messages are always useful, symptoms are useful, etc. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Orogan
(23 posts) Bio
|
Date
| Reply #2 on Sun 09 Nov 2008 04:35 PM (UTC) |
Message
| Lua space variable.
And as far as error go I think I had them all ranging from
parcing errors over 'doing nothing at all' to 'ambiguous syntax (function call x new statement)' depending on the stuff I try. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #3 on Sun 09 Nov 2008 04:41 PM (UTC) |
Message
| Ah... well, SetVariable is a function to set MUSHclient-space variables. There are many ways to do this, that are more or less complicated depending on how complicated you want it to be, but here's one way:
loadstring(a .. " = 1")()
This loads up the string "glow = 1" as a function, and then executes it to make the assignment.
You could also try:
_G[a] = 1
which talks directly to the table of Lua global variables. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Orogan
(23 posts) Bio
|
Date
| Reply #4 on Sun 09 Nov 2008 04:51 PM (UTC) |
Message
| The first solution worked.:)
To make more clear what i wanted to do:
list = "glow, hum, res"
for f in string.gmatch (list, "%a+") do
loadstring(f.."=1")()
end
Just to be sure this is the correct way to go about it?
Thanks again for the help. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #5 on Sun 09 Nov 2008 05:02 PM (UTC) |
Message
| That looks reasonable to me, although I might make it an explicit list, something like:
list = {"a", "b", "c"}
for _, varname in ipairs(list) do
loadstring(varname .. " = 1")()
end
If you know the list ahead of time, it's easier to skip the splitting phase and just represent it as a list. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Orogan
(23 posts) Bio
|
Date
| Reply #6 on Sun 09 Nov 2008 05:13 PM (UTC) |
Message
| I grab the list in that format from the mudoutput so to make it into a table would be unnecessary I would think. Unless there's a good reason for it that I'm newbie unaware of? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #7 on Sun 09 Nov 2008 05:29 PM (UTC) |
Message
| Ah -- I didn't realize that you only get the list as a string. (I wasn't sure if it was a known list of things to check, or something from the MUD.) In that case, what you did with string.gmatch makes perfect sense. |
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 #8 on Sun 09 Nov 2008 09:46 PM (UTC) Amended on Sun 09 Nov 2008 09:58 PM (UTC) by Nick Gammon
|
Message
| Personally I don't like the loadstring approach, as that is invoking the Lua compiler to do something which is really a runtime operation.
At the very least, do this:
for f in string.gmatch (list, "%a+") do
_G [f] = 1
end
That at least doesn't need a compile phase, which is slower. Better again, use a boolean:
for f in string.gmatch (list, "%a+") do
_G [f] = true
end
However all this looks messy. What happens if the flag from the MUD happens to be called "print"? Then you have clobbered your global print function with the number 1. Or if a flag is called "string" then your script stops working because string.gmatch won't work any more as string has changed from a function to 1.
Far, far better to make a table of flags, like this:
flags = {}
for f in string.gmatch (list, "%a+") do
flags [f] = true
end
Now you can just test flags, eg.
if flags.hum then
-- do something
end -- if
Also you can now iterate through all the flags, eg.
print "flags set = "
for k, v in pairs (flags) do
print (k)
end -- for
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Orogan
(23 posts) Bio
|
Date
| Reply #9 on Mon 10 Nov 2008 08:23 AM (UTC) |
Message
| If used what you said Nick.
Works,exept I use 1 and 0 instead of boolean.
The reason is I'm using the values in this:
assert (con:execute(string.format([[INSERT INTO lore
VALUES ('%s', '%d')]],(p.Type),(flags.hum))))
(shorter version)
To pass the values to a database.
Couldn't find the specifier for the boolean so I used %d wich worked.
I can't see a difference between using 0 and 1 over boolean.
So if there is a diff. please explain. :)
| Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #10 on Mon 10 Nov 2008 01:25 PM (UTC) |
Message
| In Lua, this code will print "hello":
if 0 then print("hello") end
The only things that are "false" in Lua are nil and, well, "false". Everything else evaluates to "true". |
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 #11 on Mon 10 Nov 2008 07:16 PM (UTC) |
Message
| When David asked you before, you said it was a Lua variable, nothing was mentioned about a database.
You may well need to convert from true/false to 1/0 for a database, however as David says, once you retrieve the information later, you will need to test for equal to 1 to be true, and not equal to 1 to be false. It might be better to have a helper function that converts true to 1 and false to zero, like this:
function db_bool (val)
if val then
return "1"
end -- if
return "0"
end -- function
You could make a similar function to convert back, when reading from the database. The point of this is that then you can subsequently test:
rather than:
if <variable> == "1" then ...
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | 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.
25,448 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top