Table of Contents

Save/load static variables

This tutorial covers how to save/load static parameters in your projects.

Create project

In a newRGG project, add a new file called “paramfile”. (in the file explorer click “new>file>”, enter paramfile - no extensions).

In the Model.rgg file copy:

import java.io.Writer;
import java.io.Reader;
 
 
import de.grogra.pf.ui.registry.RunAfterSave;
import de.grogra.pf.ui.registry.RunAfterLoad;
import de.grogra.pf.registry.RegistryContext;
 
 
global float myFloat = 1f;
public static int myInt = 3;
public static String myString = "initValue";
 
public class LoadParam implements RunAfterLoad{
	public void load(RegistryContext ctx){
		Object f = getFileFromProject("paramfile");
		Reader r = ctx.getRegistry().getFileSystem().getReader(f);
		char[] buf = new char[2048];
		r.read(buf);
		String alltext = new String (buf);
		String[] lines = alltext.split("\n");
		for (String line : lines){
			String[] param = line.split("=");
			if ("myInt".contentEquals(param[0])) {
				myInt = Integer.valueOf(param[1]);
			} else 
			if ("myString".contentEquals(param[0])){
				myString = param[1];
			}
		}
	}
}
 
public class SaveParam implements RunAfterSave{
	public void save(RegistryContext ctx){
		Object f = getFileFromProject("paramfile");
		Writer w = ctx.getRegistry().getFileSystem().getWriter(f, false);
		w.append("myInt="+myInt+"\n");
		w.append("myString="+myString+"\n");
		w.close();
	}
}
 
protected void init ()
[
	Axiom ==> ;
]
public void r(){
	myInt ++;
	myString = "anotherValue";
        myFloat++;
}
 
public void p ()
{
println(myInt);
println(myString);
println(myFloat);
}

Note:

You can run the r() command to change the value of the variables. Save the project, close it, re open it, and run p() to print their current value.

With the plugin PropFile

The PropFile plugin includes some methods to:

Example with the previous file:

import de.grogra.pf.ui.registry.RunAfterSave;
import de.grogra.pf.ui.registry.RunAfterLoad;
import de.grogra.pf.registry.RegistryContext;
import de.grogra.propFile.TypedTextPropertyFile;
 
public static int myInt = 3;
public static String myString = "initValue";
 
static int myInt = 5;
static String myString = " ";
 
public class LoadParam implements RunAfterLoad{
	public void load(RegistryContext ctx){
 
		TypedTextPropertyFile props = TypedTextPropertyFile.fromProjectFile("paramfile", ctx);
		props.load();
		myInt = props.get("myInt", myInt);
		myString = props.get("myString", myString);
 
	} 
} 
 
public class SaveParam implements RunAfterSave{
	public void save(RegistryContext ctx){ 
 
		TypedTextPropertyFile props = TypedTextPropertyFile.fromProjectFile("paramfile", ctx);
		props.clear();
 
		props.set("myInt", myInt);
		props.set("myString", myString);
 
		props.save();
 
	}
}