Hello!
I have an ArrayList on my gameserver. I need to transfer this to all clients, to show the players and set their locations.
However, as far as i’ve been able to read, Kryonet doesn’t accept objects with constructors (Player obviously have that) so i can’t just send the ArrayList.
How can i get the ArrayList to the clients?
Trying just to pass it with the constructor gives this:
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:239)
at com.esotericsoftware.kryonet.Client.run(Client.java:317)
at java.lang.Thread.run(Thread.java:680)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): NFT.Player
Serialization trace:
playerList (NFT.Network$PlayerList)
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1048)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1060)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:228)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:217)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:654)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:57)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
And my Player class:
public class Player {
private int playerId;
private String name;
private int playerX;
private int playerY;
private String figure;
public Player(String name, int positionX, int positionY, int playerId) {
this.name = name;
this.playerX = positionX;
this.playerY = positionY;
figure = "NFT/player.png";
//setRandomPosition();
}
public int getX() {
return playerX;
}
public int getY() {
return playerY;
}
public void setPosition(int x, int y) {
this.playerX = x;
this.playerY = y;
}
public String getFigure() {
return figure;
}
public int getId() {
return playerId;
}
public String getName() {
return this.name;
}
public void setRandomPosition() {
int randomX = 5 + (int)(Math.random() * ((768 - 5) + 1));
int randomY = 5 + (int)(Math.random() * ((512 - 5) + 1));
int newX = Math.round(randomX / 50) * 50 + 25;
int newY = Math.round(randomY / 50) * 50 + 25;
int tileCoordY = newY - (newY % 50);
int finalX;
int finalY = newY;
if (tileCoordY % 2 == 1) {
finalX = newX + 25;
}
else {
finalX = newX;
}
setPosition(finalX, finalY);
}
}