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
<<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.
Using the is and to keywords:
Using equals signs for assignment = and comparision ==
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.
Good to know!
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.
The above will produce: