and yeah RG is completely right about that, corona is event driven and you should program accordingly
Woops forgot to add that in the example.
I will try that out
CONCLUSION:
I will leave the GameLoop alone and instead add smaller event functions
If it helps, my games usually have this structure:
- builds.settings
- config.lua
- main.lua
- ssk/ - Not required, but of course I use it.
- scripts/ - Folder for game scripts.
- common.lua - Module I use as scratchpad. I require it in all other scripts (for the most part)
- game.lua - Primary GAME logic. Contains these methods:
- create( group ) - Creates all game content and takes a group as an argument. I create my own child-groups and insert them into that group. I add my objects into the child groups. This makes it ‘composer.*’ ready.
- destroy() - Stop, clean up, destroy game content.
- pause() - Optional function to pause game play.
- resume() - Optional function to start game play back up.
- restart() - Optional function to re-start from beginning.
- continue () - Optional function to re-start where I died.
- player.lua - Module for making player object. Required in game.lua and used there.
- enemy.lua - One or more ‘enemy’ modules. Also used from game.lua
- huds.lua - Interface builder for game play gui. Also used from game.lua
- … etc.
- images/ - Folder for images
- sounds/ - Folder for sounds
- …
If you would like to see an example ‘blank’ project let me know and I’ll put one up on RG_FreeStufflater.
I can do one for and one w/o SSK2
Oh please that would be super helpful!
Thanks!
your original error has nothing to do with the “size” of the function, or how many lines it has – fe, a single unwieldy line could be crafted that produces that error. what it does mean is that your function references too many variables declared in more-outer scopes - which strongly suggests that you are long overdue for a refactoring.
@davebollinger is correct.
I think the limit for locals is 200-ish in Lua.
Trick: If you ever what to know how many locals are in scope at a particular line of code in your file, function, etc. Do this:
-
Add this code in main.lua (near top)
function _G.countLocals( level ) level = level or 2 local i = 1 while(debug.getlocal(level,i) ~= nil ) do local name,value = debug.getlocal(level,i) i = i + 1 end print(“Found: " … tostring( i ) … " Locals” ) return i end
Now, anywhere you want to check you can type:
\_G.countLocals() -- Prints # locals in scope at this point in your code.
200 locals per module, 60 upvalues per closure, hard limits
As promised, two (very basic) starters, one with and one without SSK2:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/03/starters.zip
For better starters, see my marketplace content:
A great starter with pausing, resuming, etc. is: https://marketplace.coronalabs.com/asset/balloon-pop-game-templates
If you only have PayPal, you can go here: https://sellfy.com/roaminggamer
Thanks a lot @roaminggamer! I think Dave is right, I have a lot of variables referenced. Thanks a lot for your help!
Rather than having loads of variables, you can put related ones in tables.
[lua]
local playerScore = 0
local playerHighScore = 300
local playerDead = false
local playerPositions = {0, 0.5, 1.5, 3}
[/lua]
can become:
[lua]
local player = {score = 0, highScore = 300, dead = false, positions = {0, 0.5, 1, 1.5}}
[/lua]
Immediately saving three local variables.