[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Variable Problem

Variable Problem

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Orogan   (23 posts)  [Biography] 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?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Orogan   (23 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Orogan   (23 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Orogan   (23 posts)  [Biography] 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?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] 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
[Go to top] top

Posted by Orogan   (23 posts)  [Biography] 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. :)
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] 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:


if <variable> then ...


rather than:


if <variable> == "1" then ...



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


22,112 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]