[Solved] Something i don't understand with If and Else

Started by actaria, Mon 25/07/2022 09:38:10

Previous topic - Next topic

actaria

Hello,

There's something i don't understand so i hope someone can help and explain to me.

I have a door locked with a key.
If you interact with the door without the key it says"the door is locked"
Everything is working fine, the key open the door.
I can open and close the door this is how i want it to work.
Unfortunately i have a problem even when the key has been used to unlock the door if you close it and reopen it, it still says "the door is locked" when you click to reopen.
and i don't want that to happen again once the door has been unlocked of course.

This is the code i have in my room script file:

Code: ags

bool door_unlocked = false;

function oDoorclose_Interact()
{
  cChar1.Walk(872, 712, eBlock);
  cChar1.FaceDirection(eDirectionUp);
  if (door_unlocked) door1.Play(eAudioPriorityNormal, eOnce);
  if (door_unlocked) oDooropen.Visible = true; 
 
   else  door1.Play(eAudioPriorityNormal, eOnce);
  
  	player.Say("The door is closed.");

}


I tough that the "else" would have done the job but it looks like there's something i don't understand well.
Thanks a lot for your precious help i can provide the rest of the code if needed but i don't think so.

a few pictures so you can see how it looks:





Thanks again  :)

Gilbert

That's because you didn't group the lines in the else part inside curly brackets properly.
Without the curly brackets, only the play audio line is executed in the else part, and the "The door is closed." line will be executed regardless of the value of door_unlocked variable.
So:
Code: ags

bool door_unlocked = false;

function oDoorclose_Interact()
{
  cChar1.Walk(872, 712, eBlock);
  cChar1.FaceDirection(eDirectionUp);
  if (door_unlocked) {
	door1.Play(eAudioPriorityNormal, eOnce);
	oDooropen.Visible = true; 
  }
  else {
	door1.Play(eAudioPriorityNormal, eOnce);
	player.Say("The door is closed.");
  }
}


actaria

#2
Yes it's working,
Thanks you so much for the explanation Gilbert this is very kind and now i understand my mistake.

Been working on this for 2 days  :)
Now i won't make this mistake again, thank you for your time and the nice explanation.

This forum is really amazing  :P

SMF spam blocked by CleanTalk