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: Metamethods

Previous Thread :: Next Thread 
louiskk is not online. louiskk
Joined: 31 Dec 2008
Total Posts: 228
04 Jan 2015 04:22 AM
Hi,

I'm currently trying to learn about metatables and metamethods. I've been following Anaminus' tutorial on it, but I can't get it to work.


tab = {t1 = 2, t7 = 32}

mt = {m1 = 22, m2 = 32, __index = function() print("Stuff") end}

setmetatable(tab, mt)

wait()

print(tab['t1'])

--print(mt['m1'])

I've tried changing the index like this with no success.

mt.__index = function() print("Stuff") end

Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
04 Jan 2015 04:24 AM
The __index metamethod is only called IF AND ONLY IF the key does not exist.

print(tab["t2"]) would fire that, you would see Stuff then nil printed.
What's the point of the wait? :)
Report Abuse
louiskk is not online. louiskk
Joined: 31 Dec 2008
Total Posts: 228
04 Jan 2015 04:27 AM
Oh, thanks. I just added the wait as a test.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
04 Jan 2015 04:30 AM
Also, same with __newindex. It is only fired when you do:
tbl[key] = value
and tbl[key] is nil
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