Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
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  
  How to Load Player Save Data?  (Read 1530 times)
0 Members and 1 Guest are viewing this topic.
Offline Rayexar
« Posted 2013-09-01 04:01:33 »


I've been trying to work out how I can load player save data from a text file and apply it to the current game state. I don't mean how to read data from the text file, but how to change the game variables to the values in the file.

I have a Play screen which contains the Map and Player, but the properties can vary. For example, which map the player is currently in and what level the player is. I would like to be able to use a class called StatLoader or something like that to read the data and load the variables, but I'm not sure how to design it.

I can make the Play screen, Map, Player and methods in StatLoader static, but I heard this is not good design.

Can anyone give some suggestions?
Offline BurntPizza

« JGO Bitwise Duke »

Medals: 449
Exp: 6 years

« Reply #1 - Posted 2013-09-01 04:46:46 »

Usually people have a GameState class or similar, this keeps track of all such variables in one place, and allows for easy saving and loading of game data. Such a class might have:
  • Player location
  • Current map
  • Score
  • Current difficulty level
  • etc, including anything that is setup during game initialization that needs to be kept track of.

Then either write a encoder/decoder for saving the information to file, or do as Cas does and serialize the class. Note that Java serialization will break saved games if you change the GameState class, so I recommend using a custom method!
Offline Rayexar
« Reply #2 - Posted 2013-09-01 05:08:41 »

Thanks for that, but now I'm stuck on something that seems fairly simple. I get a nullpointer exception using Libgdx external file handling on the line
boolean exists = Gdx.files.external("testing.txt").exists();
which gives the exception

Exception in thread "main" java.lang.NullPointerException
   at game.GameStatsLoader.loadWorldData(

This is the class:
package game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

import screens.Gameplay;

public class GameStatsLoader {
   public void loadWorldData() {
      boolean exists = Gdx.files.external("testing.txt").exists();

I know the file exists, and I'm testing using the Desktop version. Any ideas?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »

Medals: 449
Exp: 6 years

« Reply #3 - Posted 2013-09-01 05:21:05 »

I'm assuming you've read it, but just in case: File Handling LibGDX, tells differences between internal, external, etc.

That exception means either the Gdx.files is null, which can happen if you are trying to access it from a static context, or before the application's create() method has been called (e.g. in the main(String[] a) method, or the returned FileHandle is null, which shouldn't happen, as I believe a GdxRuntimeException is thrown if the file doesn't exist.

So check or tell me where you are calling loadWorldData().
Offline Rayexar
« Reply #4 - Posted 2013-09-01 07:01:36 »

Hi, I solved the problem after reading your reply. I was calling loadWorldData() in the constructor of the Play screen. I moved it into the show() method of the Screen and it worked.

How do you create a file with Libgdx?
Offline namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

« Reply #5 - Posted 2013-09-01 15:06:16 »

If you are using LibGDX as it looks like you are.
There is a really easy alternative

It works on all systems and android too.

Basically create a

Preferences prefs ="your_game_name");
prefs.putInteger("level", 4); //  save the current level is on 'level 4' obviously replace 4 with your saving intVar

Then whenever you load up the game do
Preferences prefs ="your_game_name");
currentLevel = prefs.getInteger("level");

and then you can load the currentLevel.  Thats literally it. Its extremely easy and fully functioning without dealing with file locations saves/loads

note, if you are on windows, it saves/loads from C:\Users\%user%\.prefs    If you go there, I found several other games were already saving their load/save files there Cheesy

The downside is that you can only work with primitives and strings.
You can always keep it simple, like I think you want with,  playerLocation(int x/y),  currentMap(int), score(int), currentdifficulty(int), currnetMusicVolume(int)

For anything that is more complicated, you could write something that converts it into some string that you can decode later.

"Experience is what you get when you did not get what you wanted"
Offline BurntPizza

« JGO Bitwise Duke »

Medals: 449
Exp: 6 years

« Reply #6 - Posted 2013-09-01 15:06:58 »

How do you create a file with Libgdx?

Read the link I provided, it shows how to create and write files.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (14 views)
2015-10-07 02:11:23

KaiHH (13 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (23 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 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‑
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!