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: metamethod help

Previous Thread :: Next Thread 
lostend is not online. 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 is not online. 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 is not online. 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
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
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
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