Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Gold Dragon on Thu 22/05/2008 04:07:30

Title: Why isn't this Timer function not working??
Post by: Gold Dragon on Thu 22/05/2008 04:07:30
Ok I'm trying to set up a kind of Light flicker effect by randomly selecting backgrounds that have different tints of light in them.

the code is this


function room_RepExec(){
  if (IsTimerExpired(1) == 1) {
    SetBackgroundFrame(Random(4));
    SetTimer(1,(Random(33)+40);
  }
}


It constintly changes.. there is no pauses between background changes. And when I put 'SetTimer(1,(Random(33)+40);' above the function room_RepExec() I get an error saying unexpected SetTimer?? huh?? so I left it thinking that if (IsTimerExpired(1) == 1)  would be true being that no setTimer has been set then it would be natually true.

Anyways can anyone help me out here?
Title: Re: Why isn't this Timer function not working??
Post by: monkey0506 on Thu 22/05/2008 04:19:29
Well the default game speed is 40 loops (or frames) per second. So best case scenario here you're only waiting 1.825 seconds before changing, all other cases would be less.

How much of a pause do you want between the changes? You could try something like this:

SetTimer(1, (Random(4) + 1) * GetGameSpeed());

This code would wait between 1 to 5 seconds before changing the background frame. Random(X) returns a value from 0 to X inclusive, so Random(4) + 1 would return a value between 1 and 5, multiplied by the current game speed yields 1 to 5 seconds between changes.

The reason you got an error when you tried posting the code above the repEx function is that you're trying to set the timer outside of a function. AGS wouldn't know when to call the code. Just leave the SetTimer command where it's at, using something like the above and you should be good to go.
Title: Re: Why isn't this Timer function not working??
Post by: Gilbert on Thu 22/05/2008 05:27:29
My guess that the background constantly changes is because the codes may not even be executed and you didn't stop the animations at the first place (as you didn't mentioned you have initialised these things this is my assumption).

By default, the background frames animate automatically when a room is first loaded, so you need to stop it first when the player enters the room. On the other hand, the "if (IsTimerExpired(1) == 1) {" phrase won't be executed unless the timer really expires, so you need to initialise the timer to let it expire once first, otherwise it won't be set again). Therefore, try adding the following to the "enters room after fade-in" event of the room (if you haven't done this yet):
SetBackgroundFrame (0);
SetTimer(1,Random(33)+40);




Also, was the original
SetTimer(1,(Random(33)+40);
line a typo? It has one ( too many, it shouldn't even compile!
Title: Re: Why isn't this Timer function not working??
Post by: Gold Dragon on Thu 22/05/2008 22:07:59
Thanks Gilbot that did it.. I think what was happening was that the engine was still trying to animate the backgrounds and I had set the backgroundanimation delay to 0 thinking it would cancel it. But your sugestion made it work.. thank you  ;D ;D