I can't seem to get If and elseif to work Sugarcube

Hi, I seem to have a problem while using the if and elseif macro in my game. I’m using Sugarcube 2.30.0 (the latest I guess ?).

In one passage, I ask the player to clink on iether one of two links when choosing their gender. If you click on girl, the next passage shows:

<<set $player={
gender: “girl”,

Same thing with boy. So far it works. Both paths lead back to the same passage.
Then I ask for their name with this:

Your last name is <<textbox “$player.lastname” “”>>
And a link to confirm and move on to the next passage. It still works perfectly this far.

Then I would like the player to receive a letter and to take into account their gender and name. That’s where if doesn’t work. It’s been driving me mad for the past txo hours. Here’s what I have:

<<if $player.gender is “girl”>>
Dear Ms. <<print $player.lastname>>,
<<elseif $player.gender is “boy”>>
Dear Mr. <<print $player.lastname>>,
<< endif >>

Problem is, nothing is shown, it’s blank.
I’ve looked at other topics on forums, read the sugarcube documentation and tinkered but I can’t seem to figure it out. Could somebody help?

I seemed to got it to work.
Try this

<<if $player_gender is “girl”>>
Dear Ms. <<print $player_lastname>>
<<elseif $player_gender is “boy”>>
Dear Mr. <<print $player_lastname>>
<< endif >>

Thanks for the help but it still doesn’t work with dashes…

I managed to make the Dear Mr. and then the last name appear somehow with $player.lastname and by changing elseif to just else, but now it’s stuck on Mr. and doesn’t show Ms. even if you chose girl before…

I feel like the if isn’t working, that it automatically jumps to else…

FYI, you can tighten up your code a bit like this:

Dear <<if $player.gender is "girl">>Ms.\
<<elseif $player.gender is "boy">>Mr.\
<</if>> <<print $player.lastname>>,

Note that you should end <<if>> macros with <</if>>, instead of <<endif>>. The <<endif>> method is the old way of doing things, and is now depreciated.

If that code doesn’t work, then it’s likely that either A) $player.gender is set to something other than "girl" or "boy", or B) some other code is deleting the .gender property off of $player.

You might want to temporarily put a line there that does <<run alert($player.gender)>>, that way it will pop up a message to show you what that variable is actually set to at that point.

That said, based on your use of this code:

<<set $player = { gender: "girl" }>>

I’m guessing that you’re accidentally deleting off the gender property with some other code you didn’t show us.

Initializing an object variable like that is fine, however, if you do that to an existing object, then that will remove all of the other properties, leaving only the properties inside the {...}. If you want to add a new property to an existing object, or change the value of an existing property, you should do this instead:

<<set $player.gender = "girl">>

If that doesn’t fix the problem, then you’ll likely need to send us a link to your code so we can see what else is causing the problem.

Hope that helps! :slight_smile:

P.S. In this forum you should make sure you wrap any code within a code block (using the “preformatted text” </> button), otherwise the forum may not display your code properly.

Omg it works! Thank you so much!

It was the $player variable. After adding the property gender I added another one that erased gender like you guessed.

And thank you for the shorter and cleaner code as well.

I add been looking for the button to put my code lines in code and I finally found it. So next time my posts should look like true code.

Thank you so much everyone for all your help.