Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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 874 times)
0 Members and 1 Guest are viewing this topic.
Online Rayexar
« Posted 2013-09-01 04:01:33 »

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?
Offline BurntPizza
« 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!
Online 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
1  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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
« 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().
Online 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

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

It works on all systems and android too.

Basically create a


save
1  
2  
3  
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
1  
2  
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 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
« 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (74 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!