How do I make an if then else statement check something only once?

So I have some code - and I need an if statement to check something only once, and then go to a different function without the code checking again and again.

--Sorry about the whitespace - I used Outlaw and when I copied and pasted it --messed up my code. --I ran this below part twice (patternDot1-2) to get 2 dots: math.randomseed(os.time()) patternDot1 = math.random(9)         print ("pattern dot 1 is " .. patternDot1) --\> print which dot patternDot1 is         --find out which dot patternDot1 is and then blink that dot         if patternDot1 == 1 then             transition.to(dot1, {time = 200, alpha = 0.2, onComplete = glow1Remove})             patternDot1 = dot1         elseif patternDot1 == 2 then             transition.to(dot2, {time = 200, alpha = 0.2, onComplete = glow1Remove})             patternDot1 = dot2 end --end of aforementioned "below part" :) function userDotCopy2()         local function afterGlowRemove2()              if userDot2 == patternDot2 then                 userSelectCorrect2 = true              end             print("2nd dot user selected is: " .. tostring(userSelectCorrect2))             userDotCopy3()         end              local function onTouch2(event)             userDot2 = event.target             function glowRemove2(obj)                 transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove2})             end             transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove2})             userSelectCorrect2 = false         end              dot1:addEventListener("tap", onTouch2)         dot2:addEventListener("tap", onTouch2)     end          function userDotCopy1()         local function afterGlowRemove1()              if userDot1 == patternDot1 then                 userSelectCorrect1 = true              end             print("1st dot user selected is: " .. tostring(userSelectCorrect1))             userDotCopy2() --\> call userDot2 so it doesn't go again, but that isn't working         end              local function onTouch(event)             userDot1 = event.target             function glowRemove(obj)                 transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove1})             end             transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove})             userSelectCorrect1 = false         end              dot1:addEventListener("tap", onTouch)         dot2:addEventListener("tap", onTouch)     end     --start functions     userDotCopy1() end  

In my code, the computer sets two variables - patternDot1 and patternDot2.

Then, it waits for the user to click one of thedots. When one is clicked, the computer turns the alpha of that dot to 0.2 and then 1 again.

It then sets that dot (event.target) to userDot1, and compares userDot1 to patternDot1. Then it goes a second time.

The problem is, on the second time, it is still comparing userDot1 to patternDot1, and making it false since the first dot is, most of the time, different from the second. It isn’t a problem if the user selected the first one false, but what if it’s true? Then it needs to stop comparing. How can I do that?

You need to set a variable, perhaps called

local alreadyChecked = false

Then the first time you say:

if whatever_my_condition_is and not alreadyChecked then

      alreadyChecked = true

      – do your other work

end

or something like that.

I tried that, but unfortunately, didn’t make any difference. Actually, it made alreadyChecked false again when I clicked the second dot.

Can you post your revised code in, with the alreadyChecked variable? 

alreadyChecked should only be set back to false if you are telling it to. My guess is you have made it local to the function, not local to the class, so every time you call the function it is redefined and set to false.

On a separate note, your code has a lot of repetition in it, you would be better off putting your objects into a table, and indexing the table entries in a single function using the index as an argument. That would save you having to have userDotCopy1(), userDotCopy2(), userDotCopy3() etc

Something like this:

function userDotCopy(index) local function afterGlowRemove() if userDot[index] == patternDot[index] then userSelectCorrect[index] = true end print("dot user "..index.." selected is: " .. tostring(userSelectCorrect[index])) userDotCopy[index+1]() end local function onTouch(event) userDot[index] = event.target function glowRemove(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove}) userSelectCorrect[index] = false end for i =1, 2 do dot[i]:addEventListener("tap", onTouch) end end

But please keep in mind that I’ve very quickly pseudocoded that based on your example, so you’d have to put your objects, userSelectCorrect bools etc into tables.

function userDotCopy1() local function afterGlowRemove1() alreadyChecked = false if userDot1 == patternDot1 and alreadyChecked == false then userSelectCorrect1 = true end alreadyChecked = true print("1st dot user selected is: " .. tostring(userSelectCorrect1)) userDotCopy2() --\> call userDot2 so it doesn't go again, but that isn't working end local function onTouch(event) userDot1 = event.target function glowRemove(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove1}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove}) userSelectCorrect1 = false end dot1:addEventListener("tap", onTouch) dot2:addEventListener("tap", onTouch) end --start functions userDotCopy1() end

That’s my revised code (not yet shortened with index and such). So why doesn’t it work?

And thanks alanplantpot for helping me shorten my code, since I’m a newbie I still have to learn how to be more efficient. I just don’t understand what you mean by I would have to put the objects and bools and stuff into tables.

What do you mean by I should have made it local to the class? You mean like the .lua file? I made it global, same result.

And yes, that is true - every time I call the function it is redefined, and unfortunately, set to false.

The reason it’s not working is simple. Look at the line where you check if alreadyChecked == false Now look at the line IMMEDIATELY before it, where you set alreadyChecked to false. So every time you call that function you are setting it to false, then checking if it is false. Then you set it to true, but that doesn’t matter because you set it to false again the next time you call the function. The solution is to remove the alreadyChecked = false line from the function, and instead put: local alreadyChecked = false somewhere BEFORE the function. I would strongly recommend that you look up some of the corona tutorials. This error is a very simple scope issue, and while even an experienced developer could have overlooked it, I sense that’s not the case here. Likewise, if you don’t understand how tables work in Lua you will quickly find yourself being stuck.

