Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jojowl80 on Fri 29/05/2020 02:21:11

Title: Turning on and off objects continuosly
Post by: Jojowl80 on Fri 29/05/2020 02:21:11
as said I would like to turn on and off objects


Code (ags) Select

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

  } else if (Verbs.UsedAction(eGA_Push)) {
}
if (MyCounter == 0) {
   
    player.Say("Okay.");
     player.Walk(214, 159, eBlock);
    object[0].Visible = true;
    MyCounter +=1;


}
if (MyCounter == 1) {
   
    player.Say("Okay.");
     player.Walk(214, 159, eBlock);
    object[0].Visible = false;
    MyCounter -=1;
}



but instead of having separate instances, It does it all in the same loop. essentially i'm hoping using the minus counter will reset back to 0 enabling the character to keep turning off and on continuously
Title: Re: Turning on and off objects continuosly
Post by: Sedgemire on Fri 29/05/2020 09:27:57
Shouldn't you have

Code (ags) Select
MyCounter = 1;

instead of MyCounter +=1, and vice versa (= 0;) with the second one. The same could be done with a boolean variable, but I guess it all comes down to personal preference.
Title: Re: Turning on and off objects continuosly
Post by: Laura Hunt on Fri 29/05/2020 09:31:32
You don't need a counter. Simply do:

Code (ags) Select
player.Say("Okay.");
player.Walk(214, 159, eBlock);
object[0].Visible = !object[0].Visible;


If the object is visible (true), this will set it to false. If it's false, it'll turn it to true.
Title: Re: Turning on and off objects continuosly
Post by: Snarky on Fri 29/05/2020 10:00:04
Laura shows a better solution, but the reason your original code doesn't work is that between the two MyCounter conditions you need an else if to ensure only one of them runs. Otherwise the first block runs, changes the value of MyCounter to 1, then checks the second condition (which is now true), and runs that block as well.
Title: Re: Turning on and off objects continuosly
Post by: Jojowl80 on Fri 29/05/2020 13:46:55
Perfect. that's so much eaiser! thanks everyone
Title: Re: Turning on and off objects continuosly
Post by: Khris on Fri 29/05/2020 20:28:17
Btw, you should really look into the features of the Thumbleweed template, specifically the  Verbs.*  functions.

That interface is well-known for the ability to interrupt any action as long as the character hasn't reached the hotspot yet, and you're using blocking walks right now, which can be pretty jarring for the player.

Check out the PDF in the game folder to learn about the available commands, the arguably most important one being  Verbs.MovePlayer().
Title: Re: Turning on and off objects continuosly
Post by: Jojowl80 on Fri 29/05/2020 22:38:13
thanks Khris wil do!