Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jojowl80 on Wed 22/07/2020 23:26:51

Title: turning objects off and on while also turning bools off and on.
Post by: Jojowl80 on Wed 22/07/2020 23:26:51
[code/ags]

player.Say("okay.");
   object[2].Visible = !object[2].Visible;
   stove = true;



So as of now the stove turns off and on but obviously, once this happens once the bool stays on forevor. anyone to set it to false in conjecture with !object
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Matti on Wed 22/07/2020 23:43:01
I think you just need to change it to stove = !stove;

A longer, more comprehensible code would be this:

Code (ags) Select

if (stove == false)
{
  stove = true;
  object[2].Visible = true;
}
else
{
  stove = false;
  object[2].Visible = false;
}


I'm just assuming that the object should turn visible when the variable is true. You didn't provide much info and didn't even post the function your code snippet is in.
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Khris on Thu 23/07/2020 00:13:51
object[2].Visible  is a bool, too; you can use the same syntax to toggle both.

Or just use
Code (ags) Select

  stove = object[2].Visible;

after toggling the object's visibility.

Also, unless  stove  is a global bool you need to switch in other rooms, you don't really need it at all; you can just use  object[2].Visible  itself to find out if the stove is turned on or not.
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Jojowl80 on Thu 23/07/2020 00:28:15
I should have mentioned I have for different burners on the stove. each with similiar coding. Object 0,1,2,3



Code (ags) Select


function hDial1_AnyClick()
{
if (Verbs.UsedAction(eGA_WalkTo)) {

  } else if (Verbs.UsedAction(eGA_Use)) {
   player.Say("okay.");
   object[0].Visible = !object[0].Visible;
   stove = object[0].Visible;
}
}



I have it set when the charater leaves the room, he warns the player that the house will burn down if he leaves the burners on. So now when I try to leave it shows that dialogue over and over again.
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Khris on Thu 23/07/2020 00:54:01
Why does it show the message over and over again? Where did you put it?

Anyway, you can do this:
Code (ags) Select
  bool stove = false;
  for (int i = 0; i < 4; i++) if (object[i].Visible) stove = true;
  if (stove) Display("Better turn off that stove before leaving.");
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Jojowl80 on Thu 23/07/2020 01:01:31
Code (ags) Select


function room_LeaveLeft()
{
  if (stove == true){
 
  player.Say("Lets not burn the house down, in the first 5 minutes of the game.");
 
  }
if (stove == false) {
  player.ChangeRoom(12, 250, 350);
 
}
}


I made the bool already, the rest of it I honestly do not understand.
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Khris on Thu 23/07/2020 07:42:22
I'm guessing that function is called each frame as long as the player is outside the left edge, so you would have to add a blocking walk back inside the edge at line 7 of that snippet.

My code iterates over the first four objects and checks if at least one of them is true. Use it like this (and remove the global bool):

Code (ags) Select
function room_LeaveLeft()
{
  bool stove = false;
  for (int i = 0; i < 4; i++) {  // long version with blocks
    if (object[i].Visible) {
      stove = true;
    }
  }

  if (stove) {
    player.Say("Lets not burn the house down, in the first 5 minutes of the game.");
    player.Walk(50, player.y, eBlock);
  }
  else player.ChangeRoom(12, 250, 350);
}
Title: Re: turning objects off and on while also turning bools off and on.
Post by: Jojowl80 on Thu 23/07/2020 23:46:25
That Code boggles my mind lol. It worked tho, once again you are amazing. many thanks!