Question regarding deprecated use of "module".

Hi,

I’ve read on a blog post (http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/) here that module is considered “bad” because it makes you use global declarations.

I’m new to Corona so I’m yet to hit with a problem with my game, memory-wise or anything, but my previous game dev experience tells me to be aware of such thing.

So I was reading another blog post (yes I have more than 2Gb’s worth of open tabs in my browser …) regarding how to, at least one of the ways of, doing OOP in Lua/Corona that I stumbled upon this:

[lua]module (…, package.seeall)

function new()

local box = require(“BasicBox”).new()

function box:move()
self.rotation = math.random(360)
end

function box:setColor()
self:setFillColor(255, 0, 0)
end

return box

end[/lua]
And as that blog posts’ last line challenged us: “I want you to abolish the use of the module() function, while still reaping the benefits of modular coding!”, I was wondering how I can write that code in the same way that he did with it’s example, with defining a local table to store all references to functions/variables:

[lua]-- define a local table to store all references to functions/variables

local M = {}

– functions are now local:

local testFunction1 = function()

print( “Test 1” )

end

– assign a reference to the above local function

M.testFunction1 = testFunction1

local testFunction2 = function()

print( “Test 2” )

end

M.testFunction2 = testFunction2

local testFunction3 = function()

print( “Test 3” )

end

M.testFunction3 = testFunction3

– Finally, return the table to be used locally elsewhere

return M[/lua]

Thanks. [import]uid: 206803 topic_id: 34775 reply_id: 334775[/import]

That works or:

-- define a local table to store all references to functions/variables  
   
local M = {}  
   
   
   
-- functions are now local:  
   
local testFunction1 = function()  
   
print( "Test 1" )  
   
end  
   
-- assign a reference to the above local function  
   
M.testFunction1 = testFunction1  
   
   
   
local testFunction2 = function()  
   
print( "Test 2" )  
   
end  
   
M.testFunction2 = testFunction2  
   
M.testFunction3 = function()  
 print( "Test 3" )   
end  
  
-- Finally, return the table to be used locally elsewhere  
   
return M  

See how I did the 3rd function? Saves you some code.
[import]uid: 199310 topic_id: 34775 reply_id: 138217[/import]

Hi Rob,

Thanks for the reply. My question is how to have hierarchy in OOP with not using “module” keyword, “module (…, package.seeall)”. [import]uid: 206803 topic_id: 34775 reply_id: 138227[/import]

Hi Aidin,
Out of curiosity, have you read through Omid’s recent OOP tutorial series for Corona? This might give you some ideas and methods to tinker with. Perhaps you’ve already found it though, if you have 2GB of tabs open. :wink:

http://www.ardentkid.com/blog/2012-11-22/class-structure-in-corona

Brent [import]uid: 200026 topic_id: 34775 reply_id: 138233[/import]

Maybe something like this is what you’re looking for?

[code]-- box.lua
local BasicBox = require(“BasicBox”) – require in the file needed to make the box
local box = {} – table needed to return the file

local function move(self)
– move code
end

local function setColor(self)
– color code
end

function box.make() – this is the only public function because we attach the other functions to it
local newBox = BasicBox.new()
newBox.move = move
newBox.setColor = setColor

return newBox
end

return box[/code] [import]uid: 41884 topic_id: 34775 reply_id: 138302[/import]

@Brent: Thanks for the link, I’m reading through his 4 part tutorial regarding OOP in Lua. Will report back if problem persists.

@Richard9: Thanks for the reply. My question is how to make the base class, BasicBox, function as base class without using the “module” keyword in it. [import]uid: 206803 topic_id: 34775 reply_id: 138316[/import]

It really just depends how far you want to go OOP in building. Effectively that code I gave you could be the base class if you wanted, you just need to convert newBox into a display object.

If you want to use metatables then that’s another step deeper. [import]uid: 41884 topic_id: 34775 reply_id: 138328[/import]

Thanks Richard for your continuous care and replies.

To be honest with you, as of the moment I have no idea about metatables but as I’ve read around, it’s apparently one of the ways (or atleast related to) to do OOP in Lua but IIRC it was not favored by most of them.

What I basically want is to somehow make my base class not to use “module (…, package.seeall)” in it and still be able to inherit from it. [import]uid: 206803 topic_id: 34775 reply_id: 138346[/import]

Well I can’t help you much more than that, I mean in terms of inheritance the example works just fine because you can return objects and then pass it through as many files (layers) as you want to add extra functions or data. The key to avoiding module is mostly just proper use of return. :slight_smile: [import]uid: 41884 topic_id: 34775 reply_id: 138353[/import]

That works or:

-- define a local table to store all references to functions/variables  
   
local M = {}  
   
   
   
-- functions are now local:  
   
local testFunction1 = function()  
   
print( "Test 1" )  
   
end  
   
-- assign a reference to the above local function  
   
M.testFunction1 = testFunction1  
   
   
   
local testFunction2 = function()  
   
print( "Test 2" )  
   
end  
   
M.testFunction2 = testFunction2  
   
M.testFunction3 = function()  
 print( "Test 3" )   
end  
  
-- Finally, return the table to be used locally elsewhere  
   
return M  

