Updating positions from MySQL database

Hello!

I am currently running this block of code inside my Render method:


        if (this.frame == 60) {
            try {
                otherPlayers = database.getPlayers();
            } 
            catch (Exception e) {
                e.printStackTrace();
            }            
            this.frame = 0;
        }
        this.frame++;
        
        for (int i = 0; i < otherPlayers.size(); i++) {
            Player newPlayer = otherPlayers.get(i);
            
            g.drawImage(newPlayer.getFigure(), newPlayer.getX() - 30, newPlayer.getY() - 53);
            g.setColor(Color.white);
            g.drawString(newPlayer.getName(), newPlayer.getX() - 30, newPlayer.getY() + 17);
        }

It updates the players in the games position every 60 frames. However, the first bit of code where it actually requests the new info from the MySQL makes the game FPS drop almost 50%!
The target FPS is 60, but when it’s requesting new positions every 60 frames, the framerate is between 30 and 50. How do i fix this? :slight_smile:

Ughh, well, polling the database can be relatively costly and you’re doing this once every second. You’re bound to encounter some lag, especially since it looks like you’re not only deserializing the players updated position but the entire structure stored for them. Are you sure you have no alternative way you can go about this? It looks like you all you’re doing is synchronizing server state with your clients. How about creating a central server which keeps track of player state and updates the client at a set interval?

edit: I think it’s worth adding that you quoted that block of code from your Render method. You should separate game logic from rendering, as far as game logic is concerned rendering should be a read only operation.

Dont fetch stuff from the database :).
You should use an client / server construction if you want to have multiplayer.
The server would have all active entitys in memory, so it would not take so long.
New entitys gets loaded from the database and then gets managed in the server, storing them at some interval.
Also the data fetching should be in an diffrent thread, so you wont need to wait for new info.

Doesn’t the Update method only run when there’s a player input? :slight_smile:

How would i approach this? I have no idea how i´d start that. Any tutorials? :slight_smile:

Fetching stuff from database is modern day equivalent of reading data from a tape drive.

I do wonder why you’re using a database. Besides, using framerate to control the polling frequency into your database is awfully wrong. What happens if I run the game at 20000 fps or 5 fps?

I’m using MySQL because i’ve made webapplications for some years, so i’m familiar with that :slight_smile: The framerate control of updates is going to use Deltatime, this is just for testing purposes

Maybe I should have put that a little differently, when I said update what I meant was synchronize state with the clients. My bad :slight_smile:

OP: for such a simple game I’d have a server run which handles input and sends off state updates (anything such as a new position) every set period. For example:


long lastUpdate = 0;
while(running) {
    handleInput(); // pop events off of a queue which is populated by the networking thread
    long delta = System.currentTimeMillis() - lastUpdate;
    if(delta >= synchronizationInterval) {
        for(int i = 0; i < connectedPlayers.length; i++) {
            Player player = connectedPlayers[i];
            // send updates to this player
        }
        lastUpdate = System.currentTimeMillis();
    }
}

I can see the sense of having a server to handle the game state and now and then write the current state to the database. I, however, have absolutely no idea how to write such a server. How would you suggest me learning it? :slight_smile:

Well – there’s really countless ways to go about implementing a client-server model for a game. However for now I’d suggest you keep it simple and look into the thread per client model.

.
If you don’t want to get into the nitty gritty details of networking code right nwo and just want to progress on with your game you could also use a pre-existing third party library which abstracts a lot of the stuff like Netty or Apache MINA.

https://netty.io/
http://mina.apache.org/

edit: http://docs.oracle.com/javase/tutorial/networking/overview/networking.html

As a lib I would choose kryonet: http://code.google.com/p/kryonet/
I wrote my first Multiplayer in UDP, it’s funny to deal with dataloss (write own “tcp-methods”) etc. But I don’t know how much time you wanna spent on the project.
If you don’t have that much time and want an effective solution, you should use a lib.
best regards

Thank you for the really great answers!

I think i’ll try to use a framework to get going. I guess later on i can read more up on the networking stuff, i’ll probably have classes about it in school as well (just started 3 months ago studying computer science).

The game is going to run on the browser. Which framework would be easiest and best to use?

My recommendation is ditch the database completely. Keep it in memory. You’re trying to write a game like a web app, and the more you hang on to that model, the worse it’s going to make the code and your experience writing it.

It should turn out as an applet. However, i did choose to write a gameserver, which just loads up the MySQL database at startup :slight_smile: