Persistent Counter & Cooldown Timer for Slot Machine Mini-Game Across Screens

[HELP URGENT] Persistent Counter & Cooldown Timer for Slot Machine Mini-Game Across Screens

Hi everyone, its me agian

I'm developing a virtual pet game in App Inventor 2 and I'm stuck on a state persistence issue for a slot machine mini-game. I've tried several solutions, but I can't get it to work as expected.

Mini-Game Context:

  • The mini-game is on a separate screen (Screen5, according to my blocks) from the main game screen.
  • The user has 5 attempts to play the slot machine.
  • Once all 5 attempts are used up, the game enters a 5-minute cooldown period. During this time, the user cannot play, and a countdown timer is displayed.
  • After 5 minutes, the attempts are reset to 5, and the user can play again.

The Problem:

The attempts and cooldown logic works correctly as long as the user stays on Screen5 (the mini-game screen). However, the critical problem arises when:

  1. The user plays (uses some attempts or all 5).
  2. Clicks a "Return" button to go back to the ScreenPrincipal (or Screen2).
  3. Returns to Screen5 (the mini-game screen).

Upon returning to Screen5, the attempt counter resets to 5, and the countdown timer disappears or doesn't work, allowing the user to play again immediately, which is not the desired behavior. I want the attempts and cooldown time to persist even if the user switches screens or closes and reopens the application.

What I need:

I need a robust structure to:

  1. Persist global Intentos and the cooldown status (if active) across screens and app sessions.
  2. Display an accurate countdown (MM:SS format) when the game is in cooldown.
  3. Have the game automatically reset to 5 attempts once the cooldown period has ended, even if the user has left and returned to the screen.

Could someone please help me restructure this logic to achieve the desired persistence and countdown, using Clock.SystemTime as suggested? I'm quite overwhelmed with all the changes and haven't managed to get it working.

Thank you very much in advance for any help you can provide! :pray:

Snark_tumascotapasivoagresiva_1 (4).aia (798.2 KB)

to store persistent data, you need TinyDB.

I would use these TinyDB tags:

  • remainingCooldownSeconds (0 if not in cooldown or not found)
  • remainingSlotAttempts (5 if not found, 0 if in cooldown)

I would also add a OneSecondClock in Screen5, 1000 milliseconds, Enabled while in the Screen but disabled when leaving the screen. Turn off its Always flag so it does not run in the background.

Do not use global variables for those.
Just work from TinyDB.

Code an initialize procedure, to be run on entry to Screen5 both in Screen5.Initialize and in Screen5 When Other Screen Closed.

initialize:

If TinyDB remainingCooldownSeconds  > 0 then
  Set mm:ss Label visible and load it from  formatted remainingCooldownSeconds  
else 
    Set mm:ss Label invisible
if remainingSlotAttempts  > 0 then
  Set slots Arrangement Visible
else
  Set slots Arrangement inVisible
end proc

When OneSecondClock Timer:

if remainingCooldownSeconds > 1 then
  set remainingCooldownSeconds to remainingCooldownSeconds  - 1
  save remainingCooldownSeconds back in TinyDB
  display the formatted seconds remaining
else if remainingCooldownSeconds  = 1 then
  set remainingCooldownSeconds to 0
  save remainingCooldownSeconds back in TinyDB
  Set slots Arrangement Visible
  set remainingSlotAttempts  to 5
end Timer

After a slot game:

Decrement remaining slot attempts
If remaining slot attempts<= 0 then
  Hide slots 
  Set remaining cooldown seconds to 300
  Show cooldown mm:ss
End if
1 Like