generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Metatables -An actual scripting question

Previous Thread :: Next Thread 
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 04:28 PM
I can script pretty well I guess (inb4 GOBWEY).

However one thing I have never gotten round to learning our understanding is metatables. What is the purpose of metatables, what exactly are metatables, how are they different from regular tables/arrays.

If I get flamed for this I guess it proves that this forum is literally incapable of having a discussion about Roblox Lua.

Report Abuse
Quenty is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
28 Aug 2011 04:30 PM
Flamity Flame Flame...

JK.

Anyway, basically you can call an index on a 'Metatable' that's nil, and not error. Applications... I have no idea.
Report Abuse
Spectrumw is not online. Spectrumw
Joined: 04 Aug 2009
Total Posts: 13510
28 Aug 2011 04:36 PM
Metatables affect the way a table behaves. Using metamethods will allow you to preprogram 'responses' in certain situations when handling with the table the metatable is attached to.
Report Abuse
booing is not online. booing
Joined: 04 May 2009
Total Posts: 6594
28 Aug 2011 04:37 PM
cuz they are epicer.
Metatables let you change the behavior of a table.
You can make a table "read-only" like this:
tablelist = {"apples","pairs","plums","peaches"}
tableproxy = {}
setmetatable(tableproxy, {__index=tablelist, __newindex = function() error("Attempt to write to read-only table.") end})
print(tableproxy[1]) >apples
tableproxy[1] = "pairs" >std:1:Attempt to write to read-only table
Report Abuse
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 04:37 PM
So..

tab={}
setmetatable(t,tab)

print(getmetatable(t)[1])==nil

Is that it?
Report Abuse
Ozzypig is not online. Ozzypig
Joined: 27 Mar 2008
Total Posts: 4906
28 Aug 2011 04:38 PM
See also: http://www.roblox.com/jrLua-2-1-4-item?id=59196492

~Ozzy
roblox.com/my/groups.aspx?gid=372
Report Abuse
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 04:39 PM
I'll stick to real Lua ;p
Report Abuse
Ozzypig is not online. Ozzypig
Joined: 27 Mar 2008
Total Posts: 4906
28 Aug 2011 04:39 PM
print(setmetatable({}, {__tostring = function () return "Hello, world" end}))

-->"Hello, world"

~Ozzy
roblox.com/my/groups.aspx?gid=372
Report Abuse
Ozzypig is not online. Ozzypig
Joined: 27 Mar 2008
Total Posts: 4906
28 Aug 2011 04:39 PM
It is real Lua; you haven't even tried it yet :x

~Ozzy
roblox.com/my/groups.aspx?gid=372
Report Abuse
blobbyblob is not online. blobbyblob
Joined: 29 Oct 2008
Total Posts: 12165
28 Aug 2011 04:42 PM
They're used to determine how tables act.

One thing you may use them for is userdata. Let's say you made a Vector4 object. Now, when the user accesses the property _magnitude_, you want to have the property ready. You _could_ calculate the magnitude when you create the Vector4 object, or you could use metatables to do something like this:

`setmetatable(Vector4Object, {
__index=function(t,i)
if i=="magnitude" then
return math.sqrt(t.w * t.w + t.x * t.x + t.y * t.y, + t.z * t.z);
end
end});`


Another way I use them is to make _scrolling_ tables. That is, if the table has 5 indices, and I query the 7th one, it will return the second.

So in practice, I could do something like this:

info={"H", "e", "l", "l", "o"};
for i=1,8 do
print(info[i]);
end
H
e
l
l
o
H
e
l


And that's helpful if you're making lists that are scrolled through.
Report Abuse
blobbyblob is not online. blobbyblob
Joined: 29 Oct 2008
Total Posts: 12165
28 Aug 2011 04:44 PM
For my second example, pretend I did some metatable manipulation.

setmetatable(info, {__index=function(t, i)
if type(i)=="number" then
return t[i%#t];
end
end});
Report Abuse
Spectrumw is not online. Spectrumw
Joined: 04 Aug 2009
Total Posts: 13510
28 Aug 2011 04:44 PM
Table = {"Hello"}
setmetatable(Table, {__Index = function(T, Bleh) return " World!" end})

print(Table[1] .. Table[2])
> Hello World!
Report Abuse
Tenal is not online. Tenal
Joined: 15 May 2011
Total Posts: 18684
28 Aug 2011 04:44 PM
[ Content Deleted ]
Report Abuse
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 04:46 PM
Points at the Tenal's primary

tells Tenal to go die

repeat 1
Report Abuse
dekkonot is not online. dekkonot
Joined: 22 Dec 2010
Total Posts: 6685
28 Aug 2011 04:47 PM
[GET OUT EMESS]
Report Abuse
Tenal is not online. Tenal
Joined: 15 May 2011
Total Posts: 18684
28 Aug 2011 04:57 PM
[ Content Deleted ]
Report Abuse
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 04:58 PM
Points back at my groups

See's one clan.

asks tenal to point out anything in that clan that neither me or SilentSwords made.

victory

repeat 1
Report Abuse
Tenal is not online. Tenal
Joined: 15 May 2011
Total Posts: 18684
28 Aug 2011 05:03 PM
[ Content Deleted ]
Report Abuse
dekkonot is not online. dekkonot
Joined: 22 Dec 2010
Total Posts: 6685
28 Aug 2011 05:05 PM
[b64]R28lMjBhd2F5JTIwcGx6Lg==[/b64
Report Abuse
Roundel is not online. Roundel
Joined: 20 Mar 2010
Total Posts: 469
28 Aug 2011 05:07 PM
*Points at Owen0202's clans*

*Asks Owen0202 if WIJ is worth joining if I want to make lazer guns and futuristic transportation and st00f*
Report Abuse
owen0202 is not online. owen0202
Joined: 27 Feb 2008
Total Posts: 6912
28 Aug 2011 05:13 PM
Cerulean -Built by me and SilentSwords
Indigo -Built by me
Cobalt -Built by me and SilentSwords
HQ -Built by me and SilentSwords
Hologrounds -Built by SilentSwords
Cruiser -Built by SilentSwords
WIJEngine -Scripted by me and SilentSwords

HaveIwonyet?
Report Abuse
xSIXx is not online. xSIXx
Joined: 06 Aug 2010
Total Posts: 9202
28 Aug 2011 05:18 PM
In very simple terms...

Metatables are events for tables... example.

tacos = {"moldy turds"}
lolmetatable = {__EVENTNAMEHERE = function()
-- Stuff which happens when event fires.
end}
setmetatable(tacos, lolmetatable)

You can look up the list of possible "events" (aka metamethods) on the wiki.

For example the "event" "__index" fires whenever the table is indexed (which means it fires whenever you mess with the table).

tacos = {"moldy turds"}
lolmetatable = {__index = function()
print("pee")
end}
setmetatable(tacos, lolmetatable)
Report Abuse
DonnyTheDemented is not online. DonnyTheDemented
Joined: 29 May 2010
Total Posts: 1413
28 Aug 2011 05:42 PM
Whats the difference between __index and __newindex? (if __index fires whenever you mess with the table whats the point of __newindex?)
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
28 Aug 2011 05:43 PM
[ Content Deleted ]
Report Abuse
Spectrumw is not online. Spectrumw
Joined: 04 Aug 2009
Total Posts: 13510
28 Aug 2011 05:43 PM
@Donny
__newindex is trigerred each time you add a new value to the table, while __index is triggered when you index an inexistant index.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image