Some advice with timers please. (SOLVED).

Started by RetroJay, Thu 28/04/2011 23:20:47

Previous topic - Next topic

RetroJay

Hi peeps.

I wonder if anyone could help me with this piece of code I have.
I haven't worked with timers before as I haven't needed them but now I do.
If at all possible I am looking for advice for the best way of using them.

Now I have a couple of stars that twinkle (star objects) I want them to twinkle a pause and then twinkle and so on.
In 'Player enters room before fadein' I put this.

Code: ags


  SetTimer(1,300);  
  SetTimer(2,212);

 
In my room script I have made the 'function repeatedly_execute_always()' and put this.

Code: ags

function repeatedly_execute_always() {
  // Script for twinkling stars. //

if (IsTimerExpired(1) == 1) {
   object[3].Animate(0,5,eOnce,eNoBlock);
   SetTimer(1,300);  
   return;
  }
 
  if (IsTimerExpired(2) == 1) {
  object[4].Animate(0,5,eOnce,eNoBlock);
  SetTimer(2,212);
  return;
 }
}


This works as I want it to but it just seems clunky somehow. Is there a better way of doing this?

Many thanks.
Jay.


Khris

If you use timers then that's the way to do it; you don't need the returns though.
(Also, it's better to use if (IsTimerExpired(1))
Comparing it to 1 works for AGS but it's not necessary and in other languages, true might equal -1.)

An alternative way of doing this, especially if you're going to have more stars:

Code: ags
int timer;

function repeatedly_execute_always() {

  timer++;
  if (timer == 300*212) timer = 0;  // reset to avoid overflow

  if (timer % 300 == 0) object[3].Animate(0, 5, eOnce, eNoBlock);
  if (timer % 212 == 0) object[4].Animate(0, 5, eOnce, eNoBlock);
}


(% is the mod operator and is used to calculate the remainder of a division.)

RetroJay

Hi Khris.

Long time no speak. I hope you are well.

That looks better.
As you said I do indeed want to have more stars than just 2.

The 'int timer' part you suggested. does that go at the top of my room script?
And if so can I do away with the first script I gave where I set 2 timers?

Thank you.
Jay.

Khris

Hi Jay,

you can put "int timer;" anywhere into the room script as long as the line is outside of any function and above rep_ex_always. I always put variable definitions above the first function that uses them, so directly above rep_ex_always is best here for tidiness.

Yes, you can remove all SetTimer lines, you no longer need them.

RetroJay

Khris. (My Mentor) ;)

Thank you.

That's fantastic. Works much better and looks neater.
I am still fuzzy on these "int's" but learning.
I assume that now I can add more stars to the scene by adding a new 'if (timer ==)' thing.

P.S. What happened to 'Khris MUC' and why has your avatar grown yellow hair? :D

Jay.



Khris

You're welcome :)

Just remember, if you add a new star, add the delay to the reset line:

Code: ags
  if (timer == 300*212*450) timer = 0;  // reset to avoid overflow          <------  add delay factor

  if (timer % 300 == 0) object[3].Animate(0, 5, eOnce, eNoBlock);
  if (timer % 212 == 0) object[4].Animate(0, 5, eOnce, eNoBlock);
  if (timer % 450 == 0) object[5].Animate(0, 5, eOnce, eNoBlock);             <----- add star line


Basically, the first line should reset timer to 0 as soon as it has reached the least common multiple of all delays.
So in this example, a better way would be: if (timer == 47700) timer = 0;
(ints can hold numbers up to 2^31 I believe, so there's some room :))

Quote from: RetroJay on Fri 29/04/2011 02:08:40
P.S. What happened to 'Khris MUC' and why has your avatar grown yellow hair? :D

I ditched the MUC some years ago to avoid people constantly misspelling my nick. The blonde hair is that of Cloud Strife of Final Fantasy VII fame, we all changed our nicks in celebration of Icey Day and I felt my avatar could use some iceyfication, too :D

RetroJay

Damn.

Looks like I missed that one.
I must do some iceyfication now. Gives me a good reason to update my pic. ;D

Thanks Khris for your help.

Jay.

SMF spam blocked by CleanTalk