Cant save with saveStrings?

edited March 2014 in Android Mode

Im trying to save the games highscore.

void loadHighscore(){
  // Load text file as a string
  String[] scoreboard = loadStrings("Highscore.txt");
  // Convert string into an array of integers using ',' as a delimiter
  Highscore = int(scoreboard[0]);
}

void saveHighscore(){
  String[] scoreboard = new String[2];
  scoreboard[0]=str(Highscore);
  scoreboard[1]="why this no work?";
  saveStrings("Highscore.txt",scoreboard);
}

Where Highscore is an int in game that holds, you guessed it, the current highscore. And Highscore.txt is a text file in my data folder with one line in it as "0" (zero if your font is wierd)

ignore the comments BTW i stole this from the data tutorial :P

Answers

  • edited March 2014

    Have you invoked those functions anyway?! :O)

    int Highscore;
    
    void setup() {
      loadHighscore();
      saveHighscore();
    
      exit();
    }
    

    Moreover, in order to save in the data folder, we gotta use dataPath(""):

    saveStrings(dataPath("Highscore.txt"), scoreboard);
    
  • edited March 2014

    Yes, but you did scare me, i had to check :))

  • Hmm still didnt work...

  • uh ok. It did work if i add datapath to both savestrings and loadstrings 8-| thanks alot Mr Loop.

  • edited March 2014 Answer ✓

    Nice it work. Notice that only save functions actually need dataPath(""). It's redundant for load 1s! :P

  • Actually on android it didnt seem to work without it.

  • ok nevermind. Your answer doesnt work on the kindle fire? This is strange...

  • edited March 2014

    I don't think Android has a dataPath("")! You gotta search other threads for an equivalent solution! :(|)

    Notice that load functions search 1st the sketchPath for a file. Only then it looks inside dataPath("").
    So if a file is found inside sketchPath already, it won't bother to search for it further! (~~)

  • I would recommend using a Table, which seems to be able to load / save without needing dataPath() (loadTable() and saveTable()). I believe that this worked on my Kindle Fire as well. Note that the use is a bit different, but the table data structure might be better suited to storing high score data in the first place (I have used Table for high scores in the past).

  • In your app, when you save to a table, did you use an absolute path or just the file name?

  • edited March 2014

    When saving the Table, I was able to use just the file name.

  • i thought i tried that but ill give it another crack.

Sign In or Register to comment.