Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

What's the best way to design long if/elseif trees?

I doubt this will matter practically for a Twine 2 game, but I'm playing around with Twine 2 / Sugarcube partially to learn basic programming. So, basic coding question.

Say we have this example.
<<if ($hero[$i].assignment is “train sword skill”) and ($hero[$i]. swordSkill is 1)>>
	They practice clumsily with a sword.
<<elseif ($hero[$i].assignment is “train sword skill”) and ($hero[$i]. swordSkill is 2)>>
	They exercise with a sword.
<<elseif ($hero[$i].assignment is “train sword skill”) and ($hero[$i]. swordSkill is 3)>>
	They work masterfully with a sword.
<</if>>

Would the game run faster if things were rearranged like this?
<<if ($hero[$i].assignment is “train sword skill”)>>
<<elseif ($hero[$i].swordSkill is 1)>>
	They practice clumsily with a sword.
<<elseif ($hero[$i].swordSkill is 2)>>
	They exercise with a sword.
<<elseif ($hero[$i].swordSkill is 3)>>
	They work masterfully with a sword.
<</if>>

I'm wondering whether the empty <<if>> at the start of the tree would speed things up by preventing the rest of the tree from running by stopping the game running through each <<elseif>> for heroes whose assignment is not “train sword skill.” Put another way, does the code go all the way through all if trees, or does it move on once a clause is satisfied?

Or should I be designing long if trees another way entirely?

Comments

  • I seem to be unable to edit the OP. Anyway, noticed I made a mistake in my example: is where I should have used isnot. The alternative rearrangement should be:
    <<if ($hero[$i].assignment isnot “train sword skill”)>>
    <<elseif ($hero[$i].swordSkill is 1)>>
    	They practice clumsily with a sword.
    <<elseif ($hero[$i].swordSkill is 2)>>
    	They exercise with a sword.
    <<elseif ($hero[$i].swordSkill is 3)>>
    	They work masterfully with a sword.
    <</if>>
    
  • edited August 2015
    Using an empty <<if>> (as you have in the first line of your latest example) will work but is considered a bad coding practise. A better way to write your example is to use one <<if>> within another:
    <<if ($hero[$i].assignment is “train sword skill”)>>
    	<<if ($hero[$i].swordSkill is 1)>>
    		They practice clumsily with a sword.
    	<<elseif ($hero[$i].swordSkill is 2)>>
    		They exercise with a sword.
    	<<elseif ($hero[$i].swordSkill is 3)>>
    		They work masterfully with a sword.
    	<</if>>
    <</if>>
    
    note: I have indented the code to make it easier to read, the indents can be removed.
  • Testing $hero[$i].assignment only once, rather than possibly three times would be better, yes.

    As to how that should be done, greyelf is correct.

    Beyond that, the parenthesis are redundant here. Additionally, you seem to be using typographic (a.k.a. curly/smart) quotes (copy/paste from a word processor?) which will not work here. Only double/single quotes are allowable as "programming" quotes. You may still use typographic quotes within your normal text, however.

    So, merging greyelf's suggestion with the issues I noted, it should probably look like the following:
    <<if $hero[$i].assignment is "train sword skill">>
    	<<if $hero[$i].swordSkill is 1>>
    		They practice clumsily with a sword.
    	<<elseif $hero[$i].swordSkill is 2>>
    		They exercise with a sword.
    	<<elseif $hero[$i].swordSkill is 3>>
    		They work masterfully with a sword.
    	<</if>>
    <</if>>
    
    Whatever you do, mind your whitespace (incl. line breaks).

    Speaking of which, here's a somewhat more succinct version, which ensures that it's not creating extra whitespace internally, particularly line breaks:
    <<if $hero[$i].assignment is "train sword skill">>\
    They \
    <<if     $hero[$i].swordSkill is 1>>practice clumsily \
    <<elseif $hero[$i].swordSkill is 2>>exercise \
    <<elseif $hero[$i].swordSkill is 3>>work masterfully \
    <</if>>\
    with a sword.\
    <</if>>
    
Sign In or Register to comment.