Calling a function from another document

Hello Everybody. 

I know there are posts about this already, and questions, but this is what I have tried so far, using a tutorial:

–Main Lua file

local gfx = require(“gfx”);

gfx.printSomething();

–gfx lua file

local M = {};

function printSomething()

    print("Hello World);

end

M.printSomething = printSomething;

That’s my code, but for the life of me, I can not get it to work.

I get an error in the main lua file telling me that I am attempting to call printSomething(), which is a nil value. Did something change in recent versions of Corona, or am I not doing something right?

Thanks in advance.

Have you added:

[lua] return M [/lua]

At the very end of gfx.lua?

Actually, yes, I have. Sorry, I forgot to put that in the post. It still hasn’t worked out.

try

function M.printSomething()

 

    print("Hello World);

 

end

Thats the style i use ( copied from Rob M’s score module tut).

T.

Instead of your printSomething being called:

[lua]function printSomething()[/lua]

Make it…

[lua]printSomething = function ()[/lua]

Works here…

I think I got it to work. Thanks so much!

Just one question. Why doe I need to do Return M; what does that do? I have looked it up but haven’t found an answer yet.

the M is not really the focus, it could be anything but in this case for your  module you used 

local M = {}

so you created a table to store all “stuff” in the module, and called it “M”

you call the module, it runs your call - ( in the other room ) and by return M - it’s like you coming back ( with the data, from the other room) back to your starting room - to continue with your work.

T.

Oh. That’s why! Thank you!

I’m sorry, just (hopefully) one last question. When I pass variables through the function, 

e.g.

M.smoke = function(x, y)

I can use x and y when calling the function, but I can’t perform arithmetic on them. Is there a way to get around that?

yes it’s possible - but you haven’t provided alot of information/code etc… to assist easily.

Advice look at and breakdown the score module by Rob - that’s how i learnt about using modules.

T.

Ok. Thanks a lot!

Have you added:

[lua] return M [/lua]

At the very end of gfx.lua?

Actually, yes, I have. Sorry, I forgot to put that in the post. It still hasn’t worked out.

try

function M.printSomething()

 

    print("Hello World);

 

end

Thats the style i use ( copied from Rob M’s score module tut).

T.

Instead of your printSomething being called:

[lua]function printSomething()[/lua]

Make it…

[lua]printSomething = function ()[/lua]

Works here…

I think I got it to work. Thanks so much!

Just one question. Why doe I need to do Return M; what does that do? I have looked it up but haven’t found an answer yet.

the M is not really the focus, it could be anything but in this case for your  module you used 

local M = {}

so you created a table to store all “stuff” in the module, and called it “M”

you call the module, it runs your call - ( in the other room ) and by return M - it’s like you coming back ( with the data, from the other room) back to your starting room - to continue with your work.

T.

Oh. That’s why! Thank you!