Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Tower Defense Save File  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-04-27 22:31:25 »

Alright, this is a biggie for me. I need to create a save file that writes the data of all the blocks in the game to the save. The blocks are like this: airBlocks can contain towers which also have upgrades. If the airBlock doesn't contain a tower its value is 0 (Value.airAir) if it's a tower, than it is another int (Value.airTower"insert-tower-name-here"). So what's the best way to write all the values of the current blocks including tower upgrades to the file?
Thanks,
-cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Phyyl

Senior Newbie





« Reply #1 - Posted 2013-04-28 14:22:11 »

A good way to save things to files is to create two functions on every items you want to save. One for creating a byte[] of the values (exporting its values), and another one for decoding an byte[] (importing its values).

Example :
1  
2  
3  
4  
5  
6  
7  
8  
public byte[] getBytes() {
    return this.name.getBytes();
}

public void fromBytes() {
    this.name = new String(bytes, "iso-8859-1"); // remplacer l'encodage au besoin

}


it's a very abstract example but you're probably going to need to add lists or arrays with a correct size for storing your data.
Offline Slyth2727
« Reply #2 - Posted 2013-04-28 14:40:36 »

I'm afraid I don't fully understand what you mean here. Would I put this method everywhere, or in one place and just call it tons of times every auto-save? In all honesty I have no CLUE what I have to do for this, so you may have to walk me through this if you don't mind

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phyyl

Senior Newbie





« Reply #3 - Posted 2013-04-28 14:48:11 »

Let's say you have an Object(class) called "Player" and this player has value(String name, int life).

You need to make an array of bytes to save to the file. The best way is to make an interface (let's say we call it "ISavable").

this interface forces you to implement the two methods shown previously. when you save your game, you itterate over all your game elements that are "ISavable" and you append the byte array of each object to the file. when you load the game, it's gonna go over all the file byte by byte and put those values in the right place using the fromBytes(byte[] b) method.

hope this helps
Offline Slyth2727
« Reply #4 - Posted 2013-04-28 14:52:54 »

Alright, makes sense. Way less code than I was thinking of doing originally. And I'm guessing that this willl work for a Tower Defense game that is block based? And probably that the fact it is block based will make it easier?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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