of     1   

MrHistory
#94827249Sunday, April 14, 2013 3:46 AM GMT

I'm not new to OOP, but I just started using it in my scripts, probably because APCS got me used to it. So, one thing I don't understand much is instance data. You can basically take any table of data and instantiate an object with it (or so I think?). Is there any way to have discreet pieces of instance data in a class that instances of that class can ONLY have?
Sharksie
#94827757Sunday, April 14, 2013 3:51 AM GMT

Rewrite your post, but replace every occurrence of the word "instance" with a synonym so I can actually understand what you mean. Lua has no "Instance" data type.
[rfa#hidefromsearch]
#94827844Sunday, April 14, 2013 3:51 AM GMT

[rfa#hidefromsearch]
MrHistory
#94827981Sunday, April 14, 2013 3:52 AM GMT

By the "instance" of a class, I mean an object that uses the class as a blueprint Idk, I'm not too good with terminology
miz656
#94828023Sunday, April 14, 2013 3:53 AM GMT

"You can basically take any table of data and instantiate an object with it (or so I think?). " define "object" be more specific on that. also what arceus said
Sharksie
#94828143Sunday, April 14, 2013 3:54 AM GMT

mmm, still not sure what you mean. Just try rewording your whole question.
MrHistory
#94828195Sunday, April 14, 2013 3:54 AM GMT

Object is table
Sharksie
#94828701Sunday, April 14, 2013 4:00 AM GMT

Okay, here's what I'm reading "You can basically take any table and create an object with it? Is there any way to have table data in a class which only objects of that class can have?" If that's what you mean, I'm still not sure how you mean to "create an object with it" or what you mean by "table data in a class".
MrHistory
#94828821Sunday, April 14, 2013 4:01 AM GMT

Basically, I have this: Account = { balance = 0, new = function(self, o) o = o or {} setmetatable(o, self) self.__index = self return o end } And I can do this: MyAccount = Account:new({lololo = 86573}) I'm trying to now allow this. All I want as a parameter when the Account.new function is called is a table containing "balance," since that is the only variable, that is not a function, I have in Account
smurf279
#94831392Sunday, April 14, 2013 4:30 AM GMT

*Note* Sorry for the word wall but this has a lot of info and this took awhile to write so idk if someone already explained it Basically with OOP there are 2 types of data: object data - data specific to that instance and separate from any other object/class class data - data specific to that class that any class instances will inherit When you instance an object the object itself will only hold its own data(the object data) but with metatable magic we can make it so the object "contains" class data as well. It works so that when we index the object, it will check inside of itself for the given property. If the property is not found it then goes on and looks inside the _separate_ class data(and any additional base classes if necessary). This allows objects to be much more light weight while still containing all its necessary properties. Usually the class data will contain constant data like functions and constant variables while the object data will contain variables that you can edit and work with. (The reason being, when you edit class data, the change will be reflected on _all_ class instances since they have to index the data, while the object stays to itself) Take a roblox part for example. Properties like "CFrame" and "BrickColor" would be part of the object's object data while methods like "Destroy" and "GetMass" are part of the object's class data. print(Instance.new("Part").GetMass == Instance.new("Part").GetMass) This would print true because the methods are actually being indexed from the class data and we are not creating new functions for the object itself Check this out as an example: http://www.roblox.com/createObj-v-3-2-4-item?id=96126365 I implemented the system of OOP so you can try figuring out how it works(I didn't include guides but you can look at the comments or ask me about it)
LPGhatguy
Forum Moderator
#94831719Sunday, April 14, 2013 4:34 AM GMT

I've got a decent implementation in my LÖVE game engine: https://github.com/LPGhatguy/Ussuri/blob/master/ussuri/core/oop.lua table_deepcopy and table_merge are both taken from another part of the engine, here: https://github.com/LPGhatguy/Ussuri/blob/master/ussuri/core/utility.lua
smurf279
#94832237Sunday, April 14, 2013 4:42 AM GMT

"I'm trying to now allow this. All I want as a parameter when the Account.new function is called is a table containing "balance," since that is the only variable, that is not a function, I have in Account" local Account = {cData = {}, oData = {balance = 0}} -- class/object data function Account:new(data) setmetatable(data or {}, {__index = self.cData}) for i, v in pairs(self.oData) do data[i] = data[i] or v end return data end This is how I would personally set it up but its not like this is the only way to do it
Dr01d3k4
#94851333Sunday, April 14, 2013 12:18 PM GMT

The way I write "classes" in Lua, is like this: local Account = { }; function Account.new(balance) local o = {balance = balance or 0}; setmetatable(o, {__index = Account}); return o; end function Account:deposit(amount) self.balance = self.balance + (amount or 0); end local account = Account.new(20); account:deposit(5); print(account.balance); -- 25 Though I have also done it like this before: local Account = { }; function Account.new(balance) local data = {balance = balance or 0}; local acc = { }; function acc:deposit(amount) data.balance = data.balance + (amount or 0); end setmetatable(acc, { __index = function (_, v) if (v:lower() == "balance") then return data.balance; end end; }); return acc; end Though now I'd use Moonscript's way: class Account new: (b = 0) => @balance = b deposit: (am = 0) => @balance += am acc = Account! acc\deposit 5 print acc.balance
YourLocalFaxMachine
#95437636Saturday, April 20, 2013 9:05 PM GMT

Or another implementation of "classes" in Lua that I made. pastebin/qX0z5RWC

    of     1