2
\$\begingroup\$

I’ve been trying out Java Monkey’s multi threading recently via the wiki’s tutorial. Since I just wanted to test a prototype clinet, I made a program which creates some cubes using the ‘Cubes’ library (hub.jmonkeyengine.org/wiki/doku.php/jme3:contributions:cubes), and then serializes it to some byte data (which is what the server would do to the map chunks near a player). The aim is to imitate a chunk being sent from the server, which would occur in a separate thread, as mentioned in the networking tutorial on the wiki (hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:networking).

Each chunk would have it’s own node to store it, and the chunk would be replaced if the server sent either an update of the chunk, or if the player moved into a new region (which would cause the chunk to be replaced). The code is here. The issue is though, is that I need to recreate an empty chunk before I can read from the bytes, which requires collecting the application settings. As well as this, I cannot modify the game scene within a separate thread (which the function does). The code below is from line 93+.

//...
//(In Update Loop)
if (future.isDone()) {
    //Binds the temporary data to the clone object
    blockTerrainClone = (BlockTerrainControl) future.get();
    future = null;
//...

Callable<BlockTerrainControl> serverInput = new Callable<BlockTerrainControl>() {
    public BlockTerrainControl call() {
        /*Creates a temporary data storage object, then assigns the data. 
        I can't mutate the current BlockTerrainControl which is being rendered, 
        as this is a seperate thread.*/
        BlockTerrainControl data = new BlockTerrainControl(CubesTestAssets.getSettings(app), new Vector3f());
        CubesSerializer.readFromBytes(data, serializedBlockTerrain);

        //Returns the data
        return data;
    }
};

The multithreading wiki article (near the bottom) does partially cover how to modify the scene and get access to the game state, but not into much detail. Would anyone be able to expand on that section, and explain how I may be able to transfer the blocks from the server to the client in a separate thread (or in this case, make the data transfer from one thread to another)?

If I missed out anything in the question, I'd be happy to add it!

\$\endgroup\$
1
\$\begingroup\$

You might want to have a look at the monkeyzone example, and use the `ActionMessage? or some other Message class to send messages between client and server. You can broadcast from server to the client and the clientlistener invokes a callback e.g.

app.enqueue(new Callable<Void>() {
    public Void call() throws Exception {
        app.doActionByBroadcast();
        return null;
    }
});
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.