Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 15 Mar 2012 09:48 PM |
| What would you _use_ metatables for in ROBLOX. No really? What is the application besides script builders? |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 15 Mar 2012 09:51 PM |
| They're very useful in making things like custom parsers and libraries. |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 15 Mar 2012 09:52 PM |
I use them to define librarys.
"God created us, and he will end us" - eminem |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 15 Mar 2012 10:04 PM |
| Tell me about Custom parsers and Libraries that you have made using Metatables. |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 15 Mar 2012 10:07 PM |
Well they are used with the C-- programming language to form into a designated form_data.
"God created us, and he will end us" - eminem |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2012 10:12 PM |
They're really good for OOP. account = {new = function(Balance) local x = setmetatable({}, {__index = { balance = Balance, input = function(self, value) self.balance = self.balance + value end, withdraw = function(self, value) if value >= self.balance then self.balance = self.balance - value end end }, __call = function() error("Attempt to get the length of an object!", 2) -- Right there. #account doesn't make sense end })
That's just a brief example. |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2012 10:15 PM |
FAIL. account = {new = function(Balance) local x = setmetatable({}, {__index = { balance = Balance, input = function(self, value) self.balance = self.balance + value end, withdraw = function(self, value) if value >= self.balance then self.balance = self.balance - value end end }, __tostring = function(self) return tostring(self.balance) end }) return x end}
print(#account) --> 0 (new) local y = account.new(30) print(#y) --> 0; Metatables. It wouldn't make sense to have a 5 long object. It just doesn't make sense. |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 15 Mar 2012 10:22 PM |
| What merlin said. They're really good for OOP and custom libraries. I made one here (http://www.roblox.com/Util-Lib-item?id=67593770) a while ago. It's not the greatest nor is it bug free, but I think is has a cool concept. I've also written an entirely new and much better class API but I haven't uploaded it to roblox. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 15 Mar 2012 11:04 PM |
Tell me why you can't just code your OOP stuff correctly in the first place. Also tell me why your OOP library should never error when the rest of it does, so nothing happens.
[ See how I talk like an interviewer? ] |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 15 Mar 2012 11:35 PM |
What are you talking about? If you're talking about mine, don't bother, I couldn't care less about the bugs it has since I have a much cleaner one I'm currently using.
In my new one, I use metatables to make sure functions are called with the 'inner' table as the self argument so that it can modify private variables and so that I can have, like stravant's, custom getters/setters as well as overriding operators so I can add two classes together for instance.
You can't do those cool things without metatables unless you love ugly code. |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2012 11:41 PM |
"Tell me why you can't just code your OOP stuff correctly in the first place." Get Table = {3, 4, 5} print(Table[1], Table[2], Table[3], #Table) to print 3 4 5 0 for me. Then I might agree with you. |
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
booing
|
  |
| Joined: 04 May 2009 |
| Total Posts: 6594 |
|
|
| 16 Mar 2012 07:10 AM |
Well, I guess you could make a protected library. local ProtectedLibrary = { magicnumber = 5 } _G.ProtectedLibrary = {} setmetatable(_G.ProtectedLibrary, { __index=ProtectedLibrary, __newindex=error("Attempt to modify read-only table",2) })
|
|
|
| Report Abuse |
|
|
|
| 16 Mar 2012 02:08 PM |
Why not code OOP the right way?
That __IS__ the right way.
And plus you can do awesome hax with it. Check out this: http://www.roblox.com/Forum/ShowPost.aspx?PostID=62534528 First reply. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 16 Mar 2012 02:11 PM |
Explain the use of your OOP thing, since almost all ROBLOX projects are 1 man development projects. Also explain how much of a failure this ontopic-attempt thread is, and why OOP will every apply to all of those games that people out there like.
|
|
|
| Report Abuse |
|
|
|
| 16 Mar 2012 02:26 PM |
because it's awesome, I use it a lot
ex, this is using a minorly edited version with a hardcoded 'getWrappedObject' function, and also a function called grab I made which brings a global function into scope
wait()
_G.grab("require")
require(Workspace.lQuery)()
game.Players.PlayerAdded:connect(function(player) local leaderStats = _("IntValue").SetName("leaderstats").SetParent(player) local hiddenStats = _("IntValue").SetName("Statistics").SetParent(player) local leaderstat1 = _("IntValue").SetName("Gold").SetValue("500000").SetParent(leaderStats.getWrappedObject()) local leaderstat2 = _("NumberValue").SetName("Fruit").SetParent(leaderStats.getWrappedObject()) end)
imho it looks kewl |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 16 Mar 2012 03:02 PM |
Why waste keystrokes? Make your API like this:
game.Players.PlayerAdded:connect(function(player) local leaderStats = _("IntValue").Name("leaderstats").Parent(player) local hiddenStats = _("IntValue").Name("Statistics").Parent(player) local leaderstat1 = _("IntValue").Name("Gold").Value("500000").Parent(leaderStats.getWrappedObject()) local leaderstat2 = _("NumberValue").Name("Fruit").Parent(leaderStats.getWrappedObject()) end) |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2012 03:12 PM |
Because it also has getters.
Although I am thinking of making it so that it checks for an argument. |
|
|
| Report Abuse |
|
|
nightname
|
  |
| Joined: 10 Jun 2008 |
| Total Posts: 8960 |
|
|
| 16 Mar 2012 03:59 PM |
What do you mean? OOP is only possible with metatables, as I said before - Lua is duck typed, and through the use of metatable OOP can be achieved.
I think I documented that in my blog too? If not, I wrote it on the Roblox wiki.
Also, metatables are extremely IMPORTANT. They are the cream of the crop! If anything, once I learned about metatables, scripting has never been more enjoyable.
How do you think my AI System works? All OOP! Since the AI System can be created through a simple function - I am able to create several systems, which hold different types of AIs.
I am going to release it soon, with the RPG set I am developing! |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 16 Mar 2012 04:03 PM |
> Although I am thinking of making it so that it checks for an argument. This.
function Parent(val) if val then setParent(val) else return getParent() end end |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 16 Mar 2012 04:05 PM |
| Still feel like an idiot, probably because I don't see how it applies so I'm too lazy to examine the code. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 16 Mar 2012 04:07 PM |
| I'm using metamethods right now in my Roblox 2D (in LOVE 2D ololloll) clone to make certain properties of my instances read-only, like className and such. |
|
|
| Report Abuse |
|
|
nightname
|
  |
| Joined: 10 Jun 2008 |
| Total Posts: 8960 |
|
|
| 16 Mar 2012 04:13 PM |
http://www.roblox.com/Forum/ShowPost.aspx?PostID=64428028
Look at that thread. I used metatables to add the "tostring" metamethod, and this means that when I print a table - it calls the tostring method, and from there I tell it to return "name".
Awesome features, right? |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 16 Mar 2012 04:31 PM |
| Would someone mind teaching me classes and metatables? I feel ignorant. |
|
|
| Report Abuse |
|
|