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...

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

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:

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

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?

Hi Steven

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

Many thanks
Pete

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.

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

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:

1
2
3
4
5
6
7
8
-- mechanic.lua
local M = {}
 
M.thisFunction = function()
    print("thisFunction");
end
 
return M

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.

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.

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?

1
2
3
4
5
6
--Mechanics file
local M = {}
 
local M.currentPage = 1
 
return M

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:

1
2
3
4
5
6
--Mechanics file
local M = {}
 
local M.currentPage = 1
 
return M

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:

1
2
3
4
5
--in my mechanic file
M.currentPane = display.newGroup()
 
--in my other file
mechanic.currentPane:insert( image_1 )

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:

1
2
localGroup:insert(mechanic.currentPane)
mechanic.currentPane:insert( image_1 )

Awesome that did the trick, thank you so much for your time. Now to venture onward with this project!!

Good luck with your project!

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!

1
2
3
4
5
 level1M.testPrint = function()
      print "Testing 1 2 3 4 "
      end
      
myMoozi.linee:addEventListener( "tap", level1M.testPrint)
views:1952 update:2011/10/4 8:06:35
corona forums © 2003-2011