200 local limit

I’m getting the following error

Syntax error: …/folders/Wu/WuqVRuCIGMupXIf3SExe0k+++TI/-Tmp-/TemporaryItems/39/main.lua:7031: main function has more than 200 local variables

anyway out of this?

the project is a commercial quality game for a major publisher… 8000 lines of code in one file

right now I’m just removing the local declaration on new variables and functions, I’m sure I’m using more than 200, so does anyone know the the ‘rules’ as what is counted as the main project AND / OR how to increase

Thanks
[import]uid: 3093 topic_id: 17198 reply_id: 317198[/import]

the project is a commercial quality game … 8000 lines of code in one file

That’s a bit of an oxymoron right there and also going to be the start of your issues.

Having all that code in one file is going to be a nightmare to manage, here is a quick tutorial on Modules that should help you get out of that issue - http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

To solve the 200 locals issue you are having you will want to use tables, for instance if you had all these variables:

local playerHealth = 100  
local playerScore = 20  
local playerAmmo = 35  
local playerState = "WALKING"  

You could put them all in a table like so:

local player = {} player.health = 100 player.score = 20 player.ammo = 35 player.state = "WALKING" [import]uid: 5833 topic_id: 17198 reply_id: 64844[/import]

modules the way to go
I bet there’s a lot of duplicate code is in that 8000 lines
some very good commercial games have been made with way less code [import]uid: 7911 topic_id: 17198 reply_id: 64850[/import]

Not being critical, but when you self proclaim

“the project is a commercial quality game for a major publisher… 8000 lines of code in one file”

you leave no doubt about the project, the choice of the major publisher and your skills.

Graham has the Lime library, Jeff has the Particle system, these two are highly optimised libraries.

surprisingly… no one has actually ever crossed the 200 variables mark, I would recommend that you have a read about how to develop better, if you cannot use the structures (as they are called under C) then try using arrays. Make your code modular and manageable.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17198 reply_id: 64860[/import]

Actually I did cross the 200 variable mark in my first game while dealing with tons of events where I can’t pass parameters and not understanding how to modularize things or being ignorant of adding values to existing tables.

The best way to avoid this is to of course modularize your code. 8000 lines in one file is problematic from a maintenance stand point.

Secondly, make use of adding variables to existing tables. Lets take your basic game where you have a player running around (RPG, Platformer, etc.)

You can do:

  
local playerAvatar = display.newImageRect("player.png", 64, 64);  
local playerHealth = 100  
local playerCoins = 0  
local playerLives = 5  
local playerHasRedPowerUp = false  
local playerHasGreenPowerUp = false  
  

which ate up 6 local variables out of your pool of 200. Or you could do:

  
local player = {}  
player.avatar = display.newImageRect("player.png", 64, 64);  
player.health = 100  
player.coins = 0  
player.lives = 5  
player.hasRedPowerUp = true  
player.hasGreenPowerUp = true  

Local variables in use: 1.

Others would have simplfied this:

local player = display.newImageRect("player.png", 64, 64);  
player.health = 100  
player.coins = 0  
player.lives = 5  
player.hasRedPowerUp = true  
player.hasGreenPowerUp = true  

This adds memebers to the display object. The first version would let you remove the graphic without removing the player’s data.

Using this concept, you can go back and clean up your code by implementing tables in this way. Its how I got past the limit. For instance, I took all my screen variables (this was before I discovered and figured out Director):

creditScreen = nil  
creditScreenActive = false  
upgradeScreen = nil  
upgradeScreenActive = false  
scoresScreenActive = false  
highScores = nil  
hangerScreenActive = false  
hangerScreen = nil  
helpScreenActive = false  
helpScreen = nil  

became:

local scr = {}  
scr.creditScreen = nil  
scr.creditScreenActive = false  
scr.upgradeScreen = nil  
scr.upgradeScreenActive = false  
scr.scoresScreenActive = false  
scr.highScores = nil  
scr.hangerScreenActive = false  
scr.hangerScreen = nil  
scr.helpScreenActive = false  
scr.helpScreen = nil  

I had 40 screen related variables/objects, things like the on/off switches for sound and music (two display objects each). This technique compressed it to 1 local variable.

When it was said and done, I moved 101 local variables into 5 tables. Given I only have 200 locals to begin with, this was huge. Keep in mind your function and requires eat up from that count as well. [import]uid: 19626 topic_id: 17198 reply_id: 64885[/import]

Well glad something else has been there. With functions and requires included within the 200 limit I reached it quite easily.

I appreciate the help, using a table to hold all the values obviously works but wasn’t my first thought starting my first programming project in 15 years. Might be time consuming to implement now, any ideas if this is more efficient code?

Also the links about modules, when I started the docs didn’t make that much sense so I left this - but aren’t variables from one module to another global? Also I’m not how much of the code can reside in different modules , it’s beem hard enough making sure each function appears before it’s called.

If there’s no simple max local variable in a setup I can tweak then obviously I’ll look into the above two methods as well as removing the local from the variables I have.

To the guy who just chimed in without any new info, well if your not helping don’t post. These forums are great, there’s a lot to learn Corona is great but the documentation is often sparse and the blog search really doesn’t work very well.
[import]uid: 3093 topic_id: 17198 reply_id: 64887[/import]

I appreciate the help, using a table to hold all the values obviously works but wasn’t my first thought starting my first programming project in 15 years. Might be time consuming to implement now, any ideas if this is more efficient code?

When I started going this route, I thought it was a daunting task. I knew I would miss variables, and really didn’t want to go that route. When you have a big long program like that you box yourself in and its not a fun prospect to fix. But there is a simple answer: Search and Replace. Globally replace isHelpScreenVisible with scr.isHelpScreenVisible… Caught 99% of the cases. The ones it didn’t case, well get what, it was a bug because I misspelled the variable name and my program was broken… So it found bugs by doing it!
Also the links about modules, when I started the docs didn’t make that much sense so I left this - but aren’t variables from one module to another global? Also I’m not how much of the code can reside in different modules , it’s beem hard enough making sure each function appears before it’s called.

Modules can have local variables and globals. Globals of course are accessible from any module or main.lua. Probably the preferred way is to think in an object oriented manner and have your variables local to the module and uses getter/setter functions or uses table elements that have been added to an object.

Lets look at a common example using a Director class style method:

-- external module scores.lua--  
module(..., package.seeall)  
  
function new(params)  
 local scoreData = {}  
 scoreData.score = 0  
 scoreData.playerName = ""  
 return scoreData  
end  
  
-- main.lua --  
local scores = require("scores")  
local myScores = scores.new()  
...  
myScores.score = myScores.score + 1  
myScores.playerName = "Fred"  

or something similar!
If there’s no simple max local variable in a setup I can tweak then obviously I’ll look into the above two methods as well as removing the local from the variables I have.

No. 200 is a hard coded Lua limit.
[import]uid: 19626 topic_id: 17198 reply_id: 65053[/import]