Too many (200) local variables

I have encountered the problem of too many local variables again. This is occurring in my Screen1.lua file and I did make the effort to put all my variables into an array/object like this:

1
2
3
4
5
6
7
8
local splashVariables = {}
        
splashVariables["boolExtraFeatures"] = true
splashVariables["numBallOptionStage"] = 1
splashVariables["numTipsPage"] = 1
splashVariables["numInstructionsPage"] = 1
splashVariables["numAchievementsPage"] = 1
splashVariables["optionsDisplayed"] = false

Can I do something like?

yes

Incidentally, with the way you've written those functions I'm guessing you have a separate function for each image, which is a little insane. Write functions that operate generically on whatever image you pass in, and then pass the image in when calling the function.

Thanks jhocking

I was sure my code was a bit insane - just not quite sure how to restructure it to be honest

For example every time I have a button I have all the following code. Would appeciate suggestions how to make a more generic button code solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        -- ------------------------
        -- Ball Options Back Button
        -- ------------------------
        
        local resumeOptionsBackButton = uiNew.newButton{
                default = "images/backDefault.png",
        over = "images/backHighlight.png",
        hit = true,
                hitWidthExtra = 140,
                hitHeightExtra = 10
                                
        }
 
                
        resumeOptionsBackButton.x = 156; resumeOptionsBackButton.y = 406
        
        local function clickResumeOptionsBackButton ( event )
        
                local buttonVariables = {}
                
                buttonVariables["self"] = event.target
                        
                --local self = event.target
 
                if event.phase == "ended" then
                
                        buttonVariables["bounds"] = buttonVariables["self"].stageBounds
                        buttonVariables["x"] = event.x
                        buttonVariables["y"] = event.y
                        
                        
                        buttonVariables["isWithinBounds"] = buttonVariables["bounds"].xMin <= buttonVariables["x"] and buttonVariables["bounds"].xMax >= buttonVariables["x"] and buttonVariables["bounds"].yMin <= buttonVariables["y"] and buttonVariables["bounds"].yMax >= buttonVariables["y"]
 
                                                
                        if buttonVariables["isWithinBounds"] then
                
                                removeResumeOptions()
                                
                        end
                        
                        
                end                     
 
                
        end
        
        
        resumeOptionsBackButton:addEventListener("touch",clickResumeOptionsBackButton)
 
        ballOptionsPanel:insert(resumeOptionsBackButton)

Well first off, I don't believe that you actually have that code for every single button. Because that code is for a back button, so that would mean all of your buttons are back buttons.

Just going along with what you said however, then you could simply reuse that function with every button, since there's nothing in that function specific to that one image. I mean, you already use event.target instead of resumeOptionsBackButton, which is what you should be doing to keep the function generic to all buttons and not just the one.

In other words, you only have to write the function once and then use the same function in every event listener:

1
2
3
4
backButton[1]:addEventListener("touch",clickResumeOptionsBackButton)
backButton[2]:addEventListener("touch",clickResumeOptionsBackButton)
backButton[3]:addEventListener("touch",clickResumeOptionsBackButton)
etc.

Thanks jhocking - I really appreciate the guidance on this.

Amending my code as follows, I still have 2 locals declared.

You are correct that I do not have lots of back buttons. All my buttons are different buttons so I assume each needs a separate local declaration? Or do I create a table of all by buttons?

Also because each button has different function calls, I am not sure if I could just have one generic function to deal with all buttons unless I was able to pass to the button the functions that need to be called when the button is pressed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        -- ------------------------
        -- Ball Options Back Button
        -- ------------------------
        
        local resumeOptionsBackButton = uiNew.newButton{
                default = "images/backDefault.png",
        over = "images/backHighlight.png",
        hit = true,
                hitWidthExtra = 140,
                hitHeightExtra = 10
                                
        }
 
                
        resumeOptionsBackButton.x = 156; resumeOptionsBackButton.y = 406
        
        local function clickResumeOptionsBackButton ( event )
                
                if event.phase == "ended" then
 
                        local withinBounds = event.target.contentBounds.xMin <= event.x and event.target.contentBounds.xMax >= event.x and event.target.contentBounds.yMin <= event.y and event.target.contentBounds.yMax >= event.y
 
                                                
                        if withinBounds then
                
                                removeResumeOptions()
                                
                        end
                        
                end                     
 
                
        end
        
        
        resumeOptionsBackButton:addEventListener("touch",clickResumeOptionsBackButton)
 
        ballOptionsPanel:insert(resumeOptionsBackButton)

This appears to work and reduces each button by one local declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local splashButtons = {}
 
splashButtons["resumeOptionsBackButton"] = uiNew.newButton{
                default = "images/backDefault.png",
        over = "images/backHighlight.png",
        hit = true,
                hitWidthExtra = 140,
                hitHeightExtra = 10
                                
        }
 
                
        splashButtons["resumeOptionsBackButton"].x = 156; splashButtons["resumeOptionsBackButton"].y = 406
        
        local function clickResumeOptionsBackButton ( event )
                
                if event.phase == "ended" then
 
                        local withinBounds = event.target.contentBounds.xMin <= event.x and event.target.contentBounds.xMax >= event.x and event.target.contentBounds.yMin <= event.y and event.target.contentBounds.yMax >= event.y
 
                                                
                        if withinBounds then
                
                                removeResumeOptions()
                                
                        end
                        
                end                     
 
                
        end
        
        
        splashButtons["resumeOptionsBackButton"]:addEventListener("touch",clickResumeOptionsBackButton)
 
        resumePanel:insert(splashButtons["resumeOptionsBackButton"])
views:14391 update:2011/10/6 9:28:12
corona forums © 2003-2011