Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Best way of loading player data?  (Read 1493 times)
0 Members and 1 Guest are viewing this topic.
Offline kpars
« Posted 2013-06-04 20:55:16 »

I'm in the middle of making a platformer game, and I'm wondering what the best way of loading/saving levels and player data would be.
Any suggestions? I mainly just need to know what I am theoretically supposed to do.  I've never ran into having to read and load files, most of what I've done have just been 'play-through once and see how long it takes till you die' games, like Mario, for example.  Grin

All help is appreciated.  Wink
Offline Andre Lopes
« Reply #1 - Posted 2013-06-04 21:12:03 »

Hm, ok, to tell you the truth, maybe would be a good idea using Json plus, State Machine Logic.

I made a little drawing for you, took me some time but, i hope it helps Cheesy

Offline Longarmx
« Reply #2 - Posted 2013-06-04 21:34:15 »

You can use an XML parser like SAX or DOM to save attributes of a character. At startup, you go through the file and assign the player variables based on the attributes inside the file.

1  
2  
3  
4  
5  
6  
7  
<player>
<attribute name="health" value="100"></attribute>
<attribute name="mana" value="55"></attribute>
<attribute name="xpos" value="102.3"></attribute>
<attribute name="ypos" value="53.0"></attribute>
...
</player>

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

JGO Knight


Medals: 28
Projects: 3
Exp: 5 years


https://github.com/wreed12345


« Reply #3 - Posted 2013-06-04 21:36:46 »

Hm, ok, to tell you the truth, maybe would be a good idea using Json plus, State Machine Logic.

I made a little drawing for you, took me some time but, i hope it helps Cheesy


Thats pretty much how I do it in my game. Also if you are using libgdx there is a very simple method of doing this, but if not this should be helpful: https://github.com/libgdx/libgdx/blob/master/demos/superjumper/superjumper/src/com/badlogicgames/superjumper/Settings.java

Offline kpars
« Reply #4 - Posted 2013-06-04 21:58:36 »

You can use an XML parser like SAX or DOM to save attributes of a character. At startup, you go through the file and assign the player variables based on the attributes inside the file.

Thank you! I will go try that now. Luckily I have basic knowledge of XML, I should have thought of this before  Grin
Offline poo2thegeek

Innocent Bystander





« Reply #5 - Posted 2013-06-06 19:03:05 »

Just an added extra point onto the previous answer.


If you wish to have a more complicated amount of info (for example, saving a players inventory) then the XML format would be difficult. The way you could do it is to make a class called something like "PlayData" and then save it as an object. Like so:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   public void saveData(PlayData pd) throws Exception{
        //The file it saves to
      File filePath ="saves";
      if(!filePath.exists()){
         //log.info("File path does not exist, creating it...");
         filePath.mkdirs();
      }//log.info("Got file path, attempting to get file...");
      File file = new File("saves/player.data");
      if(!file.exists()){
         file.createNewFile();
      }
      FileOutputStream save = new FileOutputStream(file);
      // Write object with ObjectOutputStream
      ObjectOutputStream saveOut = new ObjectOutputStream (save);
        //Now write it out
      SaveOut.writeObject (pd);
   }


And then to load it:

       public PlayerData getPlayerData() throws Exception{
      File filePath =new File("saves/player.data");
      if(!filePath.exists()){
         return null;
      }
      FileInputStream save = new FileInputStream(filePath);

      // Read object using ObjectInputStream
      ObjectInputStream saveIn = new ObjectInputStream (save);

      // Read an object
      Object obj = saveIn.readObject();

      if (obj instanceof PlayerData){
         return (PlayerData) obj;
      }else{
         return null;
      }
   }

So long as everything inside the PlayerData class (including itself) implements Serializable, then this will work.

Hope I helped!
Offline Longor1996
« Reply #6 - Posted 2013-06-06 19:47:41 »

Just an added extra point onto the previous answer.


If you wish to have a more complicated amount of info (for example, saving a players inventory) then the XML format would be difficult. The way you could do it is to make a class called something like "PlayData" and then save it as an object. Like so:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   public void saveData(PlayData pd) throws Exception{
        //The file it saves to
      File filePath ="saves";
      if(!filePath.exists()){
         //log.info("File path does not exist, creating it...");
         filePath.mkdirs();
      }//log.info("Got file path, attempting to get file...");
      File file = new File("saves/player.data");
      if(!file.exists()){
         file.createNewFile();
      }
      FileOutputStream save = new FileOutputStream(file);
      // Write object with ObjectOutputStream
      ObjectOutputStream saveOut = new ObjectOutputStream (save);
        //Now write it out
      SaveOut.writeObject (pd);
   }


And then to load it:

       public PlayerData getPlayerData() throws Exception{
      File filePath =new File("saves/player.data");
      if(!filePath.exists()){
         return null;
      }
      FileInputStream save = new FileInputStream(filePath);

      // Read object using ObjectInputStream
      ObjectInputStream saveIn = new ObjectInputStream (save);

      // Read an object
      Object obj = saveIn.readObject();

      if (obj instanceof PlayerData){
         return (PlayerData) obj;
      }else{
         return null;
      }
   }

So long as everything inside the PlayerData class (including itself) implements Serializable, then this will work.

Hope I helped!


That is a really bad idea, mostly when the game is in a stage of development where classes change rapidly.
The standard serialization is a really bad idea to use for games, since it can break so fast.

Someone told me there's a work-around for that, so it doesn't break. Sadly, I forgot who/how.

I recommend either XML,Plain-Text,NBT or JSON.

- Longor1996

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (107 views)
2017-09-30 02:57:34

theagentd (134 views)
2017-09-26 18:23:31

cybrmynd (244 views)
2017-08-02 12:28:51

cybrmynd (237 views)
2017-08-02 12:19:43

cybrmynd (237 views)
2017-08-02 12:18:09

Sralse (251 views)
2017-07-25 17:13:48

Archive (863 views)
2017-04-27 17:45:51

buddyBro (1006 views)
2017-04-05 03:38:00

CopyableCougar4 (1565 views)
2017-03-24 15:39:42

theagentd (1372 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!