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: Using a table as its own iterator

Previous Thread :: Next Thread 
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:18 PM
tickeroftime gave me an idea in that I should make a table its own iterator. The syntax I decided to "follow" was how it used to be in the very olden days of Lua and in some other languages:

for key, value in tbl do
print(key, value);
end

All you have to do is use metatables and the __call metamethod, so when the generic for calls your function as an iterator (hence the "_"), it would just get the next pair:

local tbl = setmetatable({"a", "b", "c", "d", e = 5, f = 6}, {
__call = function(self, _, key, value)
key, value = next(self, key);
if key then
return key, value;
end
end;
});
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:19 PM
Are there any obvious downfalls to using this that I can't see? It seems pretty fine (I know I can optimize it by localizing next and making it an upvalue of __call but that's not the point).
Report Abuse
mycheeze is not online. mycheeze
Joined: 27 Jun 2011
Total Posts: 6748
12 Oct 2015 09:21 PM
'Metatables, the god forsaken union of underscores and gibberish.'

Thou's metatables are ugly-eth cn
Report Abuse
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
12 Oct 2015 09:22 PM
I actually can't think of any; I never thought of that.
ROBLOX should actually put that into their instances and deprecate :children and :GetChildren even further.

be like

for i,v in pairs(workspace()) do
print(i, v.Name)
end


http://www.roblox.com/--item?id=130759239
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:23 PM
You mean
for i, v in workspace do
??

If so, I disagree. Not because it's not a bad idea but because it makes Rbx.Lua feel even more different than Lua and may cause confusion.
Report Abuse
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
12 Oct 2015 09:27 PM
Oh, wait. It took me a minute.. Yeah, that'd work. But just implementing Instances and custom userdatas made it different from Lua. I mean... ROBLOX found a hacky way using pure Lua to lock their instance metatables using __metatable, so why not use a hacky way of simplifying getting an object's children? (__metatable = "The metatable is locked")

http://www.roblox.com/--item?id=130759239
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:28 PM
'ROBLOX found a hacky way using pure Lua to lock their instance metatables using __metatable'
It's not "hacky", it's as easy as wrapping table.insert and rawset.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:29 PM
Woops I thought you were referring to how you can't alter those tables. But "locking metatables" is not hacky either.
Report Abuse
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
12 Oct 2015 09:32 PM
It's even more hacky than C++'s int + "" tostring methods..
which are pretty hacky.
regardless, it doesn't seem very bad to allow you to iterate over a userdata with a for loop, especially considering ROBLOX doesn't even utilize the __call metamethod. It wouldn't take them much effort to put that into their base Instance class.


http://www.roblox.com/--item?id=130759239
Report Abuse
mycheeze is not online. mycheeze
Joined: 27 Jun 2011
Total Posts: 6748
12 Oct 2015 09:33 PM
Oh my goodness, looking at this thread is like looking at animals communicate back and forth.

qq why can't you guys drop the metatable stuff and dumb it down please <3
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 09:35 PM
'It's even more hacky than C++'s int + "" tostring methods..'
Uh, that wouldn't even work and the WHOLE point of the __metatable metamethod is to control metatable access, not hacky at all -.-
Report Abuse
kools is not online. kools
Joined: 11 Jan 2009
Total Posts: 1659
12 Oct 2015 10:16 PM
This is a really nice way of doing things I will be using this from now on. The closest I could get to finding some reference to problems associated with the call metamethod are that things that use lua_isfunction ie table.foreach trip up when they get something something that is a metamethod, they expect a real lua function.

However, that is not your use case here, so unless a noob looks at your use of a table as a function and then passes it to table.foreach as the parameter for the callback then you are fine.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
12 Oct 2015 10:53 PM
Well you're not meant to change the actual metatable and are expected to always pass a table c;
Report Abuse
gskw is not online. gskw
Joined: 05 Jan 2013
Total Posts: 1364
13 Oct 2015 01:03 AM
Valkyrie's wrapper automatically changes the __call of Instances to an iterator
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Oct 2015 01:20 AM
Since when O_o
Report Abuse
gskw is not online. gskw
Joined: 05 Jan 2013
Total Posts: 1364
13 Oct 2015 02:32 AM
https://github.com/ValkyrieRBXL/ValkyrieFramework/blob/bleeding-edge/Shared/Core/BaseLib.mod.lua#L211

Several months it seems
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Oct 2015 02:36 AM
Well, crap.
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Oct 2015 03:08 AM
"This is a really nice way of doing things I will be using this from now on. The closest I could get to finding some reference to problems associated with the call metamethod are that things that use lua_isfunction ie table.foreach trip up when they get something something that is a metamethod, they expect a real lua function.

However, that is not your use case here, so unless a noob looks at your use of a table as a function and then passes it to table.foreach as the parameter for the callback then you are fine.
"

assert(type(parameter) == stuff, "Attempt to be a noob")
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Oct 2015 09:26 PM
Bump
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