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

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Lua Constants

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Lua Constants

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Terry   USA  (87 posts)  [Biography] bio
Date Sun 15 Jul 2007 10:34 AM (UTC)  quote  ]
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?
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #1 on Sun 15 Jul 2007 07:55 PM (UTC)  quote  ]
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.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #2 on Sun 15 Jul 2007 08:04 PM (UTC)  quote  ]
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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Reply #3 on Sun 15 Jul 2007 09:24 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Mon 16 Jul 2007 01:29 AM (UTC)  quote  ]

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


10,595 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]