intfiction.org
https://www.intfiction.org/forum/

Need help with a large if statement
https://www.intfiction.org/forum/viewtopic.php?f=49&t=26227
Page 1 of 1

Author:  creativemrgamer [ Fri May 11, 2018 8:05 am ]
Post subject:  Need help with a large if statement

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">>
<</if>>
$IntStatus

Thanks in advance
Creativemrgamer

Author:  HanonO [ Sat May 12, 2018 2:20 am ]
Post subject:  Re: Need help with a large if statement

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:

Code:
if A = 1: then foo;
elseif A = 2: then bar;
endif

You shouldn't need to specify

Code:
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:

Code:
<<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">>
[...]


:shock:

Author:  ralphmerridew [ Sun May 13, 2018 7:22 pm ]
Post subject:  Re: Need help with a large if statement

Couldn't you simplify that to:

Code:
<<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">>
[...]

Author:  HanonO [ Mon May 14, 2018 8:36 am ]
Post subject:  Re: Need help with a large if statement

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.

Author:  creativemrgamer [ Sat May 19, 2018 2:19 pm ]
Post subject:  Re: Need help with a large if statement

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.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/