[SOLVED] 200 Local Variables Limit Problem

Hello Community, I’m writing my code in just the main.lua file, I;m not using modules and packages, but I’m having a problem, the terminal says that I cannot create more than 200 local variables on the main.lua, so what can I do? I need much more than that. [import]uid: 81091 topic_id: 23653 reply_id: 323653[/import]

Couldn’t you just place all of those “varibles” into some sort of table and call them later using only one line of code?

[lua]local tablething = {
1 = localVarible1
2 = 502304
3 = whatever
}

if (tablething[3]==whatever) then
print “do something”
end[/lua]

Would something like this work?

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23653 reply_id: 94972[/import]

Hmm, I don’t know, yeah maybe I can do something with that. I’ll try it. Thank you NinjaPig. [import]uid: 81091 topic_id: 23653 reply_id: 94975[/import]

I would do something similar to what Ninja Pig suggested, but in a more object like fashion.

local game = {}
game.playerName = “Barney Rubble”
game.highScore = 0
game.var1 = xyz
game.someTable = {}
game.someTable.blurb = “asjldfjadf”
etc.

One local variable used, plenty of variable space to use.

[import]uid: 19626 topic_id: 23653 reply_id: 94980[/import]

Yeah Rob,

That’s actually a better way to approach this issue. That’s an amazing way to do it since all you need to do is pass the object and you can reference it in any function! [import]uid: 29181 topic_id: 23653 reply_id: 94982[/import]

And one more trick I learned as I experimented with my code (and thought it was amazing) is, if/when it makes sense, add a whole function in a table. So, taking Rob’s example, you can do:

[lua]function game.myFunction()
– do this or that
end[/lua]

There might be some sort of performance hit for a table look up, but if a specific function isn’t so performance sensitive, it might be an option for decreasing the number of upvalue.

Naomi [import]uid: 67217 topic_id: 23653 reply_id: 94992[/import]

Oh, yeah, that’s all what I need. Thank you guys. [import]uid: 81091 topic_id: 23653 reply_id: 94993[/import]

*Absolutely* you should group alot of variables into local tables. Not only will this avoid the limit of 200 local variables, it’s FAR better programming technique, it’s faster and more memory-efficient, it’s better for organization, etc. etc.

You also can call them easily at the function-level. As so…

[code]
local variableTable = { var1={}, var2=3, var3=“value” }

local function testFunction ()
local neededVariable = variableTable[“var1”]
neededVariable. --(do something with it)
end
[/code] [import]uid: 9747 topic_id: 23653 reply_id: 94999[/import]

Just a reminder that Lua lets you access your table members either with dot notation or bracket notation.
x = mytable.fred

is the same as

x = mytable[“fred”]

[import]uid: 19626 topic_id: 23653 reply_id: 95001[/import]

Yes, I fixed the problem using tables to reduce the number of local variables. Thanks again guys. [import]uid: 81091 topic_id: 23653 reply_id: 95013[/import]

Before you post a new thread on optimzation,

mytable.variable = “Non optimised”

local myTableVariable = “Optimised”

the . variables are slower than the local variables, so if you are after speed and performance (which you will need) you can also start to refer to the table member variables with a local variable.

[import]uid: 3826 topic_id: 23653 reply_id: 95015[/import]

@jayan I’ve using the local variables, but I can’t use more than 200, so I need to use tables or modules and packages. But I’m not sure which is better, what do you think? [import]uid: 81091 topic_id: 23653 reply_id: 95036[/import]

@Devsa Lad, I know that the issue started with the local variables. This is a rather strange situation, because I personally believe that well written code should not have more than 200 vars, however using the table method is a recommended way to reduce the variables. Further more you can use local variables in the context of each function. Which also brings up the point that your code should be modular and be broken into functions to handle redundancy.

so for example,

you can have

local myVariables = {}  
--add all the global variables that you need into this table  

then

