Modified Director For Jonathan Beebe's Blog Post

Hey all,

I just spent a few minutes doing search and replace in my text editor on director 1.4. It no longer uses the module function per Jonathan’s blog post. To be consistent, I used “director” instead of “M”.

I know a few people have asked for this already so I thought I would share. Hopefully this will be added to the next version of director.

Here it is. Let me know if it works for you. I literally swapped this in five minutes so I haven’t done much testing. Thank you so much for that blog post @Jonathan. I’ve switched everything over in my current app!

Director with no module function:

Edit: It didn’t work. I think it is too long to post. One Sec… [import]uid: 89328 topic_id: 14823 reply_id: 314823[/import]

Here it is: https://gist.github.com/1201974 [import]uid: 89328 topic_id: 14823 reply_id: 54779[/import]

Thanks for this version of director. I just tried replacing the module director with your version in something that changes scenes OK with the module version. Unfortunately, I kept getting the following runtime error when I tried it:

director.lua:670: attempt to index field ‘?’ (a boolean value) in function ‘loadScene’

The only changes I made to the existing files are to add the line require “director” and comment out the module(…, package.seeall) line.

I’m really not at all sure of what I’m doing with things like this, so am I not going about it in the right way?

Many thanks, Pete
[import]uid: 24031 topic_id: 14823 reply_id: 54850[/import]

Hi Pete,

Here is what I did in my existing files to get it working:

I commented out module line and added local M table:

--module(..., package.seeall) local M = {}

I changed my new function to look like this:

local new = function()

Right after I end the new function I added the following at the end of the file:

M.new = new return M

Hope this helps.

  • Steven [import]uid: 7175 topic_id: 14823 reply_id: 54922[/import]

Thanks for the post, i think i have everything setup correctly now, but im confused on how you call a function from another module using this method. For example im in my Main.lua, and i press a button that i would like to run a function in module “mechanics”. What would i code to do this? [import]uid: 19620 topic_id: 14823 reply_id: 54947[/import]

Hi Steven

Brilliant - nice clear help that has got me sorted so now it’s working fine.

Many thanks
Pete [import]uid: 24031 topic_id: 14823 reply_id: 54960[/import]

Sorry I couldn’t reply this morning.

I decided to make a repository for this on my github account. It has a full example and some documentation. Please check that out here.

I went with a similar approach as @ssfulfe. Instead of assigning new to M.new, I just assign the function to M.new to start with. As far as I know, that is just personal preference. [import]uid: 89328 topic_id: 14823 reply_id: 54965[/import]

Thank you for the reply, i now have director changing scenes ok. My question is how to actually call a “global” function with this setup. Before using the require “module” way. i could say mechanic:thisFunction(). How do i call functions from another module? If i understand right the functions are stored in the M table. so i have been trying to use M:thisFunction. But cant seem to make it work. any help would be appreciated, thanks [import]uid: 19620 topic_id: 14823 reply_id: 54967[/import]

Okay. The M table is internal to the file it is in. Because you are putting “return M” at the end of the file, you are passing the reference to whatever variable you require it to.

So if this is your mechanic.lua file:
[lua]-- mechanic.lua
local M = {}

M.thisFunction = function()
print(“thisFunction”);
end

return M[/lua]

The variable that you require mechanic into will become M. So, for example:
[lua]-- otherfile.lua
mechanic = require(“mechanic”)

mechanic.thisFunction()[/lua]

Does this help? [import]uid: 89328 topic_id: 14823 reply_id: 54968[/import]

Yes i got it working, thank you very much. I was confused because i thought the whole point of Jon’s new way of coding was to never use the “require”(“module”) anymore? I’m still very new when it comes to this external module stuff, trying to make more sense of it little by little. [import]uid: 19620 topic_id: 14823 reply_id: 54972[/import]

Okay. I see why you are confused. He was trying to get rid of the “module(…, package.seeall)” line. That sets up a bunch of garbage behind the scenes that doesn’t always clean up right. Essentially it takes ALL of the functions in a file and makes them global when the file is required.

So, we are replacing that declaration at the top of the file by assigning all of the functions to the M table and returning it. The require() function will always be needed because that is the only way to include another file. [import]uid: 89328 topic_id: 14823 reply_id: 54974[/import]

Ok perfect that makes sense. sorry to bother, but i have one other question. to add just a basic variable to my M table, i would just say M.myvariable = 1. now to get the info from that variable in another file, what is the correct syntax for reaching the variable?

[lua]–Mechanics file
local M = {}

local M.currentPage = 1

return M[/lua]

then in my other lua file

[lua]mechanic = require(“mechanic”)

–Page 1
local M = {}
local localGroup = display.newGroup()

local function initVars()
–Not sure what syntax to use here to refer to the variable?
if mechanic.currentPage == 1 then
local image_1

