How to save data to plain text?

I have objects moving inside a cube, but I want to save the Vector position of each object to a plain text , for future replays, once every second. Can somebody tell me how I can accomplish this…



Thanks in advanced!!

Just use java preferences or spatial UserData as the manual suggests.

1 Like

I was trying to use the following code to save the vector position of my objects:



private void saveGame(){

try {

PrintWriter outFile;

outFile = new PrintWriter(new FileWriter(“saveGame.txt”, true));

BufferedWriter out = new BufferedWriter(outFile);

out.write(String.valueOf(sph1.getLocalTranslation().x)+ “,”

  • String.valueOf(sph1.getLocalTranslation().y)+ “,”
  • String.valueOf(sph1.getLocalTranslation().z) + “n”);



    }catch (Exception ex){

    System.err.println("An error occured while saving: " +

    ex.getMessage());

    }



    }

    But when I call the method in the updateLoop(), it doesn’t write anything to the “saveGame.txt”

You never close the file… so the buffered writer never gets flushed.



Edit: I mean you never close the BufferedWriter.

1 Like

For those new to Java, here is the basic anatomy of writing a file:



[java]

Writer out = … create some writer

try {

writer some stuff to out

} finally {

out.close();

}

[/java]

1 Like

If you are calling THAT method from the update loop, you will be recreating the PrintWriter object each time your app loops, which could be up to 2000 times per second.



Create the PrintWriter object in your initialize method and only call .write(stringData) in the update loop. But even then you don’t want it writing 2000 times a second, coz you’ll end up with Megabyte size texts real quick.



Set an int variable to count up to one second by adding fps to it from zero, once it is greater than one, do a .write(stringData) and reset the int to zero again.



Before you close your app, you will need to close your PrintWriter. This will flush and close the file buffer. You’ll probably have to set up a key for this … or make a new method call for the escape key.

1 Like

I tried the following code to save my game once every second but it only saves the position of my objects once and then generates an error:

static boolean timer = true;

static Thread thread;

public void simpleUpdate(float tpf){



if(timer = true){

try {

thread = new Thread(new TimeControl());

thread.start();

timer = false;

thread.wait(1000000000);//wait one second

timer = true://update timer control



} catch (InterruptedException ex) {

Logger.getLogger(SphereSimulator.class.getName()).log(Level.SEVERE, null, ex);

}



The TimeControl() is an inner class that has the run() method which calls the method

to save my game.



And this is the error that I get when I run the game:

Jun 22, 2012 11:05:22 PM com.jme3.app.Application handleError

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.IllegalMonitorStateException

at java.lang.Object.wait(Native Method)

at mySimulator.SphereSimulator.simpleUpdate(SphereSimulator.java:299)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:244)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:662)

Jun 22, 2012 11:05:22 PM com.jme3.renderer.lwjgl.LwjglRenderer cleanup

INFO: Deleting objects and invalidating state

Jun 22, 2012 11:05:22 PM com.jme3.scene.Node detachChildAt

INFO: Gui Node (Node): Child removed.

Jun 22, 2012 11:05:22 PM com.jme3.scene.Node detachChildAt

INFO: Gui Node (Node): Child removed.

Jun 22, 2012 11:05:22 PM com.jme3.input.lwjgl.LwjglMouseInput destroy

INFO: Mouse destroyed.

Jun 22, 2012 11:05:22 PM com.jme3.input.lwjgl.LwjglKeyInput destroy

INFO: Keyboard destroyed.

Jun 22, 2012 11:05:22 PM com.jme3.system.lwjgl.LwjglAbstractDisplay deinitInThread

INFO: Display destroyed.

BUILD SUCCESSFUL (total time: 4 seconds)

  1. You completely block the render thread with your wait()
  2. wait() != sleep() anyway
  3. single-argument wait() takes milliseconds not nanoseconds… so you are actually trying to wait one million seconds.



    …but you shouldn’t be waiting anyway. Just add tpf to a counter. When the counter goes above 1 then write your file and reset the counter to 0.
1 Like

Also, for the record, the reason you get an exception is because you did wait() without synchronizing. I only mention that for educational value because wait() is not at all what you want in this situation anyway.



Edit: never mind the fact that you are starting a thread each time simpleUpdate() is called. You may want to do some Java tutorials before attempting to write a 3D game in Java. Learning a language is tough enough without trying to learn 3D game programming also.

1 Like

Thanks to every body for your help, I am able to make it work now.