[Solved] Problem with logical operator "or"

Place to get help with not working mods / modding interface.
User avatar
ickputzdirwech
Filter Inserter
Filter Inserter
Posts: 798
Joined: Sun May 07, 2017 10:16 am
Contact:

[Solved] Problem with logical operator "or"

Post 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.
Last edited by ickputzdirwech on Thu Oct 01, 2020 6:33 pm, edited 1 time in total.
Mods: Shortcuts for 1.1, ick's Sea Block, ick's vanilla tweaks
Tools: Atom language pack
Text quickly seems cold and unfriendly. Be careful how you write and interpret what others have written.
- A reminder for me and all who read what I write
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Problem with logical operator "or"

Post 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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
ickputzdirwech
Filter Inserter
Filter Inserter
Posts: 798
Joined: Sun May 07, 2017 10:16 am
Contact:

Re: Problem with logical operator "or"

Post 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:
Mods: Shortcuts for 1.1, ick's Sea Block, ick's vanilla tweaks
Tools: Atom language pack
Text quickly seems cold and unfriendly. Be careful how you write and interpret what others have written.
- A reminder for me and all who read what I write
Post Reply

Return to “Modding help”