–This works to call the function like i wanted
mechanic.mechanicStart()
image_1 = display.newImage(“img_1.jpg”)
image_1.x = display.contentWidth * 0.5
image_1.y = display.contentHeight * 0.5
localGroup:insert( image_1 )
end
end

---------NEW FUNCTION--------------------
M.new = function()


– Initiate variables

initVars()


– MUST return a display.newGroup()

return localGroup

end

return M [import]uid: 19620 topic_id: 14823 reply_id: 54975[/import]

Not a bother. This is what the community is for.

I think the issue you are having is this. You don’t need to put “local” before you assign a value to a table. The value will have the same scope as the table.

In other words this:
[lua]–Mechanics file
local M = {}

local M.currentPage = 1

return M[/lua]

Should be written like:
[lua]–Mechanics file
local M = {}

M.currentPage = 1

return M[/lua]

Let me know if that works for you. [import]uid: 89328 topic_id: 14823 reply_id: 54979[/import]

That did work for me, it makes sense now, the table M was declared as local in the first place… dur haha, and im adding values to the already local table.

Do you know if it is possible to insert a display object from 1 file into a display group that i have created in my Mechanic file?

Seems like it probly isnt possible, i was trying to use the following with no luck:

[lua]–in my mechanic file
M.currentPane = display.newGroup()

–in my other file
mechanic.currentPane:insert( image_1 )[/lua] [import]uid: 19620 topic_id: 14823 reply_id: 54982[/import]

Yes, I think that should work. You need to display the group though. I think you are adding the image fine, but the group is not showing up.

In your second file, you should do something like this:
[lua]localGroup:insert(mechanic.currentPane)
mechanic.currentPane:insert( image_1 )[/lua] [import]uid: 89328 topic_id: 14823 reply_id: 54983[/import]

Awesome that did the trick, thank you so much for your time. Now to venture onward with this project!! [import]uid: 19620 topic_id: 14823 reply_id: 54988[/import]

Good luck with your project! [import]uid: 89328 topic_id: 14823 reply_id: 54991[/import]

Accessing external modules

Thanks Jonathan and justimag.in.board

Got everything working, after changing Director to director. Also I am using ui.lua 1.5 Edited by William Flagello.

Here’s a little snippet that might help someone:

I am in a module through director change screen called level1. I then draw/set up the graphics from another module called myMoozi (local myMoozi = require(“myMoozi”)) (This is how you bring in other modules, it doesn’t change screen, but allows you to use anything from this external module)
I am now able to add a listener to an object (linee) in the master set/draw module (myMoozi)

Cool!

[code] level1M.testPrint = function()
print "Testing 1 2 3 4 "
end

myMoozi.linee:addEventListener( “tap”, level1M.testPrint)
[/code]

Can anyone answer this question? :

–> Like ustimag.in.board changed director, do we have to also change ui lua, beebegames.lua, TextCandy.lua, etc (Other modules/scripts) that we use in our projects, as they have module(…, package.seeall) at the top ?

Thanks from the UK! [import]uid: 47300 topic_id: 14823 reply_id: 55402[/import]

Excellent post! But I have some questions about what functions to “include” in the new local “M” table (or whatever you decide to name it).

Let’s say that I have an external module “popupwindow.lua”. In that module are 3 functions, but only 1 of those functions is “called” from elsewhere, i.e. from main.lua. For example…

--popupwindow.lua  
local function pauseContent() (my code) end  
  
local function clearContent() (my code) end  
  
local function loadContent() (my code) end  

Of these 3 functions, the only function that needs to be “accessible” from outside this module is “loadContent”, and so I would include it in the table “M” as specified…

--popupwindow.lua  
local M = {}  
  
local function pauseContent() (my code) end  
  
local function clearContent() (my code) end  
  
local function loadContent() (my code) end  
M.loadContent = loadContent  
  
return M  

This, of course, makes “loadContent” accessible from any other Lua file which requires-in “popupwindow.lua”. But what about the other two functions in the module? They are used only internally within that module… never outside it… but do I still need to add them to table “M” to prevent them from going into the global space?

Expand this outward to a theoretical module with 15 local functions. If only 3 of those functions need to be accessible from elsewhere, what about the other 12? It seems very tedious if I must include all 12 functions inside “M” even if they’re never used outside the module… but if this is necessary for true optimization, so be it. I’m just trying to gain a better understanding of what’s going on under the hood. :slight_smile:

Thanks!
Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 14823 reply_id: 58018[/import]

Now i’m even more confused lol.

I’ve been wanting to dive into director, but waiting for the whole package…seeall debate to simmer down and see who wins :slight_smile:

I shall have to see what Ricardo comes up with and what will happen in 1.5 :slight_smile:

ng [import]uid: 61600 topic_id: 14823 reply_id: 59778[/import]