modules vs classes

I’m currently implementing classes like in my code snippet below (although it may be better to use something that has been developed more fully eg http://love2d.org/wiki/MiddleClass)

however i’m a bit confused between modules and these “classes”. I looked at the movieclip.lua example and it seems like the whole “class” is wrapped in a function

what I’m not sure about in mine is how to create a public static variable. I have a private static variable which works fine.

Can I just do:

ParallaxLayer:MY_STATIC_VARIABLE = “something”

?

are there benefits of using modules over what I am trying to do here?

main.lua
[lua]require(“ParallaxLayer”)

local p = ParallaxLayer:new(“whatever”)
print(p:getPrivateID())[/lua]

ParallaxLayer.lua
[lua]ParallaxLayer= {} – Create a table to hold the class methods
ParallaxLayer.__index = ParallaxLayer

local STATIC_ID – private, static

local _private = setmetatable({}, {__mode = “k”}) – private var container

function ParallaxLayer:new(layer)

print("ParallaxLayer::new " … layer)

local object = {

id = layer; – public variable

}

_private[object] =
{
_id = layer … " (private)"
}

setmetatable(object, ParallaxLayer) – Inheritance

if(STATIC_ID == nil) then
STATIC_ID=1
else
STATIC_ID = STATIC_ID + 1
end if

return(object)

end

function ParallaxLayer:getID()

return self.id
end

function ParallaxLayer:getPrivateID()

print(“ParallaxLayer::getID”)
print(“static STATIC_ID=” … STATIC_ID)
print("private _id = " … _private[self]._id
print(“public id =”… self.id)

return(_private[self]._id)

end[/lua] [import]uid: 6645 topic_id: 3512 reply_id: 303512[/import]

just looking at Mike Ziray’s Game Template

that’s different again. it’s using module, but a class format slightly like mine

with the “Class:method” syntax and require(“Class”) rather than Class=require(“Class”)

[import]uid: 6645 topic_id: 3512 reply_id: 10577[/import]

In your example, you can create a public “static” variable (along the lines of AS3), you could do the following:

[lua]ParallaxLayer.MY_STATIC_VARIABLE = “something”[/lua]

The 2nd edition of “Programming in Lua” does a good job of explaining the difference between creating modules by returning a table vs using the module() function. If you did use the module() function, then any local function/variable is private to the module; any function/variable that’s not local is “public”:

[lua]-- ParallaxLayer.lua
module(…)

– ParallaxLayer.MY_STATIC_VARIABLE
MY_STATIC_VARIABLE = “something”

local private = “something else”

local privateFunction = function()
end

– ParallaxLayer.publicFunction()
publicFunction = function()
end[/lua]

As an aside, there are various ways to produce classes. If you’re familiar with AS2, then Lua provides support for prototype-based classes via the metatable mechanism.

The movieclip.lua method is another way, though it’s hard to achieve inheritance in that model. [import]uid: 26 topic_id: 3512 reply_id: 11468[/import]

i’ve mixed up module and metatable in this example
http://developer.anscamobile.com/forum/2011/02/22/converting-variable-name-string#comment-24527

anything “wrong” with that?

it works, just wondering about memory/performance etc

thanks
j

[import]uid: 6645 topic_id: 3512 reply_id: 24532[/import]