User Tools

Site Tools


tutorials:first-steps:save-load

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorials:first-steps:save-load [2025/03/25 11:53] – created gaetantutorials:first-steps:save-load [2025/05/26 23:56] (current) barley1965
Line 1: Line 1:
 ====== Save/load static variables ====== ====== Save/load static variables ======
  
-This tutorial cover how to save/load static parameters in your projects.+This tutorial covers how to save/load static parameters in your projects.
  
 ===== Create project ===== ===== Create project =====
  
-In a ''newRGG'' project, add a new file called "paramfile". (in the file explorer click "new>file>" enter paramfile - no extensions).+In a ''newRGG'' project, add a new file called "paramfile". (in the file explorer click "new>file>"enter paramfile - no extensions). 
 + 
 +{{:tutorials:first-steps:parafile.png?200|}}
  
 In the ''Model.rgg'' file copy: In the ''Model.rgg'' file copy:
Line 70: Line 72:
 } }
 </code> </code>
 +
 +**Note:**   
 +  * The variable ''myFloat'' is not included in the ''save'' and ''load'' methods. As a ''global'' it is automatically included in GroIMP serialization.
 +  * Any other serialization method can be used. Here we "simply" write varName=varValue as string in a text file. However, any other approach would work (e.g. write in csv, ...). The file does not have to be within the project (as ''paramfile'' is). 
 +  * To reset the three variables to their initial values, the project needs to be either ''recompiled'' or ''reset''.
 +
 +
 +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:
 +  * automatically create/ get a property file
 +  * save/load variables by simply giving them a "name" in the file
 +  * save/load variables without casting their type
 +
 +Example with the previous file:
 +
 +
 +<code java>
 +
 +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();
 +
 + }
 +}
 +</code>
 +
tutorials/first-steps/save-load.1742900017.txt.gz · Last modified: 2025/03/25 11:53 by gaetan