A Fuse Powered Company

Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

[SOLVED] 200 Local Variables Limit Problem
Started by devsalad Mar 20 2012 08:12 AM

18 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

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.
uid: 81091 topic_id: 23653 reply_id: 323653


[TOPIC: post.html]
#2

Ninja Pig Studios

[GLOBAL: userInfoPane.html]
Ninja Pig Studios
  • Pro
  • PipPipPipPipPipPip
  • 571 posts
  • Jedi

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

local tablething = {1 = localVarible12 = 5023043 = whatever}if (tablething[3]==whatever) then   print "do something"end


Would something like this work?

Regards,
Jordan Schuetz
Ninja Pig Studios
uid: 29181 topic_id: 23653 reply_id: 94972


[TOPIC: post.html]
#3

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

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


[TOPIC: post.html]
#4

robmiracle

[GLOBAL: userInfoPane.html]
robmiracle
  • Starter
  • PipPipPipPipPipPip
  • 2,971 posts
  • Jedi

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.

uid: 19626 topic_id: 23653 reply_id: 94980


[TOPIC: post.html]
#5

Ninja Pig Studios

[GLOBAL: userInfoPane.html]
Ninja Pig Studios
  • Pro
  • PipPipPipPipPipPip
  • 571 posts
  • Jedi

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!
uid: 29181 topic_id: 23653 reply_id: 94982


[TOPIC: post.html]
#6

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Pro
  • PipPipPipPipPipPip
  • 2,303 posts
  • Jedi

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:

function game.myFunction()  -- do this or thatend


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
uid: 67217 topic_id: 23653 reply_id: 94992


[TOPIC: post.html]
#7

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

Oh, yeah, that's all what I need. Thank you guys.
uid: 81091 topic_id: 23653 reply_id: 94993


[TOPIC: post.html]
#8

ignis075

[GLOBAL: userInfoPane.html]
ignis075
  • Pro
  • PipPipPipPipPipPip
  • 877 posts
  • Jedi

*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...

local variableTable = { var1={}, var2=3, var3="value" }

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


[TOPIC: post.html]
#9

robmiracle

[GLOBAL: userInfoPane.html]
robmiracle
  • Starter
  • PipPipPipPipPipPip
  • 2,971 posts
  • Jedi

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"]

uid: 19626 topic_id: 23653 reply_id: 95001


[TOPIC: post.html]
#10

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

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


[TOPIC: post.html]
#11

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Starter
  • PipPipPipPipPipPip
  • 1,740 posts
  • Jedi

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.

uid: 3826 topic_id: 23653 reply_id: 95015


[TOPIC: post.html]
#12

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

@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?
uid: 81091 topic_id: 23653 reply_id: 95036


[TOPIC: post.html]
#13

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Starter
  • PipPipPipPipPipPip
  • 1,740 posts
  • Jedi

@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
uid: 3826 topic_id: 23653 reply_id: 95038


[TOPIC: post.html]
#14

ignis075

[GLOBAL: userInfoPane.html]
ignis075
  • Pro
  • PipPipPipPipPipPip
  • 877 posts
  • Jedi

@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. :)

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


[TOPIC: post.html]
#15

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Starter
  • PipPipPipPipPipPip
  • 1,740 posts
  • Jedi

@IgnisDesign exactly.
uid: 3826 topic_id: 23653 reply_id: 95045


[TOPIC: post.html]
#16

devsalad

[GLOBAL: userInfoPane.html]
devsalad
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

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


[TOPIC: post.html]
#17

Alex@PaNc

[GLOBAL: userInfoPane.html]
Alex@PaNc
  • Pro
  • PipPipPipPipPipPip
  • 935 posts
  • Jedi

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:

playerItem1 = display.newText("itemTest1", 0,0, fontName, fSz)playerItem1:setTextColor(0,0,0)playerItem1.touch = popPlayBox1playerItem1:addEventListener("touch", playerItem1)playerItem1.id = "itemTest1"


and here is the corresponding touch handler:

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)


and here is the corresponding function from gameitempop:

--gameitempop.lua		local function createCommerceItem()		item1 = display.newImage("media/images/commerce/testItem1.png")		item1:setReferencePoint(display.CenterReferencePoint)		item1.x = 0		item1.y = 0		end


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!
uid: 135394 topic_id: 23653 reply_id: 140898


[TOPIC: post.html]
#18

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Pro
  • PipPipPipPipPipPip
  • 270 posts
  • Jedi

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
uid: 106779 topic_id: 23653 reply_id: 140931


[TOPIC: post.html]
#19

Daine v

[GLOBAL: userInfoPane.html]
Daine v
  • Pro
  • PipPip
  • 24 posts
  • Member

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.

uid: 19626 topic_id: 23653 reply_id: 94980

 

THANK YOU! solved a bundle of issues I was having. Needed a simple solution and this is as simple as it gets :)




[topic_controls]
[/topic_controls]