Modulated Function returning nil value

Hello all.

I have been working on trying to get this to work all day. I want to move a set of global functions to a modular file. My first attempt is to try creating a function to pass a scene name to and have that function change scenes. Is this possible to do? Here is the code I came up with but no matter what I do it just returns a nil value. I am new to Lua. I was reading all the material I could find on modulation and passing to functions. Any help would be appreciated.

Here is a compressed version of all perftinent code in my menu.lua file

local composer = require( "composer" )
local gF = require("globalFunctions")

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------


-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()
function scene:create( event )

	--Menu Buttons
    ----Play Button
	local playButton = display.newText( sceneGroup, "PLAY", display.contentCenterX, 240, native.systemFont, 60 )
    playButton:setFillColor( 0.82, 0.86, 1 )
    playButton:addEventListener( "tap", gF.switchScene(game) )
	
	----Credits Button
	local creditsButton = display.newText( sceneGroup, "CREDITS", display.contentCenterX + 165, 270, native.systemFont, 30)
    creditsButton:setFillColor( 0.82, 0.86, 1 )
    creditsButton:addEventListener( "tap", gF.switchScene(credits) )
	
end

-- show()
     --etc...
-- hide() 
     --etc...
-- destroy()
     --etc...

-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
     --etc...
-- -----------------------------------------------------------------------------------

return scene

Here is the globalFunctions.lua file it requiresā€¦

------------------------------------------------------
-- Pseudo-global space  ///FUNCTIONS
------------------------------------------------------
local gV = require( "globalVariables" )

local M = {}
--Navigation
M.switchScene = function(toScene)
    composer.gotoScene( ""..toScene.."", { time=800, effect="crossFade" } )
end
	
return M

You need require composer module in globalFunctions.lua
In scene file you try
playButton:addEventListener( ā€œtapā€, function() gF.switchScene(ā€œpath.to.sceneā€) end )
playButton:addEventListener( ā€œtapā€, gF.switchScene(game) ) ā†’ game is a variable nil, function need a string value.

Thank you, I added the composer. I recognize that it needs a string value. Except it keeps ending up being a nil value. I passed a string to the function, but it didnā€™t make a difference. This makes me wonder if it might also be a scope thing due to the modular separation. I changed the menu.lua code to this:

	gF.toScene = "game"
	playButton:addEventListener( "tap", gF.switchScene() )
	

and I changed the globalFunctions.lua code to this:

M.toScene = "menu"
M.switchScene = function()

	composer.gotoScene( toScene, { time=800, effect="crossFade" } )

end

Now I am getting a new error message:


menu.lua47: attempt to index field ā€˜switchSceneā€™(a function value)
stack traceback:
ā€¦ menu.lua47:in function ā€˜methodā€™
ā€¦ D:\a\corona\platform\resources\init.lua220: in funion ā€˜dispachEventā€™

D:\a\corona\corona\subrepos\composer\composer.lua1474: in function ā€˜gotoSceneā€™
ā€¦ main.lua24: in main chunk


Hmm, that error message seems a little inconsistent with the code you show there.

Anyhow, a couple things do look off:

playButton:addEventListener( "tap", gF.switchScene() )

By adding parentheses, youā€™re calling the function on the spotā€”and adding the ā€œresultā€ (nothing) as the listener, rather than the switchScene function.

You probably want to be passing M.toScene to gotoScene, no?

The effect I am after is a function located on the globalFunction.lua file which can change a runtime scene to the scene which the toScene variable points to and able to be actiivated by any lua file which has access to globalFunction.lua.

I think that made sense. :upside_down_face:

I took off the ā€˜()ā€™. I keep forgetting those arenā€™t required in the syntax here. So, thank you for noticing that.

The error message has changed now to:

D:\a\ corona\corona\subrepos\composer\composer\lua:1457:
D:\a\corona\coronoa\platform\resources\init.lua:839: bad argument #1 toā€™findā€™ (string expected, got nil)
stack traceback:
ā€¦[C]: in fiunction ā€˜errorā€™

D:\a\corona/\corona\subrepos\composer\composer.lua:1457:
in function ā€˜gotoSceneā€™
ā€¦globalFunctions.lua:12 in function ā€˜funcā€™
ā€¦D:\a\corona\coronoa\platform\resources\init.lua:202:
in function
<D:\a\corona\coronoa\platform\resources\init.lua:189>

Iā€™m guessing that one is per what I mentioned in the last comment.

You have gF.toScene = "game" and M.toScene = "menu". But as an argument to composer.gotoScene(), you only pass it as toScene.

What it ends up trying to do is either look up a global (which doesnā€™t in fact exist) or find a local value earlier in the scope, i.e. the file globalFunctions.lua. Iā€™m assuming you did not in fact want that behavior, but rather:

    composer.gotoScene( M.toScene, { time=800, effect="crossFade" } )

This was the issue, I forgot to put the M. on there. I love when the issue is clerical like this. Easy to repair. :smiley: Thank you for the extra pair of eyes. It is most appreciated!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.