简体   繁体   中英

Java - Problems with serialized objects within serialized objects

Hey all I'm making a game for funsies and i've solved most problems by researching and reading but now i'm stuck.

Quick explanation of game: Two clients connect to server and game starts. Server performs all calculations and sends A serialized GameState object to the clients each cycle. GameState being just a bunch of variables showing the position of players and a some getters/setters. This works fine. However, now I have made an arrayList of Missiles in the gamestate, Missiles being a separate object and also serializable. Both client and server have an identical copy of the GameState class and Missile class.

Missiles get added when the client presses space and this DOES get registered by the server. I made the server print the count of missiles in the server version of gamestate. Printing the count of missiles on client side is always zero. Here's some code I've tried to put relevant stuff in only.

Server sending the object to player, player.objectout just writes an object to objectoutputstream

public void updatePlayerOutput(){
    GameState send = new GameState(gameState);
    leftPlayer.playerObjectOut(send);
    rightPlayer.playerObjectOut(send);
}

The main part of GameState(left out getter and setters). Left and right = player1 and player2

public class GameState implements Serializable {

private double leftX;
private double leftY;
private double rightX;
private double rightY;
private ArrayList<Missile> missiles;

public GameState(){
    missiles = new ArrayList<Missile>();
}

public GameState(GameState game){
    leftX = game.getLeftX();
    leftY = game.getLeftY();
    rightX = game.getRightX();
    rightY = game.getRightY();
    missiles = game.getMissiles();
}

Missile class

  public class Missile implements Serializable {

private double x;
private double y;

private double dX;

public Missile(double inX, double inY, double inDX){
    x = inX;
    y = inY;
    dX = inDX;
}

public Missile(Missile inMissile){
    x = inMissile.getX();
    y = inMissile.getY();
    dX = inMissile.getDX();
}

And finally where the client reads. panel is a JPanel and uses gamestate to get locations to drawimages

while(true){
    gameState = (GameState)(ois.readObject());
    panel.setGameState(gameState);
    panel.repaint();
    updateOutput();
    }

Also on a related note(possibly the reason for my main question), in the first section of the code i linked I have to create a completely new GameState object to send. Why is this? If I try to just send the gameState variable strightup none of the information is preserved. Thanks :)

So, if I understand correctly, the server has a game state, serializes it, and sends it to the players. The players then modify the content of the game state they received, and you're surprised that the game state on the server doesn't contain the updates.

That's expected. Serialization doesn't send a remote reference to the object. It transforms the object to a sequence of bytes, sends this sequence of bytes, and the object is then reconstructed from the sequence of bytes by the receiver.

To make an analogy, sending an object using serialization is like taking a paper document and fax it. Whatever the receiver writes on the received fax won't magically appear on the original document.

The clients must send the updates to the server if you want the server to be aware of the changes.

Regarding the last part of the question, if I understand correctly, you tried to send the same object multiple times to the same ObjectOutputStream, and the receiver didn't see any difference between the first object state, and the subsequent ones. This is also expected. Sending the same object a second time only sends a reference to the previously sent object. This is necessary to support graph of objects where the same object is referenced multiple times. You need to call reset() to ... reset the stream and be able to send the new state of an already sent object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM