Frame-Rate Independent Tickrate

Really stupid question, how would you make the tick rate of something like an enemy not go by FPS?

Well if I understand you correctly, then what I would do is have a thread running at a given fps or as fast as possible, that would update all entities. Then have the opengl thread run at the given FPS. :slight_smile:

CopyableCougar4

Not a stupid question at all!

The easiest, and simplest way, is to pass a float into your update method that represents the time past since the last update (AKA : deltaTime)

Your update method would look something like this :

public void update(float delataTime) {
exampleObject.move(speed * deltaTime);
}

Please note that this solution will cause small floating-point errors that can negitively affect networked games.

Edit
Creating a new thread in this situation is uneccesary, and will create more problems then it will solve.

Well, I am doing that for movement and e.t.c. But for like entity update methods I do this

public void update()
{
    // Every second tick
    if (ticks % 2 == 0)
    {
        // Move entity, or something that should be done every second tick.
    }
    ticks++;
}

The problem with that is, at higher fps, the ticks are done way too fast. I’ve tried doing something with delta time, but it didn’t work too well :stuck_out_tongue:

That depends entirely on how many entities need to be updated and what is involved in updating entities. If there is too much involved in updating entities, then rendering framerate will suffer. If the updating requires no rendering or opengl functions and the updating takes a lot of time, then multithreading is superior. :slight_smile:

In summary, if the updating entities takes a while and doesn’t have to use opengl for anything, then properly handled multi-threading may be necessary. However, the actual determination of whether it is needed depends on more information.

CopyableCougar4

Multi-threading doesn’t really seem necessary in this case, as it mainly has logic that wouldn’t take long at all.

Absolutely, having worker threads that handle slow operations can be usefull in certain cases (But would not make the update rate indipentant of the FPS).
My argument was against moving the entire update loop to another thread.

Yeah, if you can’t guarantee a fixed FPS and need a fixed logic rate, run your logic loop in it’s own thread, and it can push out the ‘latest update’ to the render thread after every new logic frame.

Of course, if you can limit FPS to a certain value (60 is the correct value :P) then that’s what you should do.

Ah, so just have like an “entity thread”, where all of the entities do their updating there?

Yep, in fact not just entities, but anything that runs off the update(), aka isn’t rendering.
But you can put both in one loop (thread) if you just limit the FPS, and that is the recommended way.

Yeah, I could limit it to 60. But what if people want to play at higher fps, should I just like add a setting in the game’s config like "max-fps: ", and put a comment above it that says it could break things?

I would not recommend this for multiplayer, but that’s just me :slight_smile:

CopyableCougar4

Or have the limit options be multiples/factors of the logic rate.

If your logic should update at 30 Hz (example), then FPS caps could be
30 (n = 1)
60 (n = 2)
90 (n = 3)
120 (n = 4)

And you just call update() every nth iteration.

Yeah, I am thinking of adding multiplayer, so I need to also keep that in mind :slight_smile:

That is also another thing I wondered, how would you only call update every nth iteration, currently it calls as fast as possible iirc.


int count = 0;

loop {
   if (count == n) {
      update();
      count = 0;
   }

   render();
   count++;

   sleep();
}

Lol I just saw [icode]count = 0;[/icode] after I posted. Then a went OOPS! and deleted my post :slight_smile:

CopyableCougar4

Oh cool! Thanks!

Shouldn’t update and render be flipped? So that it updates more than it renders? Just a question. :slight_smile:

CopyableCougar4

Yeah, [icode]count % n == 0[/icode] works fine too, and then you don’t have to reset to zero. (actually better since it calls update() on frame 0)

Only if the render rate is faster than the logic rate.

EDIT: That made more sense in my head. Whichever call is in the if statement is the one which will be slower. So if logic is to be 30, and fps is 60, then logic should be every other frame.