miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 12 Dec 2011 09:34 PM |
I'm trying to learn metatable.... Why does this print what it prints?
local x = {} local metaTable = {} -- What's the point of that table if there is no value? setmetatable(x, metaTable) print(getmetatable(x))
prints
table: [memory address]
Confuses me... |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 12 Dec 2011 09:36 PM |
| Have you ever tried to learn about metamethods? n_n |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 12 Dec 2011 09:37 PM |
-_-
Dude...I literally just started a couple seconds ago...I looked at the wiki..Saw this script and it printed the table [memory address] |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 12 Dec 2011 09:41 PM |
| Look, when using metatables, you will be able to use certain functions called metamethods, that will allow you to change the way the table the metatable is attached to behaves. They are very useful once you know how to use them. |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 12 Dec 2011 09:50 PM |
| Mkay...Well, for starting metatables what should I learn first and where should I go from there? |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 12 Dec 2011 09:51 PM |
| First, learn about the __index and __newindex metamethod. |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 12 Dec 2011 09:52 PM |
| Mkay...Where can I find this? I know wiki -_- but do you have a link ? |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2011 09:53 PM |
Also, __tostring, __metatable, and __call are quite usefull. The others are arithmetic or comparison operators.
Cheers, -- AFF
It's not what you do, but what you feel that makes you who you are, for nobody can tell you how to feel. You may say what others want to hear, you may do what others want to see, but only you can truly know what you feel. You are in control of your own feelings. |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 12 Dec 2011 09:54 PM |
| http://www.lua.org/manual/5.1/manual.html#2.8 |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 12 Dec 2011 09:55 PM |
| Do you have a wiki link for this? |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2011 10:00 PM |
The wiki was confusing last time I checked. I can type up a quick tutorial tomorrow if you'd like. I have all day at school; I have to stay longer tomorrow. >_<
I'll even throw newproxy in there for ya. :P
Cheers, -- AFF
It's not what you do, but what you feel that makes you who you are, for nobody can tell you how to feel. You may say what others want to hear, you may do what others want to see, but only you can truly know what you feel. You are in control of your own feelings. |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 12 Dec 2011 10:01 PM |
@AFF I find the use of userdatas useless in Roblox, since they locked the __gc metamethod D: |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2011 10:02 PM |
Oh well. :P Playing with userdata is fun.
Cheers, -- AFF
It's not what you do, but what you feel that makes you who you are, for nobody can tell you how to feel. You may say what others want to hear, you may do what others want to see, but only you can truly know what you feel. You are in control of your own feelings. |
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
Pyzothon
|
  |
| Joined: 26 Oct 2011 |
| Total Posts: 822 |
|
|
| 13 Dec 2011 06:49 AM |
@oxcool
I don't think that's what he was asking for at all. |
|
|
| Report Abuse |
|
|
natjdog09
|
  |
| Joined: 16 Jan 2010 |
| Total Posts: 1370 |
|
|
| 13 Dec 2011 08:16 AM |
| Lol, I haven't got to metatables yet, I just started scripting a few weeks ago.. :P |
|
|
| Report Abuse |
|
|
|
| 13 Dec 2011 06:59 PM |
Okay, to start off, metatables are tables that describe the actions of other tables. Metatables control other tables with the following properties (known as metamethods):
__index __newindex __metatable __call __tostring
These metamethods act as event listeners to a table. Each time a certain event happens to the table, the metamethod fires.
__index --- fires when a nil value is looked up in a table __newindex --- fires when a NEW value is attempted to be added to a table* __metatable --- fires when the getmetatable function is called on a table __call --- fires when the table is called as a function __tostring --- fires when tostring is called on a table (includes concatenation)
*When using table.insert or rawset, the __newindex metamethod does NOT fire.
To further understand, let's make an example.
-- My First Metatable Script!
local Table = { Key = "Noob" }; local Meta = { __index = function(Table, Key) print("Unable to find " .. Key .. " in the table!") end, __newindex = function(Table, Key, Value) print("Access Denied! Cannot add " .. Key .. ":" .. Value .. " to table!") end, __metatable = "This metatable is locked", __call = function() print("This isn't a function! Stop trying to call it!") end, __tostring = function() return "TableAAAAA" end }
setmetatable(Table, Meta)
local i = Table.NilIndex -- __index Table.NilIndex = 42 -- __newindex print( getmetatable(Table) ) -- __metatable Table() -- __call print( Table ) -- __tostring
Cheers, -- AFF
It's not what you do, but what you feel that makes you who you are, for nobody can tell you how to feel. You may say what others want to hear, you may do what others want to see, but only you can truly know what you feel. You are in control of your own feelings. |
|
|
| Report Abuse |
|
|
Pyzothon
|
  |
| Joined: 26 Oct 2011 |
| Total Posts: 822 |
|
|
| 14 Dec 2011 06:30 AM |
| Wait. What would it print as the key, the number after the last key in the table? NilIndex doesn't exist, and your first metatable function prints the key, so... |
|
|
| Report Abuse |
|
|
|
| 14 Dec 2011 12:09 PM |
Oh wow, I didn't supply the output... >_<
______________________________________________________________ | OUTPUT | METAMETHOD | |___________________________________________|__________________| | Unable to find NilIndex in the table! | __index | | Access Denied! Cannot add NilIndex:42 to table! | __newindex | | This metatable is locked | __metatable | | This isn't a function! Stop trying to call it! | __call | | TableAAAAA | __tostring | |___________________________________________|__________________| |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 14 Dec 2011 12:26 PM |
"local x = {} local metaTable = {} -- What's the point of that table if there is no value? setmetatable(x, metaTable) print(getmetatable(x)) prints table: [memory address]"
It prints a memory address because metaTable is still a metatable. setmetatable(x, metaTable) assigns metaTable to be x's metatable. When you call getmetatable(x), you're printing metaTable. When you try to print a table, you will get a memory address.
For example: local x = {"waffles", "pancakes", "muffins", "[insert random food here]"} print(x) > table: 0x22e3dc0
---------- ~ pwnedu46, the unicorn ~ |
|
|
| Report Abuse |
|
|