local function myFunc1(tableParam)  
 local myVar1 = tableParam.var1  
 local myVar2 = tableParam.var2  
 local myVar3 = tableParam.var3  
  
 -- now use the vars in here  
 print(myVar1, myVar2, myVar3)  
end  

this will offer you better performance than

local function myFunc1(tableParam)  
 local myVar1 = tableParam.var1  
 local myVar2 = tableParam.var2  
 local myVar3 = tableParam.var3  
  
 -- now use the vars in here  
 print(tableParam.var1, tableParam.var2, tableParam.var3)  
end  

hope that helps you,

Jayant Varma [import]uid: 3826 topic_id: 23653 reply_id: 95038[/import]

@devsalad,

Look above at my code sample. I think what Jayant is saying is that you shouldn’t “up-value” reference a variable stored in a table many times (especially in a big loop). An “up-value”, if you’re not familiar with the term, happens every time a function calls a variable which is declared *above* and outside (hence “up”) that function. This is definitely slower in terms of performance and many Lua references will say the same.

So, to improve performance, you “re-declare” the variable(s) you need INSIDE the function, then use those new local references throughout the function. It’s the exact same variable you’re referring to, and you’re simply declaring a new local “pointer” to that variable within the function. That means you just up-value ONE time, and thus, performance is improved. :slight_smile:

@Jayant
If this isn’t what you mean, I apologize; please correct my statement above if it’s wrong. [import]uid: 9747 topic_id: 23653 reply_id: 95042[/import]

@IgnisDesign exactly.
[import]uid: 3826 topic_id: 23653 reply_id: 95045[/import]

@Ignis, @ jayatv Thank you guys for the info, I’ll work with tables and then localize them into the functions with the table values. [import]uid: 81091 topic_id: 23653 reply_id: 96499[/import]

This thread is definitely useful for anyone using Corona for game development.I do have a question/request for guidance, as I’m not sure how to achieve my desired ends without using local functions.

I have an object, with an object.id variable. When I touch the object, the object.id is passed to the touch function, making it a self.id. I have a function checking the self.id, calling certain functions from within an external module, depending on the value of the self.id. Below is an example:

[lua]playerItem1 = display.newText(“itemTest1”, 0,0, fontName, fSz)
playerItem1:setTextColor(0,0,0)
playerItem1.touch = popPlayBox1
playerItem1:addEventListener(“touch”, playerItem1)
playerItem1.id = “itemTest1”[/lua]

and here is the corresponding touch handler:

[lua] if self.id == “itemTest1” then
item1 = gameitempop.createitemTest1()
item1.x = playBox1.x-35
item1.y = playBox1.y
item1.touch = invokeItem1
item1:addEventListener(“touch”, item1)
itemCost = display.newText(“Cost:”…item1.cost, fontName, sz)
itemCost:setTextColor(0,0,0)[/lua]

and here is the corresponding function from gameitempop:

[lua]–gameitempop.lua
local function createCommerceItem()
item1 = display.newImage(“media/images/commerce/testItem1.png”)
item1:setReferencePoint(display.CenterReferencePoint)
item1.x = 0
item1.y = 0

end[/lua]

The item above, as is seen, then takes certain variables from the function being called from the external module. Is there a way to accomplish this in the fashion you describe above? I’m confused as to how one would call several different items from within one main local variable. If I use Brent’s suggestion, my touch handler doesn’t recognize the function and create the specified item. I’m also not sure how to adapt Jayant’s code to my purposes above. Any assistance or tips would be of great assistance. Thanks in advance! [import]uid: 135394 topic_id: 23653 reply_id: 140898[/import]

Pa, realize Ima newb, but this may be what you are after. This is a great thread and wish it existed when I started over a year ago.

I’m thinking you want to create several items with 1 external module, here’s how I do it. This isn’t complete and not simulator tested but should give you some ideas to go forward. The problem I haven’t overcome is how to pass tables back and forth between modules without declaring the tables as “globals”, so some of the tables I’m creating are not local’s.

