(Solved) Using multiple IF in a function

Started by actaria, Fri 19/08/2022 10:37:20

Previous topic - Next topic

actaria

Hello,

I am struggling again with the IF, i think i am doing better since i have no syntax errors but something
isn't working well.

I have a fridge (2 objects) open and close door.
Inside the fridge there is cheese and chicken
i can pick both they are going in inventory and dispapear from the fridge but only when i pick the cheese first.
When i pick the chicken first close the fridge door and reopen it the cheese and the chicken are still inside !

I can't see what i am doing wrong this is the first time i use multiple IF and it looks like i am doing something wrong

My code when you click on the closed fridge:

Code: ags

function fridge_close_Interact()
{
fridge_close.Visible = false;
fridge_open.Visible = true;
cheese.Visible = true;
chicken.Visible = true;

if (player.HasInventory(icheese))
  {
  
   if (player.HasInventory(ichicken))
{

cheese.Visible = false;
chicken.Visible = false;
}
  }

else {
  cheese.Visible = true;
chicken.Visible = true;
}
 }



Thank you very much for your precious help each time i am stuck

morganw

It is a little difficult to give the best solution (I don't know why you wouldn't just make the objects invisible once, at the time of picking them up) but the problem of which item is picked up first is because the check for the chicken only happens when you have already picked up the cheese. You need to indent the code so that when you read it back it will be clear which parts of the script will run.

Code: ags
function fridge_close_Interact()
{
  fridge_close.Visible = false;
  fridge_open.Visible = true;
  cheese.Visible = true;
  chicken.Visible = true;
 
  if (player.HasInventory(icheese))
  {
    if (player.HasInventory(ichicken))
    {
      cheese.Visible = false;
      chicken.Visible = false;
    }
  } 
  else
  {
    cheese.Visible = true;
    chicken.Visible = true;
  }
}


Do you see the problem now?

actaria

Hello and thank you for the quick reply,

Plus my explanation was really bad it works well when i pick both items but no when i pick only one of them.

I will look at your code now and try to understand, you are right  my indentation is really bad.

Thanks again i will tell you if i managed to do it soon  :) and if i understand

actaria

I need to use Else if so i don't need both condiions to make them Visible = false ?

Am i wrong ?  :)

morganw

#4
I think what you are trying to do is just this:
Code: ags
function fridge_close_Interact()
{
  fridge_close.Visible = false;
  fridge_open.Visible = true;
  cheese.Visible = !player.HasInventory(icheese);
  chicken.Visible = !player.HasInventory(ichicken);
}

actaria

#5
I tried your code but unfortunately the fridge is empty when i open it  :)



My objects item are set on visible false that way they are hide until i open the fridge door

This is what i got with my code it's working when i pick both item or chicken first but not when i pick the cheese 1st as you can see





Thank you.

morganw

I did edit the code shortly after posting it, does the version you are testing have "!" characters in it?

actaria

I don't think so i will try again thanks a lot

actaria

Its perfectly working  now.

I didn't know that you can do it that way it's so much better than what i was doing.

Thanks a lot Morganw you saved me and now i will use this method

Have a great day

Khris

#9
The short answer to the original problem is to handle the objects separately. Don't nest the checks for the two items. Just write one, then the other.

The current solution is error prone; if the cheese or chicken is ever used (and therefore removed from the inventory) elsewhere, they'll reappear in the fridge.

To easily solve both issues, change the game in the following way:
- make the open fridge part of the background, but without the door
- place objects inside the fridge
- place closed door object on top (it will have a lower baseline by default so it'll automatically cover the other objects
- when the fridge is opened / closed, simply change the graphic of the door object. Due to the different positions this requires adding blank space to the closed door sprite

The advantage of this approach is that handling the objects inside the fridge works just like regular objects: when they're taken, they get turned invisible and that's it. No variables necessary, no ifs, nothing.

Only if objects are supposed to be taken from inside the door will it get complicated.

Edit: I just found this thread: https://www.adventuregamestudio.co.uk/forums/index.php?topic=60196.0  :-D

actaria

#10
Quote from: Khris on Sun 21/08/2022 14:13:41
The short answer to the original problem is to handle the objects separately. Don't nest the checks for the two items. Just write one, then the other.

To easily solve both issues, change the game in the following way:
- make the open fridge part of the background, but without the door
- place objects inside the fridge
- place closed door object on top (it will have a lower baseline by default so it'll automatically cover the other objects
- when the fridge is opened / closed, simply change the graphic of the door object. Due to the different positions this requires adding blank space to the closed door

Hello,

Thank you Khris this is clearly a better way, without using variables i really like  it and will try very soon with another room.

Thanks again and  i will show my next room using your method

EDIT:
Ok i understand now but anyways you gave me new ideas to try  :)

SMF spam blocked by CleanTalk