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

Previous Thread :: Next Thread 
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:05 PM
Why dis no print :O

tab = {
__index = {"4","5"}
__newindex = {"6"}--Want to print 6.
}
jack = setmetatable({},tab)
print(jack[3])

How do I make it print using __newindex?
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:08 PM
tab = {
__index = function() print(4) end
__newindex = function() print(6) end
}
jack = setmetatable({},tab)
print(jack[3])
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:15 PM
Workspace.Script:3: '}' expected (to close '{' at line 1) near '__newindex'
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:16 PM
tab = {
__index = function() print(4) end,
__newindex = function() print(6) end
}
jack = setmetatable({},tab)
print(jack[3])
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:18 PM
__index printed 4, __newindex printed nil. Why?
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:20 PM
Because you're not creating a new index. Your indexing something (even if it is nil).

Just put some code down (not in print)

jack[4] = "Savannah"
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:22 PM
Look, I know what __index is. The point of this is trying to learn __newindex, so do I put that line in or out of tab{}?
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:24 PM
Did I not just tell you why __newindex did not fire and what you should do to make it happen?

tab = {
__index = function() print(4) end,
__newindex = function() print(6) end
}
jack = setmetatable({},tab)
jack[3] = "Savannah"

Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:32 PM
So __newindex always runs when you try to index something and it's nil?
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Apr 2012 08:33 PM
its on the wiki. . .
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:33 PM
No, it runs when you create a new index in a table that was previously nil OR when you overwrite an existing one (one that was already in the table).
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Apr 2012 08:34 PM
wait what? i though it was only for nil indexes let me check
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 08:36 PM
Never mind, it's only for nil indexes.
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:41 PM
gracias.
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
10 Apr 2012 08:44 PM
Wait, does __newindex always return nil, because that's all I'm getting when I'm returning it :/
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
10 Apr 2012 09:01 PM
derp = {
__newindex = function() print'DIE' end
}
herp = {}
setmetatable(herp,derp)
herp[3] = 8

>DIE

__newindex fires when you try to set (table[index] = value) when table[index] doesn't exist.

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 09:03 PM
@miz

It returns what you tell it to. Make a return somewhere.
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
10 Apr 2012 09:04 PM
That logic is actually for __index. Since you can't actually get back whatever __newindex returns, it is pointless to return anything.
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
10 Apr 2012 09:04 PM
@MrNicNac
Are you talking to me or Miz?

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Apr 2012 09:04 PM
http://wiki.roblox.com/index.php/Metatables
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