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:
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
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.
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:
This was the issue, I forgot to put the M. on there. I love when the issue is clerical like this. Easy to repair. Thank you for the extra pair of eyes. It is most appreciated!