See how I did the 3rd function? Saves you some code.
[import]uid: 199310 topic_id: 34775 reply_id: 138217[/import]

Hi Rob,

Thanks for the reply. My question is how to have hierarchy in OOP with not using “module” keyword, “module (…, package.seeall)”. [import]uid: 206803 topic_id: 34775 reply_id: 138227[/import]

Hi Aidin,
Out of curiosity, have you read through Omid’s recent OOP tutorial series for Corona? This might give you some ideas and methods to tinker with. Perhaps you’ve already found it though, if you have 2GB of tabs open. :wink:

http://www.ardentkid.com/blog/2012-11-22/class-structure-in-corona

Brent [import]uid: 200026 topic_id: 34775 reply_id: 138233[/import]

Maybe something like this is what you’re looking for?

[code]-- box.lua
local BasicBox = require(“BasicBox”) – require in the file needed to make the box
local box = {} – table needed to return the file

local function move(self)
– move code
end

local function setColor(self)
– color code
end

function box.make() – this is the only public function because we attach the other functions to it
local newBox = BasicBox.new()
newBox.move = move
newBox.setColor = setColor

return newBox
end

return box[/code] [import]uid: 41884 topic_id: 34775 reply_id: 138302[/import]

@Brent: Thanks for the link, I’m reading through his 4 part tutorial regarding OOP in Lua. Will report back if problem persists.

@Richard9: Thanks for the reply. My question is how to make the base class, BasicBox, function as base class without using the “module” keyword in it. [import]uid: 206803 topic_id: 34775 reply_id: 138316[/import]

It really just depends how far you want to go OOP in building. Effectively that code I gave you could be the base class if you wanted, you just need to convert newBox into a display object.

If you want to use metatables then that’s another step deeper. [import]uid: 41884 topic_id: 34775 reply_id: 138328[/import]

Thanks Richard for your continuous care and replies.

To be honest with you, as of the moment I have no idea about metatables but as I’ve read around, it’s apparently one of the ways (or atleast related to) to do OOP in Lua but IIRC it was not favored by most of them.

What I basically want is to somehow make my base class not to use “module (…, package.seeall)” in it and still be able to inherit from it. [import]uid: 206803 topic_id: 34775 reply_id: 138346[/import]

Well I can’t help you much more than that, I mean in terms of inheritance the example works just fine because you can return objects and then pass it through as many files (layers) as you want to add extra functions or data. The key to avoiding module is mostly just proper use of return. :slight_smile: [import]uid: 41884 topic_id: 34775 reply_id: 138353[/import]

Hi,

I’m reading more and more and more about object oriented in Lua/Corona and wanted to ask what did you mean by “The key to avoiding module is mostly just proper use of return. :)” ? If you would, please elaborate.

As I Google around, there are lots of ways people are doing OO with Corona/Lua and one just wonders what’s the most proper way to do it.

What I’m trying to do is just create a class that creates a table that has an image attached to it with newImageRect and several various functions and values. I’m not doing inheritance now, what I’m most interested is how to do OO the best way in Lua/Corona.

(I know “best” is defined by opinion but since I want to attach image to my object, I think there are less arguments with that way because as I’ve read you can’t use metatables that way)

I would appreciate any info on this guys.

Thanks. [import]uid: 206803 topic_id: 34775 reply_id: 139945[/import]

Thanks for the code and explanation but when I created a new project and wanted to run your program, apparently “require” statements do not work because after they execute, they are booleans and do not have methods on them. [import]uid: 206803 topic_id: 34775 reply_id: 140069[/import]

Previous versions of Lua (and thus Corona) used a command called module(..., package.seeall). The short of it is that this approach has some potential for memory leaks and other quirks, so the recommended method changed and that command was deprecated. (Here’s an old blog post about it)

return is a pretty elemental part of programming. If you think of a function as like an equation, return is what gives you the answer. That same logic applies to the modules themselves - you can put almost anything you can think of in a single .lua file, and the only thing the rest of your code will see is whatever is on the return line.

I know there is a good old blog post about OOP but so far I’m not a big fan of metatables. It depends what sort of game you’re making, though.

Here’s a rough example of how I use OOP, I suppose…

[1] A clean base module

[code]-- textBox.lua
– makes a message box with text in it
local M = {}

local function flip(self, newtext)
– flip code goes here
self.string.text = newtext
end

function M.create(blab)
local group = display.newGroup()
group.box = display.newRect(group, 0, 0, 200, 48)
group.string = display.newText(group, blab, 0, 0)

– Add functions it needs
group.flip = flip

return group
end
return M – important!
[/code]

[2] A specific child module

[code]–combatBox.lua
– text box only used for combat
local textBox = require(“textBox”)
local M = {}

local function defend(self)
– some code
end

function M.create()
local box = textBox.create(“FIGHT!”)
– add some combat specific functions
box.defend = defend
return box
end
return M – important![/code]

[3] call from where you want. You don’t need to require textBox since combatBox already does that.

[code]-- fightScene.lua
local cbox = require(“combatBox”)

local combatmsg = cbox.create()
combatmsg:flip(“part 2”)
combatmsg:defend()[/code] [import]uid: 41884 topic_id: 34775 reply_id: 139952[/import]