Predeclaring Variables and upvalue

Hi Folks!

I’m in a position where i need to predeclare 90 local images, these will control the level stars on the level select screen.

I added them by using

local Level1Stars

but it sent me an error saying the upvalue of that function is above 60.

After some research i discovered why i can’t do this.

My question is, instead i am using a local table to contain my upvalues.

Can you tell me if this is right and safe?

Predeclaring images varaibles

local World1Stars = {  
 World1Stars1, World1Stars2, World1Stars3...  
 }  

Within the function

[code]…
World1Stars1 = display.newImage(…)_W, _H)

World1Stars2 = display.newImage(…)_W, _H)

World1Stars3 = display.newImage(…)_W, _H)

[/code]
I ask because, if i don’t do the local table, it works anyway, but this is because they’re then all global variables!

So this means they’re all local yes? while reducing upvalues?

Thanks, Matt

PS: My games going awesomely :wink:
[import]uid: 91798 topic_id: 18419 reply_id: 318419[/import]

Yes, I think that is correct. The table will be local. And the fields in the table will only be accessible from your local table.
[import]uid: 7563 topic_id: 18419 reply_id: 70671[/import]

If the values of World1Stars1, World1Stars2, World1Stars3 (etc.) are not defined as local variables before the code posted from your predeclaration you are definitely using global variables to store your images within the function. Instead I think you want:

Predeclaration

local World1Stars = { }
Within the function

... World1Stars[1] = display.newImage(...)\_W,\_H) World1Stars[2] = display.newImage(...)\_W,\_H) World1Stars[3] = display.newImage(...)\_W,\_H) ...

alternately if you want to keep the names you simply change the function to:

... World1Stars.World1Stars1 = display.newImage(...)\_W,\_H) World1Stars.World1Stars2 = display.newImage(...)\_W,\_H) World1Stars.World1Stars3 = display.newImage(...)\_W,\_H) ...
or

... World1Stars["World1Stars1"] = display.newImage(...)\_W,\_H) World1Stars["World1Stars2"] = display.newImage(...)\_W,\_H) World1Stars["World1Stars3"] = display.newImage(...)\_W,\_H) ...

Admittedly I’m a little confused by the syntax of your newImage code so feel free to correct me if I’m misunderstanding something.
Nathan [import]uid: 100558 topic_id: 18419 reply_id: 70687[/import]

Nathan is correct. Listen to Nathan. I wasn’t paying close enough attention to the original code, just the concept.
[import]uid: 7563 topic_id: 18419 reply_id: 70688[/import]

Ahh now then,

I chose to do the 2nd choice in naming the variables.

World1Stars.World1Stars1 = display...  

It is abit odd syntax wise but nevermind for now.

I did that and it’s saying that the functions upvalue is more than 60 again. >:[

I do have more than 60 in the function but i thought putting them into a local table and calling them from there only counted as 1 upvalue then?

Is there something i am missing?

(Do i need to decalre all the variables in the table when i create the table (This is what i’ve done)
Or create them on the fly in the function?

Thanks again. [import]uid: 91798 topic_id: 18419 reply_id: 70726[/import]

I think i may have solved this by rearranging the part of the code that searches for World1Stars1 images,
and placed it at the bottom of the function they’re called in. [import]uid: 91798 topic_id: 18419 reply_id: 70778[/import]

For the question I can answer with certainty, you do not need to define the “variables” in the table. If desire to do so for clarity’s sake the definition should look like this:

local World1Stars = { World1Stars1 = nil, World1Stars2 = nil ... }  

I’m not sure what would be causing your error with upvalues as I’ve never seen such an error myself. One thing to keep in mind is that where you define the table does matter for instance in Example 1 below the call to myFunc() is not accessing the local table World1Stars defined below the definition of myFunc() but instead it is attempting to access a global table named World1Stars. In order for the function to access the local table the table must be defined before the function as in Example 2 below. Not sure that this is your issue but hope it helps.

Example 1 (incorrect):

[code]local function myFunc()
World1Stars.World1Stars1 = newImage(…)
end

local World1Stars = { }

myFunc()[/code]
Example 2 (correct):

[code]local World1Stars = {}

local function myFunc()
World1Stars.World1Stars1 = newImage(…)
end

myFunc()[/code]
Nathan [import]uid: 100558 topic_id: 18419 reply_id: 70849[/import]