Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  Only one JAR file and a savegame feature. Posible?  (Read 2920 times)
0 Members and 1 Guest are viewing this topic.
Offline H3rnst

Senior Newbie


Medals: 1



« Posted 2012-04-14 02:46:05 »

That's what I've been asking myself since I read the criteria for selecting featured games. I'm talking about this, specifically:
Quote
Installation: +1 point if the game installs and uninstalls correctly

I wanted to add achivements to my game (like the ones in PS3/XBox games) and I thought about writing a text file and save the progress of the player and the amount of trophies earned in that file. But the thing is that that file would be created in the same directory as the .jar (or any other folder) and if you wish to delete the .jar then the savegame file will remain there (duh) unless you delete it as well. Then I thought that I could simply create a file or modify an existing one within the .jar, and update it while in game. A quick google search on how to do that made me realize that it was too difficult/impossible to do.

I could also use an installer and then the uninstall would take care of the problem of deleting the .jar and the savegame files for the user, but the question remains the same:

Is it possible to create a savegame feature in a game deployed trough an executable .jar while not creating extra files for that purpose?
Thanks!

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-04-14 03:32:56 »

You could write in the Users's home directory. System.getProperty("user.home") returns the path to the home folder, where you can create a folder for the game and write stuff there.

Offline H3rnst

Senior Newbie


Medals: 1



« Reply #2 - Posted 2012-04-14 04:54:52 »

But, wouldn't you have to manually delete the game folder after deleting the .jar?  Huh

Perhaps I didn't explain very well what my question was (not being a native English speaker doesn't help either). Basically what I'm asking is if there's a way to save the state of a game packaged in an executable .jar without creating any other aditional files. And of course, without uploading the data to a server or something like that.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-04-14 05:39:26 »

In Android you can save on Bundle. Saving state on desktop absolutely need another file with simple access.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2012-04-14 05:55:19 »

Nope, there is no way you can save into the JAR file, because the file will be in use so you can't modify it while the JVM is using it.

Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #5 - Posted 2012-04-14 08:06:33 »

If you look at how a lot of apps work, they use something in the workspace like: /.minecraft

If you save it to there, you can easily set up a savegame feature and still have updates.
Offline Z-Man
« Reply #6 - Posted 2012-04-15 02:34:06 »

On the desktop there isn't a way to delete saved files without having an uninstaller. I use this to figure out where to save to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static String getDataFolder()
{
   String folder = System.getProperty("user.home");
   String os = System.getProperty("os.name").toLowerCase();
   
   if(os.contains("win"))
   {
      if(os.contains("xp"))
         folder += "\\Application Data\\";
      else if(os.contains("7") || os.contains("vista"))
         folder += "\\AppData\\Roaming\\";
   }
   else if(os.contains("mac"))
      folder += "/Library/Application Support/";
   
   return folder;
}
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2012-04-15 03:05:52 »

Don't forget to put a '.' before the folder name so it would be invisible on Linux so if your game was to be named MyAwesomeGame, the folder would be '.myawesomegame'

Offline Z-Man
« Reply #8 - Posted 2012-04-15 05:08:49 »

Don't forget to put a '.' before the folder name so it would be invisible on Linux so if your game was to be named MyAwesomeGame, the folder would be '.myawesomegame'
The '.' works on UNIX-based systems, so it will work on Mac OS X as well. So I'd just do getDataFolder() + ".gamename". Also, you can hide the folder on windows using this (this method will also work for UNIX based systems):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public static void hideFile(File src)
{
   if(System.getProperty("os.name").toLowerCase().contains("win"))
   {
      Process p;
      try
      {
         p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
         p.waitFor();
      }catch(IOException exc1)
      {
         exc1.printStackTrace();
      }
      catch(InterruptedException exc2)
      {
         exc2.printStackTrace();
      }
   }
   else if(!src.getName().startsWith(".")) // If the file is already hidden to UNIX systems don't add another dot
     src.renameTo(new File(src.getParentFile().getPath() + "/." + src.getName()));
}
Offline gouessej

« In padded room »



TUER


« Reply #9 - Posted 2012-04-15 10:31:58 »

Hi

Using only one JAR file as an "executable" file is a bad idea, I already explained it several times on this forum. Unfortunately, on lots of machines, the OS will try to open your JAR with an archiver, it won't run your game when double-clicking on the JAR. For example, Winzip and WinRAR will do that by default under Windows, Ark will do the same under some GNU Linux distros. I advise you to use another solution like Java Web Start (very easy, allows you to use your JAR but a bit buggy), IzPack (very complete, very professional) or GetDown.

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

JGO Coder


Medals: 18



« Reply #10 - Posted 2012-04-15 17:47:32 »

On the desktop there isn't a way to delete saved files without having an uninstaller. I use this to figure out where to save to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static String getDataFolder()
{
   String folder = System.getProperty("user.home");
   String os = System.getProperty("os.name").toLowerCase();
   
   if(os.contains("win"))
   {
      if(os.contains("xp"))
         folder += "\\Application Data\\";
      else if(os.contains("7") || os.contains("vista"))
         folder += "\\AppData\\Roaming\\";
   }
   else if(os.contains("mac"))
      folder += "/Library/Application Support/";
   
   return folder;
}


On Windows, just always use the value of the %APPDATA% environment variable, rather than hard-coding what the path will be depending what Windows variant you're on.

1  
2  
3  
4  
if (os.contains("win")) {
   return System.getenv("APPDATA");
}
else ...

Offline H3rnst

Senior Newbie


Medals: 1



« Reply #11 - Posted 2012-04-15 19:26:13 »

Using only one JAR file as an "executable" file is a bad idea

I was thinking about using Launch4j and create an .exe file from the .jar (that way I can also create an icon for the app Tongue )

Ok, so there's no way to implement a savegame feature without creating extra files. Thanks everyone for the code snippets and advices  Grin

Offline ReBirth
« Reply #12 - Posted 2012-04-16 01:34:25 »

Or you can create a bat file in same folder as your jar
1  
2  
@echo off
java -jar syalala.jar

However I found some cases where Java not configured on win's Path. Duh!

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.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (82 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!