0

First I'll explain what I want to do and afterwords I'll provide a proposed solution.

Problem

I'm running a game where I want to do a certain amount of work every frame. For example, I have N objects that are in a queue waiting to be initialized (imagine initialization is a fairly expensive operation and N is large) and after adding them all, I want to create their collision boxes, and after that, I want to merge them together to limit render calls. I can't do these operations on a different thread because all this stuff is heavily coupled with the game world. But I want to split up all these operations into bite-size chunks to run each frame so that there is minimal lag (framerate dips). How would I go about doing this?

Proposed Solution

It would be nice to have a function that can stop after one call and continue where it left off after calling it again:

For example,

boolean loadEverything() {
    for (int i = 0; i < objectsToAdd.length; i++) {
        world.add(objectsToAdd[i]);
        if (i % 10 == 0) {
            return stop();
        }         
    }

    makeCollision();
    return stop();

    mergeObjects();

    return true;
}

Calling loadEverything() the first objectsToAdd/10 times adds 10 objects to the game world at a time. Then calling it after should run makeCollision() and then stop. Calling it again runs mergeObjects() and then the function returns true. In the caller function I would run loadEverything() until it returns true.

I'm aware of yeild-return, yield-break implementations like those described here exist, but I'm wondering if there's a more general implementation of them, or that maybe a better solution exists that doesn't require any extra dependencies.

Luca
  • 171
  • 1
  • 13

2 Answers2

0

Do you look at Coroutine yet? There's native implementation in Kotlin but in Java there're options here and here.

By all means we need to make sure those OpenGL or Box2D operations that required to be in main thread should be in main thread, as I believe coroutine will be created under a new thread. So there might not be gain to split works for those kind of operations.

Another option

You say you need to split works in creating objects in run time. Can you predict or estimate the number of objects you would want before hand? and so if you don't really need to dynamically create object like that, I suggest to look at Object Pool in libgdx (see more here). That link has working example to use Pool in your game.

Such Pool already have initialized objects ready to be grabbed and used on-demand, also can grow if need in run time, so initially if you can provide a good estimation of number of objects you intend to use, it's all good.

haxpor
  • 2,431
  • 3
  • 27
  • 46
0

Why don't you add one static variable which would keep it's value between function calls? Then you can loop from current value to current value + 10, increase current value (that static variable) by 10 and exit.

MilanG
  • 6,994
  • 2
  • 35
  • 64