1

I'm developing a game in LibGDX and I have a static int named TILE_ID that is assigned to every tile everytime a tile is created, like this:

{loop in Level.class}

Tile t = null;

            switch (material) {
                case HOME: {
                    t = new Home(x, y);
                    break;
                }
                case WALL_IRON:
                case WALL_BRICK: {
                    t = new Tile(x, y, material, type);
                    break;
                }
            }

            if (t != null) {
                if (t instanceof Home) {
                    this.home = t;
                }
                t.tileID = TILE_ID++;
                tileArray.add(t);
            }

{loop-end in Level.class}

So when I start a new game, the first instance says:

private final Listener clientListener = new Listener() {
    @Override
    public void connected(Connection connection) {
        PacketEntity p = (PacketEntity) Packet.createPacket(
                PacketEntity.class, Packet.TYPE.CONNECTED);
        p.name = pTank.getName();
        connection.sendTCP(p);

        System.out.println("MAX TILES: " + Level.TILE_ID);
        System.out.println("connected to server!");
    }

The output is:

:desktop:run
new manager
disposed main menu!
Initializing local server...
Client connected! ID# 1
MAX TILES: 76
connected to server!

And then I run a second instance, connect to the localhost server and the output is:

:desktop:run
new manager
disposed main menu!
A server is already running on localhost. Trying to connect....
MAX TILES: 152
connected to server!

I don't understand why this happens. If I connect a third client and so on, the max tiles is always 152.

The only place the tileid increases is inside the Level.class which is local to every player (not sent over the network).

Can someone help me out with this?

Jh62
  • 324
  • 1
  • 3
  • 15
  • 1
    Have you tried to debug the code? From the (rather sparse) information given it looks like the "Level-Creation-Loop" might be executed twice (`152 = 2 * 76`) if a second instance is started. – dpr Jul 13 '16 at 09:49
  • 1
    This was a bit confusing to follow because all caps field names are typically used only for `final` static fields. – Tenfour04 Jul 13 '16 at 12:29
  • Thanks. The correct answer was by dpr. I had a bug in the code that a new server instance would be created even if you were connecting to another server. – Jh62 Jul 14 '16 at 09:12

1 Answers1

1

You should to set Level.TILE_ID = 0 before increasing them in new process instance - the problem is that your are starting new process with TILE_ID already setup as with value of 76 so the new process is increasing the variable setting

    76 + 76 = 152

It is always 152 because you are staring new clients from the first one if you would start it from second it would be

    152 + 76 = 228

and so on

m.antkowicz
  • 13,268
  • 18
  • 37
  • 2
    If he reset the TILE_ID to zero then why he need to set it as a static property, I think he may need to set this class in the client scoop not in the server scoop – Anas EL KORCHI Jul 13 '16 at 09:52
  • it depends on how is he using the variable inside client's app (maybe he needs static to access this from outer the class) – m.antkowicz Jul 13 '16 at 10:00
  • Thanks. Ill try this tonight. Maybe is because i run several instantes from the IDE. Ill try running the compiled code also to see if this continues to happen. – Jh62 Jul 13 '16 at 21:05