Page 1 of 1

[Solved] Problem with logical operator "or"

Posted: Thu Oct 01, 2020 1:58 pm
by ickputzdirwech
I got the following setting:

Code: Select all

  
setting_type = "startup",
name = "spidertron-remote",
type = "string-setting",
allowed_values = {"disabled", "enabled", "enabled-hidden"},
default_value = "enabled"
I want to generate a prototype if the setting is either "enabled" or "enabled-hidden". I therefore do the following check:

Code: Select all

if settings.startup["spidertron-remote"].value == "enabled" or settings.startup["spidertron-remote"].value == "enabled-hidden" then
	...
end
From my understanding it should be possible to make this shorter with one of the following options:

Code: Select all

if settings.startup["spidertron-remote"].value == "enabled" or "enabled-hidden" then
if settings.startup["spidertron-remote"].value == ("enabled" or "enabled-hidden") then
The first one only fails when you select "disabled". It generates the prototype regardless.
The second one only fails when you select "enabled-hidden". It doesn't generate the prototype.
What am I doing wrong? Is this just not possible? This is not that big of a deal for the case described here, but if you have settings with 4 or more options checking all of them individually gets very messy.

Re: Problem with logical operator "or"

Posted: Thu Oct 01, 2020 5:22 pm
by eradicator
ickputzdirwech wrote: Thu Oct 01, 2020 1:58 pm

Code: Select all

  ("enabled" or "enabled-hidden") 
Your understanding is fundamentally wrong. You may want to consider reading a proper tutorial. Logic or between string literals will always return the first string. Logic or chains with any string literal in them are tautologically true. Use a lookup table if you want to check more than one condition.

Code: Select all

local ok = {enabled=true,['enabled-hidden']=true}
if ok[blabla_setting] then

Re: Problem with logical operator "or"

Posted: Thu Oct 01, 2020 6:33 pm
by ickputzdirwech
ok I got it now. Complete fool today I guess :oops: Thank you very much! I don't know what I was thinking there. :cry: