Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
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  
  [States] [Networking] [slick2d] Game state is being initialized when not wanted  (Read 3517 times)
0 Members and 1 Guest are viewing this topic.
Offline mana

Senior Newbie





« Posted 2012-06-13 06:48:50 »

Seems like my game state is being initialized even though I have it commented out in the initStatesList(...) is there a reason for that? I have it initializing network code would that affect the initializing?

the exact error im getting is:
Quote
Unable to get I/O connection to: localhost on port: 1967
Exception in thread "main" java.lang.NullPointerException
   at GameplayState.init(GameplayState.java:136)
   at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
   at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
   at CodaClient.main(CodaClient.java:33)

but the only connection attempt to localhost is in the game state

Heres my initStatesList:
1  
2  
3  
4  
5  
6  
public void initStatesList(GameContainer gameContainer) throws SlickException {
        this.getState(MAINMENUSTATE).init(gameContainer, this);
        //this.getState(GAMEPLAYSTATE).init(gameContainer, this);
       this.getState(VICTORYSTATE).init(gameContainer, this);
        this.getState(OPTIONSSTATE).init(gameContainer, this);
}


and the states added to the state list:
1  
2  
3  
4  
5  
6  
7  
8  
super("Title");

this.addState(new MainMenuState(MAINMENUSTATE));
this.addState(new GameplayState(GAMEPLAYSTATE));
this.addState(new VictoryState(VICTORYSTATE));
this.addState(new OptionsState(OPTIONSSTATE));

this.enterState(MAINMENUSTATE);


Complete Source Code:
CodaClient.java: http://pastebin.com/SvGnbVXr
MainMenuState.java: http://pastebin.com/W8bBDbn6
GameplayState.java: http://pastebin.com/rXm0kkik
OptionsState.java: http://pastebin.com/wStTBq0L
VictoryState.java: http://pastebin.com/gvUpGJf9

Based on slickBox Tutorial: http://slick.cokeandcode.com/wiki/doku.php?id=02_-_slickblocks

Edit: couldn't get the JGO files to work so I just used pastebin
Offline ReBirth
« Reply #1 - Posted 2012-06-13 06:54:01 »

Where is the code?

Offline mana

Senior Newbie





« Reply #2 - Posted 2012-06-13 06:54:33 »

sorry forgot to put it in, it's there now. Is there anything else that I should include to make this easier? like the gameState init()?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-06-13 07:02:28 »

The exception is pretty clear. Look at GameplayState.java line 136. I can't see what it is since it's not full source*.

*) if you want to post more source and they're huge, use JGO's pastebin.

Offline mana

Senior Newbie





« Reply #4 - Posted 2012-06-13 07:06:35 »

I'll post the full source, but the issue is that it's even running that code, I don't need that code until it has the data that I declare earlier in the program. Basically in the menus I get the user to login, then it sends those credentials to the server in the GameState. But on Launch the GameState init() is being run when I dont want to call it until I need it.

Ill post full source code once I copy it to the bin, but would there be a better way to approach something like this?
Offline ReBirth
« Reply #5 - Posted 2012-06-13 07:13:19 »

How about override init() method so it'll only execute when a boolean is true?

Offline mana

Senior Newbie





« Reply #6 - Posted 2012-06-13 07:19:32 »

Full source is uploaded, and I could try that, but that sounds like a messy workaround, considering that the initStatesList() is meant as a list to declare what to init (or so I thought) and basically SHOULD do the same thing. I probably just messed up something in my declarations that I didn't notice cause I am quite new to the Slick2D lib
Offline ReBirth
« Reply #7 - Posted 2012-06-13 07:34:33 »

You shouldn't put it in GamePlay's init() method. All code inside that method should be ready to execute on earlier. Your login method should stay on MainMenu state, and if success you can change to game state. The flow is kinda like this:

1. All states added and inited, done before user see the menu.
2. All states are ready and sit to wait. MainMenu comes first.
3. User try to log in. The process is done before changing state. So if success, change it Game state otherwise keep on there.

Offline mana

Senior Newbie





« Reply #8 - Posted 2012-06-13 07:36:19 »

Alright ill give that a shot, thank you
Offline davedes
« Reply #9 - Posted 2012-06-13 11:22:52 »

Ugh... Cranky The tutorial is wrong (it's been updated now).

You should add states in initStatesList. After initStatesList is called, StateBasedGame will initialize all of them for you (so you shouldn't call state.init yourself!! otherwise they will be initialized twice). This means all of your states will be "initialized" at the beginning of your game.

If you don't want this, you can either add them to your game and manually initialize them at some point after initStatesList... Or (preferred method) you can handle the initialization differently: subclass GameState and StateBasedGame and set up your own "activated" and "deactivated" methods that define when the state is first enetered, and then left. This way you can lazily load and destroy resources whenever the user enters/leaves states.

So, proper state initialization looks like this:
1  
2  
3  
4  
public void initStatesList(GameContainer gameContainer) throws SlickException {
        this.addState(new MainMenuState(MAINMENUSTATE));
        this.addState(new GameplayState(GAMEPLAYSTATE));
}


And your constructor shouldn't have anything related to states:
1  
super("Title");


By default, the first time you add a state in initStatesList it will be 'entered' (in this case main menu). So you could still use enterState after adding states, if you want to start with a different state.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mana

Senior Newbie





« Reply #10 - Posted 2012-06-13 15:25:13 »

If I try it that way it throws an error about not having a state with an ID of 0, so they are not initialized at all (I'm assuming), even if I leave the
1  
2  
this.getState(MAINMENUSTATE).init(gameContainer, this);
this.getState(GAMEPLAYSTATE).init(gameContainer, this);

sections in, once the .addstate() code is moved it doesnt work.
Offline mana

Senior Newbie





« Reply #11 - Posted 2012-06-13 15:53:54 »

And Rebirth, the change to the way I handle the connection info works wonderfully, thank you. But I needed to keep it in the GameState, But I adjusted based on what you said
Offline davedes
« Reply #12 - Posted 2012-06-13 23:10:00 »

1  
If I try it that way it throws an error about not having a state with an ID of 0, so they are not initialized at all (I'm assuming)

Post the exception please. This will happen if you try entering a state before you've added it via addState.

I can assure you that calling init() manually like you are doing is not the correct usage... Cranky And it will lead to the state being initialized twice. (i.e. all images/sound will be loaded more times than necessary)

Offline mana

Senior Newbie





« Reply #13 - Posted 2012-06-13 23:36:57 »

Exception in thread "main" java.lang.RuntimeException: No game state registered with the ID: 0
   at org.newdawn.slick.state.StateBasedGame.enterState(StateBasedGame.java:153)
   at org.newdawn.slick.state.StateBasedGame.enterState(StateBasedGame.java:131)
   at CodaClient.<init>(CodaClient.java:22)
   at CodaClient.main(CodaClient.java:27)
Offline davedes
« Reply #14 - Posted 2012-06-13 23:54:48 »

Are you calling enterState before adding the states to your game? persecutioncomplex

Offline mana

Senior Newbie





« Reply #15 - Posted 2012-06-14 02:43:02 »

I totally am, I'll adjust it. Sorry I've been programming this non-stop since Monday, think I'm getting sloppy.
Offline mana

Senior Newbie





« Reply #16 - Posted 2012-06-14 02:48:10 »

Ya that fixed it, thank you.
Offline ReBirth
« Reply #17 - Posted 2012-06-14 16:41:06 »

Great that work for you.

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.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!