Loading resources in the background?

Monkey Forums/Monkey Programming/Loading resources in the background?

Anatol(Posted 2012) [#1]
Hi,

I'm just wondering if there's any way in Monkeycode to load resources "in the background", i.e. to load images/sounds while the update loop is running.

I frequently need to load and discard full screen (768 x 1024) images and sound files. This is managed by a sort of a resource manager that preloads and discards resources as necessary - this happens in OnUpdate(). I just notice a very obvious stutter (the app freezes temporarily) of up to 1-2 seconds when some more heavy resources are preloaded. I especially notice a significant stutter on an iPad compared to (local) HTML5.

I can find other ways to make this less obvious (luckily the sound doesn't stop playing) but for the off-chance that loading in the background is possible somehow I would appreciate any hints, even if this is just possible on certain targets.

Cheers!
Anatol


Gerry Quinn(Posted 2012) [#2]
You can certainly load things in the background in Flash using the URLLoader object.


benmc(Posted 2012) [#3]
I do this in all my games.

The process is this:

1) Enter a pre-loading screen that just displays my "Loading" screen image.

2) Step forward to the actual loading method that then loads all the bitmaps, sounds, level xml, etc. All the while, the loading image is displaying because I showed it in the pre-loading screen where - right after the loading image was displayed, it triggered my app to go to the loading method.

3) The loading screen also displays the loading graphic.

4) Once all the data has been loaded, I have it move on to the next part of my game.

EDIT: I do all my loading in OnUpdate. OnCreate just moves me into the Pre-Loading state for my game, then to Loading, then to a Menu.


Anatol(Posted 2012) [#4]
Hi,

Thank you for your suggestions.

Hm, yes, a loading screen would work well for games in between levels. I'm working on an interactive book, however, and for this any screen between pages is not really a good solution - it would pull you right out of the story.

I can work around that in some ways and keep the music playing and maybe generally freeze any animations on page turns. Or I find a way to load resources in the background, in a way that doesn't temporarily freeze the OnUpdate loop. Maybe some targets (like Flash) or hopefully iOS provide a way to do that. I'm thinking of calling a function/method in OnUpdate that initiates loading resources but doesn't "interfere" with OnUpdate and doesn't delay the processing of further Monkey code. I'll look further into this - and if that's possible at all I assume it's a target specific customisation. Maybe some changes in mojo/native? Brrrr.... If I find a solution I'll post it here.

Basically on every page turn the code discards and loads resources, the app always has the resources of the current, previous and next page loaded. E.g. when I turn from page 3 (loaded resources: page 2,3,4) to page 4 the resources of page 2 are discarded and page 5 is preloaded.

Anyway, I'll keep looking for a solution. Any further ideas are still very much appreciated!

Cheers!


secondgear(Posted 2012) [#5]
What we are missing is support for multiple threads. I don't know if all target platforms support threading.


offtehcuff(Posted 2012) [#6]
You could roll your own resource loaders. You'd have to read in X amount of data each frame and when the whole files is loaded turn it into whatever type of resource it is.

You could also load more forward pages. The typical usage is to keep reading forward so you can try to improve that part of the experience. This is common in linear games. Everything is seamless until you try to backtrack. The game has to stop and reload the previous area.

I would say on page 3 you could have 2,3,4,5 and if space is really limited 3,4,5. Think about it this way; if the reader has stopped to go back the flow is already broken. They can wait the 3 seconds to go back.

Combine the 2 suggestions and you should be constantly 2 pages ahead and no single read operation will slow down the book.


Anatol(Posted 2012) [#7]
Thanks for these suggestions, offtehcuff. I will give it a try - it'll still take me a while as I have to iron out some other issues first. I'll post here again if I find a workable solution; it sounds like these 2 suggestions could solve the issue.


Rixarn(Posted 2012) [#8]
I've been very busy with some work projects but I'm building a framework for my IPAD game. The framework itself will be BSD and it's composed of a collection of modules (some months ago I released one of the modules, a state machine module). The resource manager module allows you to do exactly what you want. It's easy to use (I think) but the reason I haven't published it yet is that it lacks proper documentation.

If you like I can email to you the framework with some simple instructions. In any case, I expect to release it in the following weeks.

Good luck!


Anatol(Posted 2012) [#9]
Hi Rixam,

that would be very kind! If you don't mind to email it to me, that would be fantastic!

I really appreciate the community here. I don't have a game programming background, but once I have a bit more solid experience with Monkey I hope I can contribute more myself.

Cheers!


Rixarn(Posted 2012) [#10]
Anatol,

I'm testing the framework before emailing to you, because I don't want to hand broken code he he... (The loading part at least) Once I've sorted out the bugs I'll make a public release.