Assistance with freezing!

PEACE UNTO YOU ALL! THANKS FOR ALL YOUR HELP IN ASSISTING ME WITH MY QUESTIONS IN MAKING THIS GAME!

Right now I am in closed testing for my game for Android devices. I got it to where the game doesn’t crash, so the app doesn’t force close and crash per se. But what does happen is every blue moon, and it’s very random. So in that perspective it’s good that it doesn’t happen consistently, because it’s very random. But randomly when the main game is being played and the player dies, inside the main game a game over screen appears over top of the main game. And a countdown happens. And when the countdown reaches 0 from 5 seconds, it goes 5, 4, 3, 2, 1, 0. Once the countdown reaches 0, then the loader script is supposed to receive a message that says, Load the main menu. I already have the script preloaded in the background. So, specifically, may you please explain how it’s possible for the game to freeze when trying to load the main menu again after the game over screen reaches 0 and the message is being sent? I have the checks that preload the main menu. So, how could it freeze? It isn’t a crash, it just freezes. And since it happens randomly, I want to try to figure out if I can figure it out before the game releases. But even if the game releases, because it happens so far and few and randomly, it’s kind of hard to still not put out the game, because the game is in a good state. Everything just works perfectly. It’s literally just sometimes when the game over screen reaches 0 it’s supposed to load the main menu screen, for whatever reason, the game just freezes and doesn’t do it. What is every possible reason that it could be so that a freeze happens? What could be the reason? Because normally it just works. It works perfectly fine every other time. So that lets me know that it’s working. So, what could have happened?

If I had to put a ratio to it I would say 1 every 10 times so it’s very rare but I’m still new to development as this is my first game so please assist me. Here is my loader script. What could I do to ensure everything works like it usually works cause the overwhelming amount of time it works fine. Also if you wanna be a tester for the game let me know!

Thank you! We are very grateful!

Loader Script:

local function load_menu(self)
    print("Loading Menu...")
    msg.post("go#menu", "load")
    msg.post("go#menu", "enable")
end

local function unload_menu(self)
    print("Unloading Menu...")
    msg.post("go#menu", "disable")
    msg.post("go#menu", "unload")
end

local function preload_main(self)
    print("Preloading Main Game...")
    msg.post("go#main_game", "async_load") -- Preload the main game in the background
end

local function preload_menu(self)
    print("Preloading Menu...")
    msg.post("go#menu", "async_load") -- Preload the menu in the background
end

local function load_main(self)
    print("Loading Main Game...")
    timer.delay(0.01, false, function()
        msg.post("go#main_game", "load")
        msg.post("go#main_game", "enable")
    end)
end

function init(self)
    print("Initializing Loader Script...")
    msg.post(".", "acquire_input_focus")

    -- Load the menu initially
    load_menu(self)

    -- Preload the main game and menu in the background
    preload_main(self)
    preload_menu(self)
end

function on_message(self, message_id, message, sender)
    if message_id == hash("start_game") then
        print("Starting Game...")
        unload_menu(self)  -- Unload the menu before starting the game
        load_main(self)    -- Load the main game collection
        admob.hide_banner()
    elseif message_id == hash("load_menu") then
        print("Returning to Menu...")
        -- Properly unload the main game before loading the menu
        msg.post("go#main_game", "disable")
        msg.post("go#main_game", "unload")
        load_menu(self)
    elseif message_id == hash("proxy_loaded") then
        -- Log which proxy has been loaded
        if sender == hash("go#main_game") then
            print("Main Game Proxy Loaded.")
        elseif sender == hash("go#menu") then
            print("Menu Proxy Loaded.")
        end
    elseif message_id == hash("proxy_unloaded") then
        -- Log which proxy has been unloaded
        if sender == hash("go#main_game") then
            print("Main Game Proxy Unloaded.")
        elseif sender == hash("go#menu") then
            print("Menu Proxy Unloaded.")
        end
    end
end

You’re testing on android, you should check the logs on the android device for Defold. Here’s a tutorial on how you can do that: Debugging - game and system logs

2 Likes

Can you reproduce it on your PC or only on Android? Do you see something in the log? Remember that you need to test with a debug version to see your print()s and any errors coming from the engine or Lua.

I noticed that you are not loading your collection proxies as recommended in the documentaion. You should post a “load” or “async_load” message, wait for “proxy_loaded” message and then post an “enable” message. You post both a “load” and “enable” at the same time. I’m not sure if this is the source of your problems but I would recommend that you change it.