You want to rename the way you name your playerItems to take advantage of table indexing and add another .Index reference to them so you can iterate over the table.

--spawn playItems...something like this  
local gameitempop = require("gameitempop")  
  
local playerItem = {}  
local item = {} --needed for code further below  
  
for i = 1, 10 do --this assumes you have 10 different playerItems you have available (coins, stars, armor...)  
 playerItem[i] = display.newText("itemTest"..i , 0,0, fontName, fSz)  
 playerItem[i].x = 100  
 playerItem[i].y = i \* 35 --Should stack items text in a column  
 playerItem[i]:setTextColor(0,0,0)  
 playerItem[i].touch = popPlayBox1  
 playerItem[i]:addEventListener("touch", touchHandler) --renamed your touch handler  
 playerItem[i].id = "itemTest"..i  
 playerItem[i].Index = i --here is the [i] reference you'll use later  
end  
  

Now modify your touch handler…place it above the code above.

local function touchHandler(event)  
  
IndexReference = event.target.Index --passes on the [i] reference,   
  
if event.phase = "ended" then  
 for i = 1, #playerItem do --here you iterate through the "playerItem" table to find a match  
 if self.id == "itemTest"..IndexReference then  
 item[IndexReference] = gameitempop.creatCommerceItem() --renamed to match function in module, not sure exactly how this works  
 item[IndexReference].x = playBox1.x-35  
 item[IndexReference].y = playBox1.y   
 item[IndexReference].touch = "invokeItem"..IndexReference -- I declared this as string, not sure what is going on here, you can iterate over the reference later when needed like I did above now that the string ends with the Index reference  
 item[IndexReference]:addEventListener("touch", item) --not sure what function does, probably have to reference index number  
 itemCost = display.newText("Cost:"..itemCostTable[IndexReference].cost.. "", fontName, sz) --you'll have to rename your table here, because I am using the item{} already.   
 itemCost:setTextColor(0,0,0)  
 else  
 end  
 end  
end  
end  
  

I guess in your gameitempop.lua do something like this, I don’t really see the need for integrating this module as the code could very easily be included in the block above, but …

--gameitempop.lua   
  
local gameitempop{}  
  
 local function createCommerceItem()  
 item[IndexReference] = display.newImage("media/images/commerce/testItem"..IndexReference..".png")  
 item[IndexReference]:setReferencePoint(display.CenterReferencePoint)  
 item[IndexReference].x = 0  
 item[IndexReference].y = 0  
  
 end  
 gameitempop.createCommerceitem = createCommerceitem --need reference to module function  
  
return gameitempop  
  

Are you after something like this?

Hope this helps,

Nail [import]uid: 106779 topic_id: 23653 reply_id: 140931[/import]

This thread is definitely useful for anyone using Corona for game development.I do have a question/request for guidance, as I’m not sure how to achieve my desired ends without using local functions.

I have an object, with an object.id variable. When I touch the object, the object.id is passed to the touch function, making it a self.id. I have a function checking the self.id, calling certain functions from within an external module, depending on the value of the self.id. Below is an example:

[lua]playerItem1 = display.newText(“itemTest1”, 0,0, fontName, fSz)
playerItem1:setTextColor(0,0,0)
playerItem1.touch = popPlayBox1
playerItem1:addEventListener(“touch”, playerItem1)
playerItem1.id = “itemTest1”[/lua]

and here is the corresponding touch handler:

[lua] if self.id == “itemTest1” then
item1 = gameitempop.createitemTest1()
item1.x = playBox1.x-35
item1.y = playBox1.y
item1.touch = invokeItem1
item1:addEventListener(“touch”, item1)
itemCost = display.newText(“Cost:”…item1.cost, fontName, sz)
itemCost:setTextColor(0,0,0)[/lua]

