How to Load Player Save Data?

Hi JGO,

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?

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!

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(GameStatsLoader.java:11)

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?

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().

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?

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

http://www.badlogicgames.com/wordpress/?p=1585

It works on all systems and android too.

Basically create a

save

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

Then whenever you load up the game do

Preferences prefs = Gdx.app.getPreferences("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 :smiley:

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.

Read the link I provided, it shows how to create and write files.