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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 10 Apr 2012 08:18 PM |
| __index printed 4, __newindex printed nil. Why? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
| |
|
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
|
  |
| 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
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 10 Apr 2012 08:36 PM |
| Never mind, it's only for nil indexes. |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
| |
|
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 |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 10 Apr 2012 09:04 PM |
@MrNicNac Are you talking to me or Miz?
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 10 Apr 2012 09:04 PM |
| http://wiki.roblox.com/index.php/Metatables |
|
|
| Report Abuse |
|
|