Checking two variables for an IF

,

OS: Windows 11
Platform: VS Code

Is it possible to check two different variables against a single or multiple values in a single line?

I'm trying to use this:

if (varA or varB) == "targetValueA" or "targetValueB" then
-- rest of function
end

This doesn't work as I thought it would, obviously, but maybe you can see what I was attempting to do and suggest how you would actually perform this kind of check?

Slightly aside, but relating to this function and IF's. This function overall has multiple IF's and it's following the last IF when it shouldn't be.

Function Code

I stripped out a bunch of the extra actions so that we can focus on the IF's

local function resetReading()
    if spreadSprite then
        spreadSprite:remove()
    end

    spreadName = "Not Selected"

    if currentMenuOptions and currentMenuOptions[1] and currentMenuOptions[1].value ~= nil then

        actions here

    end

    if (gameMode or destinationMode) == "autoReading" then
        print("DEBUG: resetReading Path A, gameMode = ".. gameMode.. ", destinationMode = ".. destinationMode)
        
        actions here
        
    end

    if (gameMode or destinationMode) == "manualReading" or "titleScreen" then
        print("DEBUG: resetReading Path B, gameMode = ".. gameMode.. ", destinationMode = ".. destinationMode)

        actions here
        
    end
end

Adding those print statements helped me confirm what it was doing when it shouldn't have been.

When I enter this function with a gameMode of saveOption destinationMode of autoReading, it's taking the last IF path.

I would understand this if the check I tried to make was only checking the first variable and not both, but why would it take the last IF? It's acting like it's an IF/ELSE if that makes sense.

I love how I get stumped by a thing for hours, then things suddenly click AFTER I make a post on the forum.

if (gameMode == "autoReading") or (destinationMode == "autoReading") then

if (gameMode == ("manualReading" or "titleScreen")) or (destinationMode == ("manualReading" or "titleScreen")) then

So I had to break them up with the OR and wrap the checks to be more individual.

So, follow-up, is there a way to make this check a bit more compact or "more clever"?

3 Likes

sadly no, also this is more of a lua question and not very playdate specific.

This is how I would do it:
if varA == "targetValueA" or varA == "targetValueB" or varB == "targetValueA" or varB == "targetValueB" then
-- rest of function
end

2 Likes