Example: Proxy

4 Likes

Peace Unto You! Thank you for your response! So to my knowledge I updated my loader script so that it waits now, I’m still new to coding so forgive me if I misunderstand anything. I’ll give my new loader script so you can see if I addressed the issue you brought up, but even with this new script the freezing still happens randomly.

To answer your question, I haven’t been able to re produce it on PC in my defold editor, so today I’ll just leave it running and constantly hit start game to see, but it only happens to my knowledge on my Android phone.

Is there any way to hook my phone up to the debugger?

local function preload_menu(self)
    if not self.menu_preloaded then
        msg.post("go#menu", "async_load")
        print("Preloading Menu in the Background")
    end
end

local function load_menu(self)
    if self.menu_preloaded then
        msg.post("go#menu", "enable") -- Enable directly if preloaded
    else
        print("Error: Menu not preloaded before load request")
    end
end

local function unload_menu(self, callback)
    msg.post("go#menu", "disable") -- Disable the menu
    msg.post("go#menu", "unload") -- Unload the menu to free memory
    self.pending_unload = "menu"
    self.unload_callback = callback
end

local function preload_main(self)
    if not self.main_game_preloaded then
        msg.post("go#main_game", "async_load")
        print("Preloading Main Game in the Background")
    end
end

local function load_main(self)
    if self.main_game_preloaded then
        msg.post("go#main_game", "enable") -- Enable directly if preloaded
    else
        print("Error: Main game not preloaded before load request")
    end
end

local function unload_main(self, callback)
    msg.post("go#main_game", "disable") -- Disable the main game
    msg.post("go#main_game", "unload") -- Unload the main game to free memory
    self.pending_unload = "main_game"
    self.unload_callback = callback
end

local function preload_main2(self)
    if not self.main2_game_preloaded then
        msg.post("go#main2_game", "async_load")
        print("Preloading Main2 Game in the Background")
    end
end

local function load_main2(self)
    if self.main2_game_preloaded then
        msg.post("go#main2_game", "enable") -- Enable directly if preloaded
    else
        print("Error: Main2 game not preloaded before load request")
    end
end

local function unload_main2(self, callback)
    msg.post("go#main2_game", "disable") -- Disable the main2 game
    msg.post("go#main2_game", "unload") -- Unload the main2 game to free memory
    self.pending_unload = "main2_game"
    self.unload_callback = callback
end

function init(self)
    msg.post(".", "acquire_input_focus")

    -- Preload states
    self.menu_preloaded = false
    self.main_game_preloaded = false
    self.main2_game_preloaded = false
    self.pending_unload = nil
    self.unload_callback = nil

    -- Initial setup
    preload_menu(self) -- Preload menu at startup
    preload_main(self) -- Preload main game in the background
    preload_main2(self) -- Preload main2 game in the background
end

function on_message(self, message_id, message, sender)
    if message_id == hash("proxy_loaded") then
        if sender == msg.url("go#menu") then
            self.menu_preloaded = true
            print("Menu preloaded and ready.")
            -- Initialize menu after it's preloaded
            load_menu(self)  -- Explicitly load and enable the menu after it is preloaded
        elseif sender == msg.url("go#main_game") then
            self.main_game_preloaded = true
            print("Main game preloaded and ready.")
        elseif sender == msg.url("go#main2_game") then
            self.main2_game_preloaded = true
            print("Main2 game preloaded and ready.")
        end
    elseif message_id == hash("proxy_unloaded") then
        if self.pending_unload then
            print(self.pending_unload .. " unloaded.")
            self.pending_unload = nil
            if self.unload_callback then
                self.unload_callback()
                self.unload_callback = nil
            end
        end
    elseif message_id == hash("start_game") then
        -- Transition from menu to main game
        unload_menu(self, function()
            preload_main(self)
            load_main(self)
        end)

    elseif message_id == hash("start_master_mode") then
        -- Transition from menu to main2 game (Master Mode)
        unload_menu(self, function()
            preload_main2(self)
            load_main2(self)
        end)

    elseif message_id == hash("load_menu") then
        -- Transition from either game to menu
        local function load_menu_callback()
            preload_menu(self)
            load_menu(self)
        end

        if self.main_game_preloaded then
            unload_main(self, load_menu_callback)
        elseif self.main2_game_preloaded then
            unload_main2(self, load_menu_callback)
        else
            load_menu_callback()
        end
    end
end

Yes: Debugging - game and system logs