What's causing my game to freeze for several seconds when changing scene?

It’s been a few weeks since I’ve asked a stupid question, so here we go again. I’m pretty certain the answer to this is better memory management - something I have only a scant grasp of.

I’m using Director class. When I move from the menu screen to the main game screen, it takes about 10 seconds for the main game to load even though the game in its entirety is rather laughably simple.

No problem, I thought, I’ll make a loading animation to help players through those 10 seconds. But that animation freezes as well.

At this point, I’m guessing the slew of sprites and sounds that I’m preparing right at the top of my main game file are probably hogging so much memory, not even a simple animation can run uninterrupted.

So I suppose this is two separate questions:

  1. Is there any way to “reserve” a chunk of memory so that there’s never total freeze; that something small can always reassure the player that stuff is being processed.

  2. What’s the best way of dealing with preparing all the sprites and assets that I need for the game? I’ve been preloading them all on scene change under the belief that if I leave the loading until they’re needed, that I’ll then encounter an unacceptable delay at that point - but maybe this isn’t the case?

Sorry to be vague/stupid/clueless…

Cheers,
Andrew [import]uid: 132606 topic_id: 33079 reply_id: 333079[/import]

I think we are going to need to see code to help.
[import]uid: 19626 topic_id: 33079 reply_id: 131385[/import]

I think we are going to need to see code to help.
[import]uid: 19626 topic_id: 33079 reply_id: 131385[/import]

Rob is correct, we’d need to see some code. A delay this painfully long leads me to guess it’s a sound issue, as in, you’re loading some insane amount of sound files into memory. Sprites by themselves should not be as time-intensive to load, but again, if your sprite sheets are absurdly large in size, it could be contributing.

Basically, you should never load more sounds into a “level” than you’ll play in that level. And you should never preload songs, those must be streamed in. Likewise with images and sprites: don’t load more than you need.

As for the actual method, you’ll need to figure it out. But loading every game asset into memory in main.lua is simply not the correct method. :wink:

Brent [import]uid: 9747 topic_id: 33079 reply_id: 131448[/import]

Rob is correct, we’d need to see some code. A delay this painfully long leads me to guess it’s a sound issue, as in, you’re loading some insane amount of sound files into memory. Sprites by themselves should not be as time-intensive to load, but again, if your sprite sheets are absurdly large in size, it could be contributing.

Basically, you should never load more sounds into a “level” than you’ll play in that level. And you should never preload songs, those must be streamed in. Likewise with images and sprites: don’t load more than you need.

As for the actual method, you’ll need to figure it out. But loading every game asset into memory in main.lua is simply not the correct method. :wink:

Brent [import]uid: 9747 topic_id: 33079 reply_id: 131448[/import]

Hi Rob and Brent - I’m sorry to leave you both hanging after you were so courteous to give me quick replies. Brent - you’re dead right, it was largely the sounds that were causing the issue. I was loading nine mp3s and this accounted for about 90% of the lag. The rest was down to various spritesheets loading before I really needed them.

I have a feeling I could optimise it even further, but I have delay between scene switching down to about 1 or 1.5 seconds and for now I’m OK with that. I might revisit this topic later after I have all the features complete.

Many thanks for your input and help.
Andrew [import]uid: 132606 topic_id: 33079 reply_id: 132014[/import]

As for the audio, you should also consider loading lower quality sounds too.

A 44khz stereo sound will sound great on headphones but on the speakers of our devices, you’re wasting memory and CPU cycles to process them. You should consider 11Khz Mono sounds which will drastically speed up load times in particular on older devices and devices like the Barnes and Noble Noon Color.

I have a client who insisted on having higher quality sounds, so what I ended up doing is having two folders in my audio: audio/low and audio/high

I put the 44khz stereo sounds in the high folder and the 11khz mono sounds in the low folder.

Then if I check to see if I’m android and if I do, I drop to the low res sounds with something like:

 local res = "high"  
 if system.getInfo("platformName") == "Android" then res = "low" end  
  
 sfx.create = audio.loadSound("audio/" .. res .. "/CreateGameScribble.wav")  

Though I suspect I should look at the models a bit more to maybe give some of the new HD android devices some love.
[import]uid: 19626 topic_id: 33079 reply_id: 132015[/import]

Hi Rob and Brent - I’m sorry to leave you both hanging after you were so courteous to give me quick replies. Brent - you’re dead right, it was largely the sounds that were causing the issue. I was loading nine mp3s and this accounted for about 90% of the lag. The rest was down to various spritesheets loading before I really needed them.

I have a feeling I could optimise it even further, but I have delay between scene switching down to about 1 or 1.5 seconds and for now I’m OK with that. I might revisit this topic later after I have all the features complete.

Many thanks for your input and help.
Andrew [import]uid: 132606 topic_id: 33079 reply_id: 132014[/import]

As for the audio, you should also consider loading lower quality sounds too.

A 44khz stereo sound will sound great on headphones but on the speakers of our devices, you’re wasting memory and CPU cycles to process them. You should consider 11Khz Mono sounds which will drastically speed up load times in particular on older devices and devices like the Barnes and Noble Noon Color.

I have a client who insisted on having higher quality sounds, so what I ended up doing is having two folders in my audio: audio/low and audio/high

I put the 44khz stereo sounds in the high folder and the 11khz mono sounds in the low folder.

Then if I check to see if I’m android and if I do, I drop to the low res sounds with something like:

 local res = "high"  
 if system.getInfo("platformName") == "Android" then res = "low" end  
  
 sfx.create = audio.loadSound("audio/" .. res .. "/CreateGameScribble.wav")  

Though I suspect I should look at the models a bit more to maybe give some of the new HD android devices some love.
[import]uid: 19626 topic_id: 33079 reply_id: 132015[/import]