Recent

Author Topic: Timer with multiple different OnTimer functions - weird behavior  (Read 2433 times)

rwebb616

  • Full Member
  • ***
  • Posts: 133
I'm trying to use a single timer for multiple purposes by redefining the procedure attached to the OnTimer event.

So one procedure does a 3 second countdown and then displays "GO!" as such:

Code: Pascal  [Select][+][-]
  1. procedure TfScoreboard.CountDownClockTimer(Sender: Tobject);
  2. begin
  3.   if ClockInterval >= 1 then
  4.     begin
  5.       countdown.Caption:=ClockInterval.Tostring;
  6.       CenterCountdown;
  7.       PlayCountDown;
  8.       dec(ClockInterval);
  9.     end
  10.   else
  11.     begin
  12.       GameClock.Enabled:=false;
  13.       countdown.Caption := 'GO!';
  14.       CenterCountdown;
  15.       if options.sounds[4] <> '' then PlayMediaFromFile(options.sounds[4]);
  16.       delay(1000);
  17.     end;
  18. end;  

And the other is a countdown clock with a variable duration set at runtime on the control form:

Code: Pascal  [Select][+][-]
  1. procedure TfScoreboard.GameClockTimer(Sender: Tobject);
  2. begin
  3.   if options.timer.interval <= 0 then begin
  4.     // Handle time expired
  5.     countdown.Caption := 'TIME''S UP!';
  6.     countdown.font.Size := 150;
  7.     countdown.font.name := 'Segoe UI Black';
  8.     centercountdown;
  9.     PlayWAVFromFile(options.sounds[7]);
  10.     GameClock.Enabled:=false;
  11.     exit;
  12.   end;
  13.   if options.timer.interval <= options.timer.WarningPoint then begin
  14.     // Handle playing warning tick-tock
  15.   end;
  16.   countdown.Caption := GetTimerString(options.Timer.Interval);
  17.   CenterCountdown;
  18.   dec(options.timer.interval);
  19. end;

The OnTimer event is changed depending on what procedure is using the timer by assigning it with OnTimer:=@procedure;

So what is happening is if I run either one individually they run as they should, but if I run one following the other it's almost as if both procedures are running .. the second one counts down by 2s and if I run it multiple times it gets faster so not sure. 

I would attach the code but even without the sounds and stuff it's 14MB.  If I need to post more of the code I can.

Rich
« Last Edit: May 31, 2021, 07:11:54 pm by rwebb616 »

Handoko

  • Hero Member
  • *****
  • Posts: 5536
  • My goal: build my own game engine using Lazarus
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #1 on: May 31, 2021, 07:17:19 pm »
Please provide a compile-able source code so we can test and help you fix the issue. This kind of problem is relatively hard to solve without inspecting the whole source code.

To make the source code smaller, you can remove the unnecessary things (form, unit, audio, images, etc) but keeping the code that can show the issue. Also don't forget to remove backup and lib folders and the binary file.

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #2 on: May 31, 2021, 07:20:59 pm »
Ok got it small enough but it won't have any resource files to play when running it - which I think is okay.  Probably at a minimum have to define a title slide and a scoreboard slide.

Also it's designed for multiple monitors so you'd have to have a second monitor for it to work right.

Handoko

  • Hero Member
  • *****
  • Posts: 5536
  • My goal: build my own game engine using Lazarus
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #3 on: May 31, 2021, 07:22:04 pm »
Sorry cannot help. I'm a Linux user.  :(

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #4 on: May 31, 2021, 07:23:44 pm »
Ah Yes, it's a windows only program - could probably rewrite it for Linux but that would be an undertaking :)

Requires the use of SDL2 library for sounds and I'm using a couple windows Fonts.. other than that I think it would work.

Rich

MarkMLl

  • Hero Member
  • *****
  • Posts: 8571
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #5 on: May 31, 2021, 10:40:21 pm »
Not having tried it but I'd observe that if anything in your handlers calls Application.ProcessMessages things are likely to go reentrant and you're thoroughly screwed.

I suggest disabling the timer on entry to the handler and re-enabling on exit, and instead of changing the handler use the timer's tag as a hint telling common code which handler should be run.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Timer with multiple different OnTimer functions - weird behavior
« Reply #6 on: May 31, 2021, 10:42:56 pm »
That's a good idea.. I always forget about the tag property.  I just rewrote it to use a boolean to tell the single ontimer procedure what section it was supposed to run... seems to work.

 

TinyPortal © 2005-2018