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 » Scripting Helpers
Home Search
 

Re: CntKillMe's Guide to Metatables

Previous Thread :: Next Thread 
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:22 PM
=======================================================
CntKillMe's Guide To Metatables
=======================================================
Before reading this, please make sure you understand tables and loops.
You can read it here: http://www.roblox.com/Forum/ShowPost.aspx?PostID=110411773

If you have tried learning about metatables and always get confused, I hope this will help.
Also, a common question: what is the use of metatables? Honestly, I don't know the answer, any script I've ever made never needed metatables.
Also, metatables are useful because of metamethods, which you will also see.
Most metamethods:
__index (Called when you index a table and the value doesn't exist.) [Apparantly this is a glitch in Lua and it should be called when you index a key at all, but I guess not.]
__newindex (Called when you add a key into a table.)
__unm (Called when you do: -table.)
__call (Called when you do: table(arguments).)
__add (Called when you add to the table: table + 5.)
__sub (Like add, but subtract. table - 5)
__mult (Like add, but multiply. table * 5)
__div (Like add, but divide. table / 5)
__mod (Like add, but module. table % 5)
__pow (Like add, but power. table ^ 5)
__concat (Called when you concate the table: table .. 5.)
__eq (Called when you check for equality to the table: table == 5.)
__lt (Called when you use the less than symbol to the table: table < 5.)
__le (Called when you use the less than or equal symbol to the table: table <= 5.)
__metatable (Called when you try to grab the metatable from it's table (adding this will also make replacing a metatable error.)

Syntax to set metatable: setmetatable(Table, Metatable)
Syntax to get metatable: getmetatable(Table)
OMG WUT IS DIS!?
local MyTable = {a = 5, b = 10}
local Metatable = {
__index = function(table, key)
return "LOL YOU FAIL, THAT DOESN'T EXIST"
end,
__newindex = function(table, key, value)
return "OMG NEW INDEX: " .. key .. " = " .. value
end,
__metatable = "NOOB YOU CAN'T GET ME!!",
__add = function(Table, Value)
if type(Value) == "Number" then --Check if Value is a number
return "SWAG YOLO: " .. 69 + Value
else
return "NOOB NUMBERS ONLY"
end
}

setmetatable(MyTable, Metatable)

print(MyTable.a)
>> 5

print(MyTable.c)
>> LOL YOU FAIL, THAT DOESN'T EXIST

MyTable["c"] = 5
>> OMG NEW INDEX: c = 5

print(getmetatable(MyTable))
>>NOOB YOU CAN'T GET ME!!

print(MyTable + "h")
>>NOOB NUMBERS ONLY

print(MyTable + 2)
>>SWAG YOLO: 71



Okay, well, there is not much to it. Here's another example:
local Swagger = {JustinBeiber = "Nah", SwagMaster143 = "Maybe", CntKillMe = "DamRight"}
local OhPlease = {
MySelf = 7,
__pow = function(Table, Value)
return tonumber(Value) and 4 ^ Value or "NUMBERS ONLY"
end,
__call = function(Table)
print("Ohh nice one")
end
}
setmetatable(Swagger, OhPlease)

print(Swagger ^ "LOL")
>>NUMBERS ONLY

Swagger()
>>Ohh nice one


If there is a problem or you need help, message me or reply.
I'm in too good of a mood some reason.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:25 PM
I forgot an "end" where it has __add
Report Abuse
ZachBloxx is not online. ZachBloxx
Joined: 26 Jun 2013
Total Posts: 2833
25 Aug 2013 07:26 PM
i hate swagger
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:26 PM
Also:

return "OMG NEW INDEX: " .. key .. " = " .. value
Should be:
print("OMG NEW INDEX: ", key, "=", value)
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
25 Aug 2013 07:30 PM
this is not a good tutorial
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:31 PM
I made it in 3 minutes, what can I say.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
25 Aug 2013 07:31 PM
You can say that it is probably impossible to make that much text in three minutes, and also that you should have perfected it before you posted it.
Report Abuse
ZachBloxx is not online. ZachBloxx
Joined: 26 Jun 2013
Total Posts: 2833
25 Aug 2013 07:32 PM
Typed over 2,900 characters in 3 minutes?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:32 PM
It's not impossible, and maybe 5 minutes.
And I don't have time, I was going to make a good one but my mom said that I have to go in 1 minute or so.
Report Abuse
Iterations is not online. Iterations
Joined: 22 Jul 2013
Total Posts: 751
25 Aug 2013 07:32 PM
i think wiki is better at explaining this
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:33 PM
No, I learned nothing from the wiki when I didn't know about this.
But my "guide" is pretty bad compared to my other one. (Okay, very bad)
Report Abuse
Iterations is not online. Iterations
Joined: 22 Jul 2013
Total Posts: 751
25 Aug 2013 07:33 PM
2969 characters.
Report Abuse
Arkose is not online. Arkose
Joined: 26 May 2013
Total Posts: 745
25 Aug 2013 07:34 PM
I thought I saw one on tables. If so may you provide me the link.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:34 PM
The link is on the thread ^^
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
25 Aug 2013 07:34 PM
"Also, a common question: what is the use of metatables? Honestly, I don't know the answer, any script I've ever made never needed metatables."
No. If this what you were planning on writing, then it wouldn't have been good either way.
Metatables are highly useful. I've used them in many instances; a practical, common example being administrator commands (it was literally centralized around the __index metamethod).
Metatables are tables that can be used to exploit usual syntax and create new environments. Please learn about what you're doing before you even think about making a tutorial.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:35 PM
I was never going to make a guide but 3 people told me to (or was it 2).
Report Abuse
Arkose is not online. Arkose
Joined: 26 May 2013
Total Posts: 745
25 Aug 2013 07:35 PM
"Before reading this, please make sure you understand tables and loops.
You can read it here: http://www.roblox.com/Forum/ShowPost.aspx?PostID=110411773"

Apparently I am blind, thank you. :)
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
25 Aug 2013 07:36 PM
I'm sure they did tell you. Can you provide evidence for this claim?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Aug 2013 07:37 PM
The link.

"create new environments. "
explain set/getfenv
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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