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]