Call function from different file with widget.newButton.

So I want to call a function from a different file. Without the widget it works perfect but with the widget it doesnt want to work.

moduleA.lua

function M.newFunction( ) print(111) end

start.lua

 local button = widget.newButton { x = y = width = height = font = defaultFile = overFile = onEvent = SC.newFunction() }

While you and I are here. How would i get touch events like this?

function M.newFunction( event ) if event.phase == "ended" then print(111) end end

Thanks! 

–SonicX278 

What is “SC” in your “start.lua” module? Did you require() “moduleA.lua” under that namespace? Also, did you “return M” from “moduleA.lua”?

Brent

Yes. I did all that. That’s what’s wierd. If i do this then it works.

moduleA.lua

function M.newFunction( ) print(111) end

start.lua

SC.newFunction()

It works. 

But if i try to call the function with widget.newButton it doesnt want to call it. 

It works with the widget if i create a local function in start.lua to call the global one.

–SonicX278 

Oh, I see what the issue is. You shouldn’t be putting the () on the end of the function pointer in the widget constructor. Just do this:

[lua]

local button = widget.newButton

{

    – …

    onEvent = SC.newFunction

}

[/lua]

Yess! Thats it! But now how would i get something like this.

moduleA.lua

function M.newFunction( bob ) print(bob) end

start.lua

onEvent = SC.newFunction( 111 )

–SonicX278 

That’s why i was adding the ().

–SonicX278 

That’s not how widgets work, whether you’re calling a local function or one in another module. The widget interaction (whatever you opt for) will call the defined function, passing to it the typical “event” table which contains all of the usual details.

If you really want to pass in some other data to the module-based function, you’ll need to use the two-prong approach where the widget calls a local function, and that function in turn calls the module-based function and passes your custom data over to that side.

Best regards,

Brent

Okay thanks! I had the whole local widget thing set up already. I was just thinking of cleaning up a bit so I wanted to go with a module. Well thanks for clarifying!

–SonicX278

What is “SC” in your “start.lua” module? Did you require() “moduleA.lua” under that namespace? Also, did you “return M” from “moduleA.lua”?

Brent

Yes. I did all that. That’s what’s wierd. If i do this then it works.

moduleA.lua

function M.newFunction( ) print(111) end

start.lua

SC.newFunction()

It works. 

But if i try to call the function with widget.newButton it doesnt want to call it. 

It works with the widget if i create a local function in start.lua to call the global one.

–SonicX278 

Oh, I see what the issue is. You shouldn’t be putting the () on the end of the function pointer in the widget constructor. Just do this:

[lua]

local button = widget.newButton

{

    – …

    onEvent = SC.newFunction

}

[/lua]

Yess! Thats it! But now how would i get something like this.

moduleA.lua

function M.newFunction( bob ) print(bob) end

start.lua

onEvent = SC.newFunction( 111 )

–SonicX278 

That’s why i was adding the ().

–SonicX278 

That’s not how widgets work, whether you’re calling a local function or one in another module. The widget interaction (whatever you opt for) will call the defined function, passing to it the typical “event” table which contains all of the usual details.

If you really want to pass in some other data to the module-based function, you’ll need to use the two-prong approach where the widget calls a local function, and that function in turn calls the module-based function and passes your custom data over to that side.

Best regards,

Brent

Okay thanks! I had the whole local widget thing set up already. I was just thinking of cleaning up a bit so I wanted to go with a module. Well thanks for clarifying!

–SonicX278