Need help with a large if statement

#1

hello I am working with Sugarcube 2 in twine and I’ve got a large if statement that needs sorting as i have tried several different ways to do it.
I have tried to do each statement separate and it does work to an extent but it does not work out the way i want it to.
Therefore it needs to be one full statement.
Currently i am having issues where it can’t find parent macro.

p.s Intellect value will keep changing as the game progresses.

here is part of the code:
<<if $Intellect lt 19>><<set $IntStatus to “Something”>>
<<elseif not $Intellect lt 19>>
<<if $Intellect lt 29 gte 20>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 29 gte 20>>
<<if $Intellect lt 39 gte 30>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 39 gte 30>>
<<if $Intellect lt 49 gte 40>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 49 gte 40>>
<<if $Intellect lt 59 gte 50>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 59 gte 50>>
<<if $Intellect lt 69 gte 60>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 69 gte 60>>
<<if $Intellect lt 79 gte 70>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 79 gte 70>>
<<if $Intellect lt 79 gte 80>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 89 gte 80>>
<<if $Intellect lt 99 gte 90>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 99 gte 90>>
<<if $Intellect lt 109 gte 100>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 109 gte 100>>
<<if $Intellect lt 119 gte 110>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 119 gte 110>>
<<if $Intellect lt 129 gte 120>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 129 gte 120>>
<<if $Intellect lt 139 gte 130>><<set $IntStatus to “SomethingDifferent”>>
<<elseif not $Intellect lt 139 gte 130>>
<<if $Intellect gte 140>><<set $IntStatus to “SomethingDifferent”>>
<>
$IntStatus

Thanks in advance
Creativemrgamer

(Hanon Ondricek) #2

Using “else” should be exclusionary in most boolean logic. If I say “if the fruit is the orange, A; else, B” means that B can’t happen if the fruit is the orange.

I’m no Twine-Sugarcube expert, but it seems though you should be able to say theoretically:

if A = 1: then foo; elseif A = 2: then bar; endif
You shouldn’t need to specify

if A = 1 and A <> 2: then foo; elseif A = 2 and A <> 1: then bar; endif.
Because if A = 1, it can’t also hold that A = 2, which seems to be an overcomplication in your code which could be simplified thusly:

<<if $Intellect lte 19>> <<set $IntStatus to "0-19">> <<elseif $Intellect gte 20 and $intellect lte 29>> <<set $IntStatus to "20-29">> <<elseif $Intellect gte 30 and $intellect lte 39>> <<set $IntStatus to "30-39">> <<elseif $intellect gte 40 and $intellect lte 49>> <<set $IntStatus to "40-49">> [...]

:open_mouth:

#3

Couldn’t you simplify that to:

<<if $Intellect lte 19>>
  <<set $IntStatus to "0-19">>
<<elseif $intellect lte 29>>
  <<set $IntStatus to "20-29">>
<<elseif $intellect lte 39>>
  <<set $IntStatus to "30-39">>
<<elseif $intellect lte 49>>
  <<set $IntStatus to "40-49">>
[...]
(Hanon Ondricek) #4

I believe so. When you do a huge stack of if statements finding something in a range you have to be careful not to overlap your ranges. I was trying to make the range values clear as possible, but yes; so long as you include elseif statements after the first one it should work as the else implies exclusivity and negates all the others once a truth is registered.

I had inferred the OP had run into this kind of problem before with a bunch of separate if-endif lines and “lt X” operands that overlapped and that’s why they were being so specific about meticulously coding opposing if and not-if statements.

#5

Thanks for all the support and that seemed to fix the problem, I was originally shown to do this as so each section was in its own separate if statement.

#6

I know it’s a bit late, but a much shorter way of doing that would be this:

<<set _result = Math.trunc($IntStatus / 10).clamp(1, 14) - 1>> <<set $IntStatus = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"][_result]>> $IntStatus
The first line sets the temporary variable “_result” to a value of 0 to 13, and the second line puts the value that of that item from the array into $IntStatus.

That way we go from 30 lines of code down to 3. :smiley: