I’ve been setting up my own kind of scene system that moves around by saying scene = “mainmenu” and calling refresh(). refresh then checks what the scene is, and adds the right event listeners, removes the ones from other scenes, sets the right variables, etc.
I have two problems/questions:
First, about having to forward declare variables. Ideally, I’d like to have parts within the refresh function that set the right variables.
function refresh()
if(scene === “play”) then
x = 50
y = 100 – etc
end
end
the problem here is that if these variables aren’t forward declared, they’re either local and stuck within that function and inaccessible from other functions, or global (and the internet doesn’t seem to like people doing this). do I have to reserve a block of text at the start of my file just forward declaring everything? that’s a lot of lines and a lot of copy and pasting, and feels like something that could be avoided
(update: I’ve also learnt that “function at line has more than 60 upvalues” is a thing, making everything even more difficult… seems you can’t refer to more than 60 “outside” variables within a function, without some messy workarounds. for now I’ve reduced it to less than 60, but maybe for future projects I should use closeMainMenuScene(), closePlayScene(), openMainMenuScene() etc functions instead of one big refresh() function that I put everything in)
#2: not sure how I’m meant to order my code, what with the single scan system, and listeners having to go after functions.
Say I have my refresh function that creates or removes various listeners for functions. This has to go at the end of my code, because the functions it refers to go above.
But then throughout my code and within these functions themselves, I have calls to change scene and refresh(). But because refresh is only defined at the end, I get errors and this doesn’t work out. If I put refresh at the top, I get errors adding my event listeners for the button functions down below. How do i make it work?
A non-literal example of this ordering problem:
local function pressplay(event)
scene = “play”
refresh()
end
local function refresh()
if(scene == “play”)
removeEventListener(“touch”, pressplay)
end
if(scene == “mainmenu”) then
playbutton:addEventListener(“touch”, pressplay)
end
end
the error here being me defining a function that calls refresh() before having defined refresh(). but put them the other way round and I’m creating a listener for a function pressplay() before I’ve defined pressplay().
thanks!