lostend
|
  |
| Joined: 21 Aug 2011 |
| Total Posts: 8265 |
|
|
| 12 Oct 2015 08:11 PM |
local equipped={false}
local unbind={ __index=function(self,key) print(self[key]) return self[key] end }
setmetatable(equipped,unbind)
equipped[1]=true
shouldn't this print 'true' help plz. doesn't print anything |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 12 Oct 2015 08:18 PM |
It only does the __index metamethod if there is no table entry that exists for whatever key you're trying to index. Also, you'd want __newindex for changing values, but that only runs when there is no previous index with a given key.
You'll have to proxy that table access through another table like this:
local equipped = {false}
local proxy = setmetatable({}, { __index = equipped, __newindex = function(self, index, value) if not equipped[index] then return end rawset(equipped, index, value) print(equipped[index]) end })
That will allow reading of any index in equipped and allow you to change current entries, but disallow creating new entries. |
|
|
| Report Abuse |
|
|
lostend
|
  |
| Joined: 21 Aug 2011 |
| Total Posts: 8265 |
|
|
| 12 Oct 2015 08:39 PM |
| The purpose is for a gun. Basically, it binds to renderstep if equipped is true, and unbinds if it gets changed to false. This is why I did that, so I don't have to unbind manually. |
|
|
| Report Abuse |
|
|
|
| 12 Oct 2015 09:00 PM |
If you don't mind BINDING it manually, then just put an if check in the RenderStep function.
Otherwise (this is untested, but I'm pretty sure it will work):
local m = {__contents = {}};
getmetatable(m).__index = function(self, index) return m[index] or m.__contents[index]; end
getmetatable(m).__newindex = function(self, index, value) rawset(m.__contents, index, value) if index == "Equipped" then if value then -- bind to renderstep else -- unbind from renderstep end end return value; end
http://www.roblox.com/--item?id=130759239 |
|
|
| Report Abuse |
|
|