Save int for Android program

hey,
i want that counter is saved when somebody restart the app or something.
I tried it with a string but idk how i can do it. I think you dont need the pictures.

thanks for help

here the code:

PImage yo_oli, kind, aldi, shop_innen;
int x;
int counter,level, auswahl;



void setup()
{
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE);
  yo_oli=loadImage("yo_oli2.png");
  kind=loadImage("kind.png");
  aldi=loadImage("aldi.jpg");
  shop_innen=loadImage("shop.jpg");
  x=400;
  level=1;
  auswahl=1;
  
}

void draw()
{
  if (auswahl==1)
  {
    
    image(aldi, 0, 0, displayWidth, displayHeight);
    image(yo_oli, x, 200, 400, 800);
    image(kind, 1000, 140, 300, 400);
    fill(255, 64, 64);
    rect(750, 700, 400, 200);
    rect(1600, 900, 300, 100);
    fill(139, 0, 0);
    textSize(50);
    text("Rein in die Olga", 760, 810);
    text("Olga Counter:" + counter, 720, 50);
    text("Level: " + level, 20, 50);
    text("Shop", 1690, 970);

    if (x>600)
    {
      x=400;
      counter=counter+1;
      
    }


    //LEVEL
    if (counter>20)        
    {
      level=2;
    }

    if (counter>40)        
    {
      level=3;
    }

    if (counter>60)        
    {
      level=4;
    }
    if (counter>80)        
    {
      level=5;
    }
    if (counter>100)        
    {
      level=6;
    }
    if (counter>120)        
    {
      level=7;
    }
    if (counter>140)        
    {
      level=8;
    }
    if (counter>160)        
    {
      level=9;
    }
  }
  if (auswahl==2)
  {

    image(shop_innen, 0, 0, displayWidth, displayHeight);
    fill(255, 64, 64);
    rect(10, 900, 300, 100);
    fill(139, 0, 0);
    textSize(50);
    text("back", 100, 970);
    textSize(100);
     text("Coming soon!", 600, 500);
  }
}

void mousePressed()
{
  println(mouseX, mouseY);
  if (auswahl==1)
  {
    if (mouseX>750 && mouseX<1150 && mouseY>700 && mouseY<900)
    {
      x=x+100;
    }

    if (mouseX>1600 && mouseX<1900 && mouseY>900 && mouseY<1000)
    {
      auswahl=2;
    }
  }


  if (auswahl==2)
  {
    if (mouseX>10 && mouseX<310 && mouseY>900 && mouseY<1000)
    {
      auswahl=1;
    }
  }
}

while it might not be easy under android,
the idea could be to use a file,
and
https://processing.org/reference/loadStrings_.html
https://processing.org/reference/saveStrings_.html


a bigger thing is a general SETTINGS handling, like i try here

jeah I tried it already but it dont work

in your code i can not see that you tried to store data to a file
-a- what code
-b- does it work with processing 3.5.3 on PC with a /data/datafile.txt
-c- but it does not work under android?

i had it before but i have a problem with the
save and loadStrings

That is the reason why im asking in this forum

@Nick1 ===

that is easy; you have to save your ints (when==on onPause) with preferences, that s all

can you make a exaple for me?
I dont understand this

but thanks

Here is a link where onPause is described in case you want to know more about this.

I did a quick search in the forum:

https://discourse.processing.org/search?q=onPause

And I recommend this: super.onSaveInstanceState does not exist to start.

You can add the following to your sketch:

//  @@@@@@@@@@@@
@Override 
  public void onPause() {
  super.onPause();
}

Also, this link gives a lead to what you could do next. For now, you can use the file approach. You whould be able to use functions saveStrings() and loadStrings() for this purpose. More here.

Kf

Thanks but i dont get it

@Nick1 ===the solution provided by @kfrajer has to work && mine too. Code snippet with sharedPrefrences:

import android.preference.PreferenceManager;
import android.content.Context;
import android.content.SharedPreferences;
/////////////////////////////////////////////////////////
int highscore= 0;
Context context;
public static final String MyPREFERENCES = "MyPrefs" ;
boolean presse = false;
SharedPreferences settings;

/////////////////////////////////////////////////////


 
void setup () {
  size(displayWidth, displayHeight);
  context= this.getActivity().getApplicationContext();
  settings = context.getSharedPreferences(MyPREFERENCES,0);//creer l'objet
  loadScore();//fonction qui charge le score
  println("lescore actuel est de====" + highscore);
}
 
void draw() {
  
  background(255,0,0);
    textSize(72);
  text(highscore, 100,100);
  
  
}

public void loadScore() {//get the score
  
    SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this.getActivity().getApplicationContext());
    highscore = sharedPreferences.getInt("score", 0);
  }
  
  public void addScore(String key, int value){//save the score
    
    SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this.getActivity().getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
  }
  
  
 
void mousePressed(){
highscore++;
addScore("score", highscore);//enregistrer le nouveau score

}
 
void onPause(){//save the score
  super.onPause();
  SharedPreferences settings =this.getActivity(). getSharedPreferences(MyPREFERENCES,0);
   SharedPreferences.Editor editor = settings.edit(); 
   editor.clear();
   editor.putInt("score", highscore); 
   editor.commit();
}
void onResume() {
super.onResume();
if(settings !=null){//au lancement settings est null....
    highscore = settings.getInt("score", 0); 
}
}
1 Like