Occasional massive slowdowns when using LibFree / LibLoad

Hi,

My game has been slowing down (framerate drops to around 5fps) roughly every 10 mins of play, and I’m out of ideas as to why this is happening. What is strange is that the profiling information shows no change in framerate and no increase in Logic, Physics usage etc, and the game seems to be running at the same speed in the ‘background’. If I am printing to the console during this time, it outputs as normal. If I move my mouse outside of the embedded player, the game returns to normal after a few seconds, otherwise it takes about 40 secs to recover.

This always seems to occur when the character reaches the side of the screen / save-point / fast-travel / death, and the thing that these events all have in common is that they use the same script to LibFree anything that is not needed in the next map and then LibLoad the new assets (everything is managed within the same scene). The game runs smoothly 100% of the time without this script. This is a simplified psudo-code version of the process, which is called by an always sensor every frame:

    def change_map(self):
        always = cont.sensors['Always']

        if always.positive and new_map is not None:

            if self.transition_timer is not None and self.transition_timer == 1:
                self.transition_timer -= 1

                # Step 3: LibFree all unneeded assets


            elif self.transition_timer is not None and self.transition_timer > 0:
                self.transition_timer -= 1

                # Step 2: This passes the time while screen fades out


            elif self.transition_timer is not None:
                self.transition_timer = None
                new_map = None

                # Step 4:
                # LibLoad map
                # LibLoad any new player assets
                # LibLoad enemies
                # Etc

                # SCRIPT ENDS HERE

            else:
                self.transition_timer = 60

                # Step 1: SCRIPT BEGINS HERE
                # Start screen fade-out action

So my question is, is there something inherently bad about loading assets in this way? It seems strange that this is happening so infrequently, but when it does it seems to only effect the ‘graphical’ framerate rather than the logic ticks per second.

I understand that the newly imported materials are being compiled by the GPU (in my test scene there are 4 materials imported in total and 4 spritemaps at 2048x2048px, not a large amount). Would this cause a slowdown every 10 mins, or might it be something else?

Thanks.

Libload will hold the current frame till loading is done, then it skips to the next frame.
To load and be able to skip frames you should use: async = True, in the libload command.

Also you don’t have to check the always sensor, it already does that when the sensor activates in order to execute the script.

also, even if you libload assets async they still have to compile the shader when the thing loads.

solution = use megamaterial and spawn in object w/no texture and assign texture on spawn.

(this is also super handy for batching)