Howdy, Stranger!

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

Problems with elseif... possible bug?

I've been having a bit of problems with getting elseif to work. I use Sugarcube. Here's the code I use, and I will explain what's happening in detail below.

<<set $rnum = random(1, 5)>>
<<set $rkind = either("Minion1", "Goblin2", "Skeleton3")>>
You will be facing <<print $rnum>> <<print $rkind>>
elseif

<<nobr>>
<<if $rkind = "Minion1">>
Oooooh <<set $pname = "Freaky Minion">>
<<elseif $rkind = "Goblin2">>
Ahhhhhh <<set $pname = "Scary Goblin">>
<<else>>
<<set $rkind = "Skeleton3">>
Doot doot <<set $pname = "Spooky Skeleton">>
<<endif>>
<</nobr>>
<<print $pname>>

The problem here is that $rkind will be assigned a value, say Minion1, or Goblin2, or Skeleton3, and it will display as such with no problem. However, when it links to what it sets $pname to, it always sets to the first result, Minion1.

I was having such difficulty with this that I went to one of my earlier games and found where I properly used the elseif code, made sure it worked, and then copied and pasted it over into my new game. After tinkering with some of the values, I realized it had bonkered itself back to only responding to the first if statement and none of the others, so I erased it all and remade the passage with the same copy and paste... Only to find it also didn't work now.

It's probably far more likely I'm just doing something horribly wrong than it being an actual bug in the program, but I used it without a problem in my earlier game only to find it no longer works when ported over without any broken links or missing variables.

So could anyone show me the proper way to code an elseif statement in Sugarcube? I'll test it out, and if it still glitches out, we'll be able to go from there.

Comments

  • edited June 2015
    Yes you're doing a common problem:

    <<if $rkind = Minion1>>

    is an example.

    In code language, a single = is setting a variable, while double equals (==) is comparing the variable. So your code is never actually comparing any variables, just constantly redefining them.


    Please use == in if and elseif statements or otherwise use "is" and "to" operators.
  • Based on @Claretta's advice:

    Using the is and to keywords:
    <<set $rnum to random(1, 5)>>
    <<set $rkind to either("Minion1", "Goblin2", "Skeleton3")>>
    You will be facing <<print $rnum>> <<print $rkind>>
    [[Again|elseif]]
    
    <<nobr>>
    <<if $rkind is "Minion1">>
    Oooooh <<set $pname to "Freaky Minion">>
    <<elseif $rkind is "Goblin2">>
    Ahhhhhh <<set $pname to "Scary Goblin">>
    <<else>>
    <<set $rkind is "Skeleton3">>
    Doot doot <<set $pname to "Spooky Skeleton">>
    <<endif>>
    <</nobr>>
    <<print $pname>>
    

    Using equals signs for assignment = and comparision ==
    <<set $rnum = random(1, 5)>>
    <<set $rkind = either("Minion1", "Goblin2", "Skeleton3")>>
    You will be facing <<print $rnum>> <<print $rkind>>
    [[Again|elseif]]
    
    <<nobr>>
    <<if $rkind == "Minion1">>
    Oooooh <<set $pname = "Freaky Minion">>
    <<elseif $rkind == "Goblin2">>
    Ahhhhhh <<set $pname = "Scary Goblin">>
    <<else>>
    <<set $rkind == "Skeleton3">>
    Doot doot <<set $pname = "Spooky Skeleton">>
    <<endif>>
    <</nobr>>
    <<print $pname>>
    
  • Claretta wrote: »
    Yes you're doing a common problem:

    <<if $rkind = Minion1>>

    is an example.

    In code language, a single = is setting a variable, while double equals (==) is comparing the variable. So your code is never actually comparing any variables, just constantly redefining them.


    Please use == in if and elseif statements or otherwise use "is" and "to" operators.

    So what my code is basically doing as-is, is saying "if --- set this variable to this sum" rather than "if this variable equals this sum...."

    Is there a reason it only seems to work with elseif? This is my first time hearing about this and I've had absolutely zero problems so far with "if $value = 1..." commands. They seem to be perfectly functional, but I suppose it's considered dirty or unstable if it doesn't have full functionality.
  • Ah, hold on. I'm so used to using numbers in my variables, I always use "if $value eq 1" or "if $value gte 1".... I don't think I've ever quite used an equals sign before with IFs now that I think about it.

    Good to know!
  • edited June 2015
    So what my code is basically doing as-is, is saying "if --- set this variable to this sum" rather than "if this variable equals this sum...."

    To revise my earlier comment to be more accurate, your code isn't doing anything at all, since a single equals sign is not a valid operator in an if statement. It just doesn't present an error message in this case.

    When you get to the final <<else>> you may still see something even with broken code before it since <<else>> assumes everything is false if nothing before it is true.
  • Claretta wrote: »
    To revise my earlier comment to be more accurate, your code isn't doing anything at all, since a single equals sign is not a valid operator in an if statement. It just doesn't present an error message in this case.
    Assignment does work within an IF macro as demonstrated by the following code:
    <<set $var to "A">>
    Var: $var
    
    <<if $var = "B">>B was assigned to Var via the IF
    <<elseif $var = "C">>The elseif never happens because the IF is always true<</if>>
    Var: $var
    

    The above will produce:
    Var: A

    B was assigned to Var via the IF

    Var: B
  • edited June 2015
    Oh so I got it the first time. :-P
Sign In or Register to comment.