Unsure On How To Change Slick States w/ Kryonet

Hello and thank you for taking the time to read my post.

In any of my State classes I simply use the following code to change to any of my desired game states.


/////////////////////////////////////////////////////////////////////////////////////
// FUNCTION: update()
//
// INFO: Overrides a method in BasicGameState.
public void update(GameContainer gc, StateBasedGame sbg, int delta) {

        if (case) 
        {
                sbg.enterState(x);
        }
}

The issue arises for me when I’m trying to enter a new state based on a packet I have received from my server. For example, say I want a client to send login details to the server and if it those details are accepted, then the client will advance to the next state.

I figured this must be handled somewhere in my NetworkListener class (which is the listener object that is added to my Kryonet Client object) … but I don’t know how to make the “Slick World” hold hands with the “Kryonet world”.


/////////////////////////////////////////////////////////////////////////////////////
// FUNCTION: received()
//
// INFO: Inherits from class Listener.
//       Called when an object has been received from the remote end of the connection. 
//       This will be invoked on the same thread as Client#update(int) and Server#update(int).
//       This method should not block for long periods as other network activity will not be
//       processed until it returns.
public void received(Connection con, Object obj) {

		if (obj instanceof Packet.Packet1LoginAnswer) {
			boolean answer = ((Packet.Packet1LoginAnswer) obj).isAccepted();
			
                if (answer)
                /* insert code here to enter state 2 */
                
}

Thank you! :slight_smile:

Create your kryonet client in your slick main class for the game. Connect it there and add the listeners. For each of the screen classes pass this original client object in as a parameter so you can share this object without having to reconnect every time. In your received method just add


public void received(Connection con, Object obj) {

      if (obj instanceof Packet.Packet1LoginAnswer) {
         boolean answer = ((Packet.Packet1LoginAnswer) obj).isAccepted();
         
                if (answer)
+                sbg.enterState(state to enter);
                
}

Where is the sbg object coming from if I am in my client Listener class?

My architecture is as follows:
ClientStart class creates a NetworkListener object and adds it to the Kryonet Client client object.
ClientStart also has my main function which sets up my Slick environment and initializes my first state.

I then have a bunch of states that can easily access each other by using sbg.enterState(state number), and they each have a local Client object that is initialized with the one from ClientStart. I can use that Client object to send packets to the server easily with client.sendTCP(obj), however I don’t have any stored variable (to my knowledge) in my program that is in-fact a StateBasedGame. I just have a bunch of functions that have a StateBasedGame sbg argument that I’m using and magic happens.

My state classes are extending BasicGameState and have no link to the NetworkListener, however as I said each class has the initialized Client client object (but I don’t really see how useful that is as I’m trying to make use of the received function… which just feels out of reach).

Once again, thank you. :slight_smile:

I was able to figure this out.

As you said, I just needed to make a copy of the client object in my network listener and use that object. It was one of those times where I’ve spent too much time starring at it and needed a fresh look!

Thank you. :slight_smile: