Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Kryonet - Passing players  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-12-03 11:21:29 »

Hello!

I have an ArrayList<Player> 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<Player>.

How can i get the ArrayList to the clients? :-)


Trying just to pass it with the constructor gives this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
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);
    }
}
Offline Phased
« Reply #1 - Posted 2012-12-03 11:27:15 »

You just recreated the same topic for the same problem that I believe should be fixed.

Packet class
1  
2  
3  
public class PlayerPacket(){
public ArrayList<Player> otherPlayers;
}



How your going to be sending the packet from the server to the client? im guessing?

1  
2  
3  
4  
5  
6  
PlayerPacket packet = new PlayerPacket();

//where players is the arraylist of players that the server contains.
packet.otherPlayers = players;

server.sendToAllTCP(packet);


You could of posted in your old topic, did not work, or better explanation instead of creating a whole new topic for the same question by the same person.

This is how I send packets through my server. the PlayerPacket should be pretty much the only code needed in the class.

edit: commented one line of code.

edit 2:
The only problem I had with ArrayLists for entities to be sent the size that is being sent can easily be to big, Im not sure if their is away to allow for bigger packet sizes.
Offline Reck

Senior Newbie





« Reply #2 - Posted 2012-12-03 11:32:51 »

That's what i'm doing already, and that's what's causing the error.

Packet class:
1  
2  
3  
        static public class PlayerList {
            public ArrayList<Player> playerList;
        }


Server sending the packet:
1  
2  
3  
                        PlayerList toClient = new PlayerList();
                        toClient.playerList = playerList;
                        server.sendToAllTCP(toClient);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phased
« Reply #3 - Posted 2012-12-03 11:45:08 »

Hmm ok, Im getting the same thing for ArrayLists now.

I guess its definitely pass what I know, so I will be reading to find out how this is fixed. I have an Idea but I think it may be unecessary to recreate every entity via the client, and only sending required info to the client to create the entity (like username/id, x and y pos etc) 
Offline saifix

Senior Newbie


Medals: 1



« Reply #4 - Posted 2012-12-03 14:50:35 »

I'm not very familiar with "Kryonet" but it seems like it assumes a no args constructor exists since there's no way for the library itself to distinguish parameters from each other (x, y, etc., conflicting types with no runtime information other than their order in the constructor.) You should add a default constructor to the class and see if that solves your problem.

1  
2  
3  
public Player() {
    this("name", 0, 0, "img.png");
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!