Yeah, I know I’m a newbie - and although I watched some of J. A. Whye’s videos, I know I need to learn a lot more…  But thanks for helping anyway.

I did change the code, but can I just say that it didn’t work the way I wanted it to. Yep, when I clicked patternDot1, it made alreadyChecked true, but userSelectCorrect false. 

Well it’s difficult to say exactly why that could be, my guess would be that this in statement:

if userDot1 == patternDot1 and alreadyChecked == false then userSelectCorrect1 = true end

the userDot1 == patternDot1 part is returning false, and the alreadyChecked == false part is returning true. Because both statements are not returning true, the userSelectCorrect1 = true is not triggered. Then, because alreadyChecked gets set to false, the next time you reach this function even if userDot1 == patternDot1 is true, the second clause is false so userSelectCorrect1 will never get set to true.

Try moving the alreadyChecked = true line up into the if statement like so:

if userDot1 == patternDot1 and alreadyChecked == false then userSelectCorrect1 = true alreadyChecked = true end

I did that… I even did this:

if userDot1 == patternDot1 and not alreadyChecked then userSelectCorrect1 = true alreadyChecked = true else alreadyChecked = true end --even though I had put this before: --but it didn't run alreadyChecked = true

Which, actually, is quite inconvenient because I don’t want to be making elses when I don’t really need it.

What it prints out when I press the second dot and the computer selected the second dot different from the first dot is:

--in the print statement I changed it a bit to show alreadyChecked 1st dot user selected is true and alreadyChecked is true  1st dot user selected is false and alreadyChecked is true  2nd dot user selected is true

You need to set a variable, perhaps called

local alreadyChecked = false

Then the first time you say:

if whatever_my_condition_is and not alreadyChecked then

      alreadyChecked = true

      – do your other work

end

or something like that.

I tried that, but unfortunately, didn’t make any difference. Actually, it made alreadyChecked false again when I clicked the second dot.

Can you post your revised code in, with the alreadyChecked variable? 

alreadyChecked should only be set back to false if you are telling it to. My guess is you have made it local to the function, not local to the class, so every time you call the function it is redefined and set to false.

On a separate note, your code has a lot of repetition in it, you would be better off putting your objects into a table, and indexing the table entries in a single function using the index as an argument. That would save you having to have userDotCopy1(), userDotCopy2(), userDotCopy3() etc

Something like this:

function userDotCopy(index) local function afterGlowRemove() if userDot[index] == patternDot[index] then userSelectCorrect[index] = true end print("dot user "..index.." selected is: " .. tostring(userSelectCorrect[index])) userDotCopy[index+1]() end local function onTouch(event) userDot[index] = event.target function glowRemove(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove}) userSelectCorrect[index] = false end for i =1, 2 do dot[i]:addEventListener("tap", onTouch) end end

But please keep in mind that I’ve very quickly pseudocoded that based on your example, so you’d have to put your objects, userSelectCorrect bools etc into tables.

function userDotCopy1() local function afterGlowRemove1() alreadyChecked = false if userDot1 == patternDot1 and alreadyChecked == false then userSelectCorrect1 = true end alreadyChecked = true print("1st dot user selected is: " .. tostring(userSelectCorrect1)) userDotCopy2() --\> call userDot2 so it doesn't go again, but that isn't working end local function onTouch(event) userDot1 = event.target function glowRemove(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove1}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove}) userSelectCorrect1 = false end dot1:addEventListener("tap", onTouch) dot2:addEventListener("tap", onTouch) end --start functions userDotCopy1() end

That’s my revised code (not yet shortened with index and such). So why doesn’t it work?

And thanks alanplantpot for helping me shorten my code, since I’m a newbie I still have to learn how to be more efficient. I just don’t understand what you mean by I would have to put the objects and bools and stuff into tables.

What do you mean by I should have made it local to the class? You mean like the .lua file? I made it global, same result.

And yes, that is true - every time I call the function it is redefined, and unfortunately, set to false.

The reason it’s not working is simple. Look at the line where you check if alreadyChecked == false Now look at the line IMMEDIATELY before it, where you set alreadyChecked to false. So every time you call that function you are setting it to false, then checking if it is false. Then you set it to true, but that doesn’t matter because you set it to false again the next time you call the function. The solution is to remove the alreadyChecked = false line from the function, and instead put: local alreadyChecked = false somewhere BEFORE the function. I would strongly recommend that you look up some of the corona tutorials. This error is a very simple scope issue, and while even an experienced developer could have overlooked it, I sense that’s not the case here. Likewise, if you don’t understand how tables work in Lua you will quickly find yourself being stuck.

Yeah, I know I’m a newbie - and although I watched some of J. A. Whye’s videos, I know I need to learn a lot more…  But thanks for helping anyway.

I did change the code, but can I just say that it didn’t work the way I wanted it to. Yep, when I clicked patternDot1, it made alreadyChecked true, but userSelectCorrect false.