Slick Forums

Discuss the Slick 2D Library
It is currently Wed Nov 20, 2019 1:03 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Dec 24, 2012 5:45 am 
Offline

Joined: Mon Dec 10, 2012 1:33 am
Posts: 17
I am working on an RPG with some friends, in which an instance of the Player() class we wrote holds the character's sprite, stats, inventory, etc. If I wanted to make one instance of the player exist in multiple game states, how would I do so (for a StateBasedGame)?

I already tried:
Code:
public class RPGGame extends StateBasedGame{

   public static final int MAIN_MENU_STATE = 0;
   public static final int NEW_GAME_STATE = 1;
   public static final int CONTINUE_STATE = 2;
   public static final int OPTIONS_STATE = 3;
   public static final int CHARACTER_MOVE_STATE = 4;
   
   //Global variables (forgive me programming gods!)
   public Player player;
   public int tileX, tileY;
   
   public RPGGame() {
      super("Menu Test...");
      player = new Player();
      tileX = 35;
      tileY = 35;
   }
   
   
   public static void main(String[] args) throws SlickException {
       AppGameContainer app = new AppGameContainer(new RPGGame());
         app.setDisplayMode(800, 600, false);
         app.setTargetFrameRate(119);
         app.setShowFPS(false);
         app.start();
   }
}


But it gave me errors because the Player is created outside of an init() method, and the SpriteSheet loading (for character walking animations) failed. Any ideas?


Top
 Profile  
 
PostPosted: Mon Dec 24, 2012 11:32 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
You are using StateBasedGame, so create your resources in the init methods of your gamestates,
or perhaps when you first enter a specific state, by overriding the enter() method in the state.
Note that creating resources like images shouldn't be done every time you enter the state, so make a check if the resource is already created or not.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Tue Dec 25, 2012 9:45 pm 
Offline

Joined: Mon Dec 10, 2012 1:33 am
Posts: 17
I understand how to use init(), but I didn't think that stateBasedGame had an init() method. Isn't init() only for gameStates? Regardless, if I recreate my player in the init() or enter() method of all of my states, then my player will only exist within the scope of a single gamestate. We're making a Pokemon-style game where the player walks around until they 'encounter a monster' and then the screen changes to the battle gameState. Should I have character movement and battle in a single gameState or can I actually have a battle() gameState and a characterMove() gameState?

EDIT: I came across the singleton design pattern. Would this be the (or one of the) valid solution(s) to this problem?


Top
 Profile  
 
PostPosted: Tue Dec 25, 2012 10:43 pm 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
Well StateBasedGame doesn't have an init method, But you do have a few options to allow the same player object to be passed around in multiple states.

1. Build you own sub-statemachine in a single slick-gamestate that allows passing around game specific objects, and only use other slick-gamestates for states the don't need much/any access to gamedata.
2. Add code to the existing statemachine that handles your gamedata.
3. use a singleton pattern to store global data like player.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Fri Dec 28, 2012 1:39 am 
Offline

Joined: Mon Dec 10, 2012 1:33 am
Posts: 17
Thank you for the help! The singleton method works perfectly. I've seen you post a lot on these forums, and just wanted to say that it really helps the community a lot!


Top
 Profile  
 
PostPosted: Fri Dec 28, 2012 2:15 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
"Passing data" from state to state came up in another forum, here was my answer:
http://badlogicgames.com/forum/viewtopi ... 663#p31663


Top
 Profile  
 
PostPosted: Sun Dec 30, 2012 1:39 pm 
Offline

Joined: Sun Dec 30, 2012 1:35 pm
Posts: 5
I guess the Player won't be the only object being passed from state to state...
Implementing every object that has to be passed from state to state wouldn't be a good solution.

I'd much rather go with davedes solution.
It's kinda like a DataModel in the Model View Controller, having the Model & Controller in one class though and without the Observer/Observable pattern.

DarXun


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 233 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group