[Solved] If statement does not work in case of Hotspot Interact()

Started by Adasiek, Thu 21/07/2022 17:59:59

Previous topic - Next topic

Adasiek

Hello Guys, I have a beginner question.

I'd like the player to interact with a door only once.
But whenever I do it, my character just does the actions in one big row,
ignoring the if conditions. Instead of two separate actions, the player character does it this way:
1.she knocks, 2.she has a little dialogue with Jin, 3.she says "I already did that".
And the point 3 should have happened only if the player tried for the second time

I have a global value Knocked, initial value is 0.

The funny thing is, I did very same set of conditions for other thing, and it worked.
Please guys, what am I doing wrong?

function Door_Interact()
{
  if (Knocked == 0)
  {
Knocked = 1;
player.Walk(405, 310,  eBlock);
aKnock.Play();
Display("Knock knock");
player.Say("Guess she is not there");
cJin.Say("I bet she's still asleep. You know she never leaves this place sis");
  }
  if (Knocked == 1) 
player.Walk(405, 310,  eBlock);
player.Think("I already did try that");
}

I tried as well this way, but with same effect:

function Door_Interact()
{
  if (Knocked == 0)
  {
Knocked = 1;
player.Walk(405, 310,  eBlock);
aKnock.Play();
Display("Knock knock");
player.Say("Guess she is not there");
cJin.Say("I bet she's still asleep. You know she never leaves this place sis");
  }
  if (Knocked == 1) 
{
player.Walk(405, 310,  eBlock);
player.Think("I already did try that");
}
}

Cheers
Adam
Thank you
Best Regards
Adam

Crimson Wizard

#1
You need to use "else if" in the second case:

Code: ags

if (Knocked == 0)
{
   // code
}
else if (Knocked == 1)
{
   // code
}


In general, each time you write "if" - that's a start of a completely new conditional sequence. Each "if" is tested separately from others. So in your previous code it worked like that:
* Knocked == 0? it is, so set Knocked = 1 and do first action
* Knocked == 1? it is (!), so do second action

"else if" allows to create a list of options, only one of which is selected (the first one which is calculated as true).

Adasiek

Hi,

thank you very much for your help :)
The solution worked perfectly.
I keep learning the scripts.
Have a great evening

Cheers
Adam
Thank you
Best Regards
Adam

SMF spam blocked by CleanTalk