Can a GameLoop get too big?

So my GameLoop (The function which always runs to check for stuff during a game) has been working pretty good.

However I added a line to my loop, and suddenly I get an error saying:

function at line 972 has more than 60 upvalues

My function contains a couple of if statements, but not doing like more than 60 things.

Is this happening because my function contains too many if statements? (There is like about 10)

Most of my if statements are mostly for example:

if var1 == 1 then

var2.x=200

doFunction()

–blaa blaa blaa

end

EDIT: By the way, If I add a print there, i wont get an error

usually that message means you are missing an “end” somehwere, although things can get too big but I’ve never encountered it. It was always a missing end, which means the function gets huge covering most of your code from that point on.

should probably look like this:

 

if var1 == 1 then

var2.x=200

doFunction()

–blaa blaa blaa

end)

end

To test it out, I added a line to the function:

print("hey")

and it works.

I have checked and I am not missing any ends

se my corrected code above, it has one end missing, the function also need an end with a “)” after

wait…why would you need to add two ends?

By the way, this was my full error:

 error loading module 'scene1' from file 'C:\Users\Buscey\Documents\Corona Projects\TheGame\scene1.lua': 17:01:01.270 C:\Users\Busceyt\Documents\Corona Projects\TheGame\scene1.lua:1169: function at line 971 has more than 60 upvalues 17:01:01.270 stack traceback: 17:01:01.270 [C]: in function 'error' 17:01:01.270 ?: in function 'gotoScene' 17:01:01.270 C:\Users\Buscey\Documents\Corona Projects\TheGame\home.lua:16: in function '?' 17:01:01.270 ?: in function \<?:190\>

my bad, thought you did a function directly there, but see now you call an external one.

your code is good and you are likely missing an end somewhere else in your code

the case with this error is the error message wont recognize it and tell you where it is since the code just tries to close the function further down…generating the max 60 upvalues error once that limit is reached.

you should probably google “lua max 60 upvalues error” to understand what this means.

but i bet its a missing “end”  :wink:

you need to step through the code the hard way

If I was missing an end it would look like this:

But right now it looks like this:

Should I just make another gameloop or something?

EDIT: Excuse my bad hand writing

well then you have more than 60 upvalues in the main code or in a function.

its a lua limitation and while you can easily fix it, you first need to find out where there problem is.

each function as well as the main document (being a function in itself) can have max 60 upvalues, which translates to max 60 variables if i recall right.

find the place it happens and divide the code up into at least 2 functions and problem solved.

your handwriting is just like everyones when writing with a mouse  :smiley:

which IDE are you using btw?

Haha yah, I actually use touchpad; not a mouse kind of guy,

The IDE is Notepad++

If for example:

--Old Gameloop local function Gameloop() if var1==1 then var2.text="Yoooooo" var3.x=150 doFuntion() print("updated") end Runtime:addEventListener("enterFrame",Gameloop)

And I change it to this:

--New Gameloop local function updateGame() var2.text="Yoooooo" var3.x=150 doFuntion() print("updated") end local function Gameloop() if var1==1 then updateGame() end Runtime:addEventListener("enterFrame",Gameloop)

Would that reduce the number of upvalues?

Tip: Don’t use a game loop at all.

Instead, spread your logic out into small event driven functions.

None of my games use a game loop.

the first part is missing and end to close the gameloop function.

both the IF statement and the GAMELOOP function need one END each, and you code only has a single end.

told ya its a missing end =P

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!