Game crashes if multiple clocks are active

I am making a cookie clicker clone for school, and I have a problem where using two clocks at the same time crashes the app when opening the screen with the clocks. If I disable any one of the 'when Clock.Timer' blocks, nothing crashes, only if they're both active at the same time.

Screenshots:


Hello John

Difficult to describe, but here goes:

  1. When you use the Blocks, they take precedence over what may be set in the Attributes Palette. Your clock timers have their 'timer interval' and 'timer always fires' Boolean set in the Palette - I recommend you use Blocks for those values too.

  2. 'Timer always fires' is very rarely required to be 'true': Will fire even when application is not showing on the screen if true.

  3. You should only start (enable) a timer when it is required and you should always stop (disable) a timer when it's task is done (can be start-stop as often as required).

  4. The clock timer is using Android's clock and it has a low system priority, starting two clocks at the same time could cause issues.

  5. You can allow the App to change the time interval of a clock on-the-fly if required but if the interval is too short, the required code execution may not finish before the clock is called again - that requires thought and experimentation by the App developer. If your App is to be distributed, be cautious and set generous time intervals to allow for the fact that some devices are slower than others.

  6. If your App has more than one screen, all clocks must be disabled before changing screens.

  7. If it was necessary to use 'Timer Always fires', ensure the Clocks are disabled before the App is exited.

  8. I think I need a coffee now.

2 Likes

Dear @Mann (John),
first of all welcome to the Community !
Everything said by @Chris is gold, but you can also think another hypotesis.
Since you say that the app crashes when you have both timers working, have you considered the fact of reducing to one clock only ?
In other words, like in a scheduler, you can have the faster clock (supposing every 50 ms) that does its job, then it increments a counter; if the counter reaches (for example) 10, then it executes also the task #2. So, every 10 times of clock1, it also executes task #2. In this case the clock is only 1 and it executes two tasks (every 10 ticks).
Something like:
image

Hoping it helps.
Ugo

Something important I missed out: unless impossible - avoid enabling clock timers during Screen Initialize. Enable a Clock timer when an event actually needs it.

Thank you both for replying. The issue is that the app crashes when switching to a screen that has multiple 'when Clock.Timer' blocks active. Even if they are both empty.

This is enough to crash the app if you press button1 a couple of times:
image

image

So I thought I could use one clock as uskiara suggested with a 1ms interval, and have two variables count up separately. But I found timers don't work with 1ms intervals, and now I don't know what to do.

Dear @Mann,
but also before, when you had two clocks, they couldn't fire @1 ms.
I have used clocks with a period of 10 ms, and they were working.fine, granting, more or less, a good response.
But, please be aware that any timer running over an Operating System will never be "precise", and a jitter will be always present because only when the OS has no other tasks in his queue, it will give time to a (software) clock.
Give it a try with the base clock @10 ms, then see if it is sufficient for your needs. Otherwise a "real time" operation shall use a dedicated external HW.
Cheers.

PS the annexed .aia shows a sample of a scheduler. In this code I've also timplemented the possibility that when a task is executed, it can also launch a subtask by enabling another clock. Maybe too complicated or useless for your needs, but just to show you how it could work. Inside it you'll find some "italian" words (like variables' names :slight_smile: ) but I hope this will not cause you confusion ... :innocent:
Ciao, ciao.

Scheduler.aia (442.7 KB)

PPSS After reading again your post: (these are hints that @Chris already said) are you sure that you disable the clocks before leaving a screen ? So another hint is: do you really need to change screens ?
I believe it would be much better whether you use "virtual screens". There are many posts on this matter in the Community. By doing a search you will find'em easily.
In the following .aia I have used an extension made by @shreyash ( :clap:) that allows applying fading effects that I use to switch between virtual screens, which are in effect only Horizontal or Vertical layouts. In this case you don't use more than 1 screen and in one screen I have used up to 7 clocks without any problem.
provafade.aia (1.4 MB)

....real End ... no more PS.... :sweat_smile:

2 Likes

Switch screens correctly...

Taifun

1 Like