Register forum user name Search FAQ

Gammon Forum

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 ➜ Lua Constants

Lua Constants

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


Posted by Terry   USA  (87 posts)  Bio
Date Sun 15 Jul 2007 10:34 AM (UTC)
Message
How do you do Lua's equivalent to C's #define? When I looked through Lua's documentation, I found a lot about metatables, but I really didn't understand any of it :( Would someone be willing to help me out?
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #1 on Sun 15 Jul 2007 07:55 PM (UTC)
Message
You can just make a variable to hold pretty much anything. I'd need a specific example of what you're doing, but I frequently do things like define os.time() and os.difftime() in plugins to compensate for the fact that some people might have those permissions turned off. From the Lua users wiki:
> x = function(a,b) return a+b, a-b end
> = x(5,6)
11      -1
> function x(a,b,c) return a..b..c end
> = x('a','b','c')
abc


It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #2 on Sun 15 Jul 2007 08:04 PM (UTC)
Message
Forgot to mention, Lua is quite forgiving with where you put variables. You can replace just about anything just about anywhere. Makes it so you have to pay a bit more attention to what you're doing, since errors aren't always thrown when you do something you didn't want to, but it makes life a little easier along the way. A #define is really just a constant anyway.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sun 15 Jul 2007 09:24 PM (UTC)
Message
To define a "constant", you can just stick a variable at the top of a Lua script and give it a value, like

MY_CONSTANT = 2

Of course, it's not really a constant because you can change it; if you want it to be a proper constant you have a little more work to do.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 16 Jul 2007 01:29 AM (UTC)

Amended on Mon 16 Jul 2007 01:30 AM (UTC) by Nick Gammon

Message
If you set your mind to it, you can make constants by putting them in a table, and with metatables make it so you can't change it.

This example demonstrates that:



function protect_table (tbl)
  return setmetatable ({}, 
    {
    __index = tbl,  -- read access gets original table item
    __newindex = function (t, n, v)
       error ("attempting to change constant " .. 
             tostring (n) .. " to " .. tostring (v), 2)
      end -- __newindex 
    })

end -- function protect_table


-------------------------- test -----------------

my_constants =
  {
  WIDTH = 22,
  HEIGHT = 44,
  FOO = "bar",
  }

-- protect my table now
my_constants = protect_table (my_constants)

-- test it
print ("WIDTH",  my_constants.WIDTH)  --> WIDTH 22
print ("HEIGHT", my_constants.HEIGHT) --> HEIGHT 44

my_constants.WIDTH = 44  --> Error: attempting to change constant WIDTH to 44


The function 'protect_table' makes a copy of the original table, and returns an empty one, with a metatable attached to it. The metatable allows read access (via __index) but stops write access (via __newindex).

- 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.


30,908 views.

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

Go to topic:           Search the forum


[Go to top] top

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