Lua Module Function Critiqued

Hi guys!

Shaun here,

It’s possible you’ve seen my name float around, I’ve done a lot of reading, I’ve done a tiny bit of replying. This is my first post.

Lately I’ve done a lot of reading on things like the Director Class, the OOP template in the code exchange, and just on modules and groups in general. With the documentation in the api on modules being next to nothing I turned to the net at large for some answers.

In my readings I came across this:

http://lua-users.org/wiki/LuaModuleFunctionCritiqued

In the post the author details how modules work typically in lua, and the problems they can cause without even listing an error. Toward the bottom (last example before comments) he offers up an alternative that makes a lot of sense.

I was curious what your takes are, considering I took very little C in college and this is only my second scripting language I’m not as experienced as some in the Ansca community.

With that, have a read and let me know what your impressions are. At the very least I thought the community should know there’s an alternative to using modules. When doing this like the director class, it seems like it could simply things greatly and reduce the lines of code.
Shaun
Zombie Alligator Games [import]uid: 20703 topic_id: 7802 reply_id: 307802[/import]

Umm that’ll take a few reads !! But his whole original approach is different

I would do
[lua]-- hello/world.lua
module(…, package.seeall)
local function test(n) print(n) end
function test1() test(123) end
function test2() test1(); test1() end[/lua]
and used like this:
[lua]local helloworld = require “hello.world”
helloworld.test2()[/lua]

I don’t know how this affects things as per his discussion re: packages and global namespace etc
[import]uid: 6645 topic_id: 7802 reply_id: 27687[/import]

No problem, perhaps I can provide a simplified example:

Let’s say you’re making a splash screen. So you’ll make a lua file called splashScreen.lua to contain it. Normally using module you would write that like this:

-- splashScreen.lua  
  
module (..., package.seeall)  
  
function play ()  
 function splashProcess (event)  
 if event.completed then  
 print ("I like tacos!")  
 else  
 print ("impossible splash screen error has occurred!")  
 end  
 end  
 local background = display.newImage ("splashBackground.png", -125, 125)  
 local splashSound = audio.loadSound ("bleah.wav")  
 local splashChannel = audio.play (splashSound, {onComplete=splashProcess})  
end  

then in your main.lua you’d have this:

-- main.lua  
local splashScreen = require ("splashScreen")  
splashScreen.play ()  

Using the methodology set forth by the article linked in the Originial Post, you would do the same thing this way:

--splashScreen.lua  
local splashTable = {}  
  
function splashTable.play ()  
 function splashProcess (event)  
 if event.completed then  
 print ("I like tacos!")  
 else  
 print ("impossible splash screen error has occurred!")  
 end  
 end  
 local background = display.newImage ("splashBackground.png", -125, 125)  
 local splashSound = audio.loadSound ("bleah.wav")  
 local splashChannel = audio.play (splashSound, {onComplete=splashProcess})  
end  
  
return splashTable  

Your main.lua file would look exactly the same.

So you’re adding two lines of code and tableName. to whatever function you’re calling in your main area. In return you’re getting a level of safety. In addition this format may be easier to read depending on your coding experience.

P.S. of course, “I like tacos” is where you would then change out to your next module. I just thought that was more fun for the purposes of explanation :smiley:
[import]uid: 20703 topic_id: 7802 reply_id: 28528[/import]