How does module.lua call a function in the scene.lua that required it.

We have scene.lua which requires module.lua. Events occur in module.lua until it needs to call a function back in scene.lua. How to do this without using _G.functions? An event listener?

Yes, basically when you call your module.lua function, you can pass it a function name to call when it’s done and that code can be in your scene.lua.

Rob

Thanks Rob. Just trying to get the right syntax for that. The following won’t work but just as a start:

scene1.lua

local funcName – forward declare funcName

newMod = require (“modA”)

newMod.new (funcName)

funcName = function()

end

modA.lua

M = {}

M.new (funcName)

then later in a function

laterFunction = function ()

 funcName()

end

scene1.lua

local funcName – forward declare funcName

newMod = require (“modA”)

newMod.new (funcName)

funcName = function( results )

– deal with any returned data in the results table

end

modA.lua

M = {}

M.new (funcName)

then later in a function

 local results = {} – put anything you want to return to the other function here

 funcName( results )

If you need to pass data back, you will create a table pas

Thanks for taking a look at this Rob, it would be great to not use the _G.function call.

Unfortunately when it comes to calling funcName(results) we get a runtime error:

attempt to call upvalue ‘funcName’ (a nil value)
stack traceback:    [C]: in function ‘funcName’

This is the actual function in ModA that calls funcName(results):

        function act_audChter (event)
            local isChannel1Playing = audio.isChannelPlaying( 1 )
            if isChannel1Playing then
                audio.stop( 1 )
            end
            local rloadClosure = function()
                if rloadFlag then
                    local results = {}
                    funcName(results)
                end
            end
            audio.play( vars.wswBlue, { channel=1, onComplete= rloadClosure } )
        end

Suggests the function didn’t get passed - but it has been following the syntax described above.

 

You’re going to have to post more code.  It will be quite helpful to see your module’s structure.

Ok, cool. Here’s more of the actual code. Using Director. If you need more, I could dropbox the 2 actual pages?

Be aware that labels like funcName and ModA used above were hyperthetical. So in the actual code substitute the following:

local funcName = local propButton

newMod = screens

“modA” = “screens”

page29.lua

module(…, package.seeall)

function new()
        local menuGroup = display.newGroup()
        local drawScreen = function()
        local propButton

        local screens = require(“screens”)
        screens.new(propButton)

        – Code here to load images

        screens.screen1()
        local objectList = screens.objectList

        propButton = function (results)

            for i = 1, #objectList do
                local button =  display.newImageRect ( imgDir… objectList[i].name…".png", objectList[i].w, objectList[i].h )
                button.name = objectList[i].name
                – etc…
            end

        end

        – more code here
    end
    drawScreen()
    return menuGroup
end

screens.lua

local M = {}

    M.new = function (propButton)

        – Code here to load images

        M.screen1 = function()    
                M.objectList =
                {
                    { name=“kwkchina”, w=345, h=300, x=177.5, y=288},
                        --etc…
                }
        end

        function act_audChter (event)

            local isChannel1Playing = audio.isChannelPlaying( 1 )
            if isChannel1Playing then
                audio.stop( 1 )
            end

            local rloadClosure = function()
                if rloadFlag then
                    local results = {}
                    propButton(results)
                end
            end

            audio.play( vars.wswBlue, { channel=1, onComplete= rloadClosure } )

        end

        – more code here

    end
return M

 

All that looks right.  What is the value of rloadFlag?  Perhaps you should put some print statements in and see if you can see how the app is flowing.

Rob

Got this working by changing the syntax of a call to this:

page29

screens.new({scNumber = i, propButton = propButton})  

screens

M.new = function (params)
    params = params or {}

    local sceneNumber = params.scNumber
    local propButton = params.propButton

Then later in screens.lua calling propButton() initiates that self named function in page29.

Very cool and now opens up lots of opportunities…

Yes, basically when you call your module.lua function, you can pass it a function name to call when it’s done and that code can be in your scene.lua.

Rob

Thanks Rob. Just trying to get the right syntax for that. The following won’t work but just as a start:

scene1.lua

local funcName – forward declare funcName

newMod = require (“modA”)

newMod.new (funcName)

funcName = function()

end

modA.lua

M = {}

M.new (funcName)

then later in a function

laterFunction = function ()

 funcName()

end

scene1.lua

local funcName – forward declare funcName

newMod = require (“modA”)

newMod.new (funcName)

funcName = function( results )

– deal with any returned data in the results table

end

modA.lua

M = {}

M.new (funcName)

then later in a function

 local results = {} – put anything you want to return to the other function here

 funcName( results )

If you need to pass data back, you will create a table pas

Thanks for taking a look at this Rob, it would be great to not use the _G.function call.

Unfortunately when it comes to calling funcName(results) we get a runtime error:

attempt to call upvalue ‘funcName’ (a nil value)
stack traceback:    [C]: in function ‘funcName’

This is the actual function in ModA that calls funcName(results):

        function act_audChter (event)
            local isChannel1Playing = audio.isChannelPlaying( 1 )
            if isChannel1Playing then
                audio.stop( 1 )
            end
            local rloadClosure = function()
                if rloadFlag then
                    local results = {}
                    funcName(results)
                end
            end
            audio.play( vars.wswBlue, { channel=1, onComplete= rloadClosure } )
        end

Suggests the function didn’t get passed - but it has been following the syntax described above.

 

You’re going to have to post more code.  It will be quite helpful to see your module’s structure.

Ok, cool. Here’s more of the actual code. Using Director. If you need more, I could dropbox the 2 actual pages?

Be aware that labels like funcName and ModA used above were hyperthetical. So in the actual code substitute the following:

local funcName = local propButton

newMod = screens

“modA” = “screens”

page29.lua

module(…, package.seeall)

function new()
        local menuGroup = display.newGroup()
        local drawScreen = function()
        local propButton

        local screens = require(“screens”)
        screens.new(propButton)

        – Code here to load images

        screens.screen1()
        local objectList = screens.objectList

        propButton = function (results)

            for i = 1, #objectList do
                local button =  display.newImageRect ( imgDir… objectList[i].name…".png", objectList[i].w, objectList[i].h )
                button.name = objectList[i].name
                – etc…
            end

        end

        – more code here
    end
    drawScreen()
    return menuGroup
end

screens.lua

local M = {}

    M.new = function (propButton)

        – Code here to load images

        M.screen1 = function()    
                M.objectList =
                {
                    { name=“kwkchina”, w=345, h=300, x=177.5, y=288},
                        --etc…
                }
        end

        function act_audChter (event)

            local isChannel1Playing = audio.isChannelPlaying( 1 )
            if isChannel1Playing then
                audio.stop( 1 )
            end

            local rloadClosure = function()
                if rloadFlag then
                    local results = {}
                    propButton(results)
                end
            end

            audio.play( vars.wswBlue, { channel=1, onComplete= rloadClosure } )

        end

        – more code here

    end
return M

 

All that looks right.  What is the value of rloadFlag?  Perhaps you should put some print statements in and see if you can see how the app is flowing.

Rob

Got this working by changing the syntax of a call to this:

page29

screens.new({scNumber = i, propButton = propButton})  

screens

M.new = function (params)
    params = params or {}

    local sceneNumber = params.scNumber
    local propButton = params.propButton

Then later in screens.lua calling propButton() initiates that self named function in page29.

Very cool and now opens up lots of opportunities…