MrHistoryJoin Date: 2010-08-30 Post Count: 5291 |
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? |
|
SharksieJoin Date: 2009-10-10 Post Count: 30576 |
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. |
|
|
MrHistoryJoin Date: 2010-08-30 Post Count: 5291 |
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 |
|
miz656Join Date: 2010-07-19 Post Count: 15336 |
"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 |
|
SharksieJoin Date: 2009-10-10 Post Count: 30576 |
mmm, still not sure what you mean. Just try rewording your whole question. |
|
MrHistoryJoin Date: 2010-08-30 Post Count: 5291 |
Object is table |
|
SharksieJoin Date: 2009-10-10 Post Count: 30576 |
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". |
|
MrHistoryJoin Date: 2010-08-30 Post Count: 5291 |
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 |
|
smurf279Join Date: 2010-03-15 Post Count: 6871 |
*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) |
|
LPGhatguyForum ModeratorJoin Date: 2008-06-27 Post Count: 4725 |
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
|
|
smurf279Join Date: 2010-03-15 Post Count: 6871 |
"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 |
|
Dr01d3k4Join Date: 2007-10-11 Post Count: 17916 |
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 |
|
|
Or another implementation of "classes" in Lua that I made.
pastebin/qX0z5RWC |
|