I need Help with save a int

** May not apply to Android** Let me know!

Read the reference:
https://processing.org/reference/saveStrings_.html

And the expected error if you are not using an array[] of strings:

:)

On Android you save strings like this.
But I think for saving scores you better use SharedPreferences.
After my lunch I’ll write some code, but now I’m hungry.

@noel can you Help me? :joy:

@Rogo === the error you get is clearly explained: saveStrings() is waiting for 2 params, first one is a String (the name for your file), second one is not a String, it s an Array; so before saving you have to create some String[] as a global and add your score_str to that. Now depending of your needs it could be more simple with android to use SharedPreferences API from android.
Now, seeing your errors while coding, i think that firstly you have to code for java (not android!) then try to adapt it to Android.

@noel

I’ve tried it for a few hours now but it didn’t work. Please help

Try this code. (tested on Lollipop)

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;

Activity act;
Context cnt;
SharedPreferences sp;
SharedPreferences.Editor editor;

int myScore;
String myScoreFile;

void setup () {
  background(0, 0, 150);
  textSize (40);
  textAlign (CENTER);
  act = this.getActivity();
  cnt = act.getApplicationContext();
  sp = PreferenceManager.getDefaultSharedPreferences(cnt);
  editor = sp.edit();
  myScoreFile = "TotalScores";
  myScore = loadScore(myScoreFile);
  text("Click here to gain points and save them.", width/2, height/4);
}

void settings() {
  fullScreen();
}

void draw () {
}

void mousePressed () {
  if (mouseY > height/2) {
    myScore++;
    saveScore(myScore, myScoreFile);
    background(0, 0, 150);
    text("Click here to gain points and save them.\n My saved score is  "+str(myScore), width/2, height/4);
    text("Click here to show saved points.", width/2, 3*height/4);
  } else {
    myScore = loadScore (myScoreFile);
    background(0, 0, 150);
    text("Click here to gain points and save them.", width/2, height/4);
    text("Click here to show saved points.", width/2, 3*height/4);
  }
}

void saveScore(int score, String name) {
  //editor.clear();
  editor.putInt(name, score);
  editor.commit();
}

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

void onPause() {
  super.onPause();
  saveScore(myScore, myScoreFile);
}

I dont understand this

what?..
On APDE you have to run in “app mode” , otherwise the integer will not be saved when you exit the app…

I’ve tweaked the code above a bit . Try again.

Okay wait :star_struck:

okay wait now i understand the meaning more

I got it thank you :slight_smile:

@noel how can I remove what is on myScore from “myScore” if I click on it during construction? for example, a upgrade costs 1000 clicks

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;

Activity act;
Context cnt;
SharedPreferences sp;
SharedPreferences.Editor editor;

int myScore;
String myScoreFile;
int bau;
float x = 100;
float y = 50;
float w = 150;
float h = 80;
String baufile;

void setup () {
  background(0, 0, 150);
  textSize (75);
  textAlign (CENTER);
  act = this.getActivity();
  cnt = act.getApplicationContext();
  sp = PreferenceManager.getDefaultSharedPreferences(cnt);
  editor = sp.edit();
  myScoreFile = "Scores";
  myScore = loadScore(myScoreFile);
  baufile = "build";
  bau = loadScore(baufile);
}

void settings() {
  fullScreen();
}

void draw(){
  myScore = loadScore(myScoreFile);
  bau = loadScore(baufile);
  background(0);
  text(str(myScore), width/2, height/14);
  fill(#FFFFFF);
  rect(x,y,w,h);
  text(str(bau), width/2, height/2);
}

void mousePressed () {
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
      ++bau;
      myScore--;
      redraw();
      saveScore(bau, baufile);
    }
  }
  myScore++;
  saveScore(myScore, myScoreFile);
}

void saveScore(int score, String name) {
  //editor.clear();
  editor.putInt(name, score);
  editor.commit();
}

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

void onPause() {
  super.onPause();
  saveScore(myScore, myScoreFile);
}

Simple.
If bau is 6400 und fein = 1000
bau = 6400-1000;
saveScore(bau, baufile);

@noel I dont underatand :joy:

Within the app, there is a preference file where values can be stored.
If your phone is rooted you can find it at this path:

/data/data/com.calsignlabs.apde.sketchpreview_preferences.xml

The file has a xml structure where you can see name of the key(label)
In this case “TotalScores”, which can have a value like here .“25”
The file looks like this:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <int name="TotalScores" value="25" />
</map>

In the first code above you can find this label name “TotalScores”
It is like a little box where you change the value when you use the function:

 void saveScore(int score, String name) {
  editor.putInt(name, score);
  editor.commit();
}

“score” is the integer value that will replace the existing value.
To retrieve the value that is stored you use the function:

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

That is all there is to it.
When a player loses points, just subtract it from the existing value in the “box”

You can have more labels in this preference.xml file, like you already have done in your code,
Just use another name for the key and the store value.
As you see it’s actually very simple.

A better name for the function could be getScore() instead of loadScore() and change the name
“myScoreFile” with myScoreLabel (or key).

I need like this

void mousePressed () {
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
      if(myScore > 9){
          myScore - 10;
          ++bau;
          saveScore(bau, baufile);
      }
    }else{
      myScore++;
      saveScore(myScore, myScoreFile);
    }
  }
}

@Rogo I dont underatand :joy:

You mean myScore -= 10;;

why are no 10 points deducted from “myScore”?

First thing to do is taking out

myScore = loadScore(myScoreFile);
bau = loadScore(baufile);

of the draw() loop and place them in mousePressed()
saveScore(bau, … in if
saveScore(myScore, … in else