and here is the corresponding function from gameitempop:

[lua]–gameitempop.lua
local function createCommerceItem()
item1 = display.newImage(“media/images/commerce/testItem1.png”)
item1:setReferencePoint(display.CenterReferencePoint)
item1.x = 0
item1.y = 0

end[/lua]

The item above, as is seen, then takes certain variables from the function being called from the external module. Is there a way to accomplish this in the fashion you describe above? I’m confused as to how one would call several different items from within one main local variable. If I use Brent’s suggestion, my touch handler doesn’t recognize the function and create the specified item. I’m also not sure how to adapt Jayant’s code to my purposes above. Any assistance or tips would be of great assistance. Thanks in advance! [import]uid: 135394 topic_id: 23653 reply_id: 140898[/import]

Pa, realize Ima newb, but this may be what you are after. This is a great thread and wish it existed when I started over a year ago.

I’m thinking you want to create several items with 1 external module, here’s how I do it. This isn’t complete and not simulator tested but should give you some ideas to go forward. The problem I haven’t overcome is how to pass tables back and forth between modules without declaring the tables as “globals”, so some of the tables I’m creating are not local’s.

You want to rename the way you name your playerItems to take advantage of table indexing and add another .Index reference to them so you can iterate over the table.

--spawn playItems...something like this  
local gameitempop = require("gameitempop")  
  
local playerItem = {}  
local item = {} --needed for code further below  
  
for i = 1, 10 do --this assumes you have 10 different playerItems you have available (coins, stars, armor...)  
 playerItem[i] = display.newText("itemTest"..i , 0,0, fontName, fSz)  
 playerItem[i].x = 100  
 playerItem[i].y = i \* 35 --Should stack items text in a column  
 playerItem[i]:setTextColor(0,0,0)  
 playerItem[i].touch = popPlayBox1  
 playerItem[i]:addEventListener("touch", touchHandler) --renamed your touch handler  
 playerItem[i].id = "itemTest"..i  
 playerItem[i].Index = i --here is the [i] reference you'll use later  
end  
  

Now modify your touch handler…place it above the code above.

local function touchHandler(event)  
  
IndexReference = event.target.Index --passes on the [i] reference,   
  
if event.phase = "ended" then  
 for i = 1, #playerItem do --here you iterate through the "playerItem" table to find a match  
 if self.id == "itemTest"..IndexReference then  
 item[IndexReference] = gameitempop.creatCommerceItem() --renamed to match function in module, not sure exactly how this works  
 item[IndexReference].x = playBox1.x-35  
 item[IndexReference].y = playBox1.y   
 item[IndexReference].touch = "invokeItem"..IndexReference -- I declared this as string, not sure what is going on here, you can iterate over the reference later when needed like I did above now that the string ends with the Index reference  
 item[IndexReference]:addEventListener("touch", item) --not sure what function does, probably have to reference index number  
 itemCost = display.newText("Cost:"..itemCostTable[IndexReference].cost.. "", fontName, sz) --you'll have to rename your table here, because I am using the item{} already.   
 itemCost:setTextColor(0,0,0)  
 else  
 end  
 end  
end  
end  
  

I guess in your gameitempop.lua do something like this, I don’t really see the need for integrating this module as the code could very easily be included in the block above, but …

--gameitempop.lua   
  
local gameitempop{}  
  
 local function createCommerceItem()  
 item[IndexReference] = display.newImage("media/images/commerce/testItem"..IndexReference..".png")  
 item[IndexReference]:setReferencePoint(display.CenterReferencePoint)  
 item[IndexReference].x = 0  
 item[IndexReference].y = 0  
  
 end  
 gameitempop.createCommerceitem = createCommerceitem --need reference to module function  
  
return gameitempop  
  

Are you after something like this?

Hope this helps,

Nail [import]uid: 106779 topic_id: 23653 reply_id: 140931[/import]