I’m able to load the tables, but the game still crashes when it’s closed. I can’t really figure out the issue.
Maybe you could post your whole code?
It’s 800 lines, i don’t know if it’s too much. maybe I can post the save data code
Maybe you can post the part that shows how you exit the sketch. Because you do an automatic saveTable() at exit?
import android.os.Environment;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import processing.core.PConstants;
import android.app.Activity;
import android.os.Environment;
size(displayWidth, displayHeight);
frameRate(60);
orientation(PORTRAIT);
save = true;
for(int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
table = null;
loadData(tableFile);
if(table != null){
for(TableRow row : table.rows()){
int id = row.getInt(“id”);
scores[id] = row.getInt(“savedata”);
}
} else {
makeTable();
}
p = new Player();
gameTable();
font = createFont(“ClearSans-Bold.ttf”, 64);
icon = loadImage(“icon.png”);
if(!loadSave) {
p.addNewTile();
p.addNewTile();
} else {
p.loadSaveFile();
}
}
void makeTable() {
table = new Table();
table.addColumn(“id”);
table.addColumn(“savedata”);
for(int i = 0; i < scores.length; i++){
TableRow newRow = table.addRow();
newRow.setInt(“id”, table.lastRowIndex());
newRow.setInt(“savedata”, scores[i]);
}
}
void loadData(String s) {
try {
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+tableFolder+"/");
table = loadTable(directory+s, “header”);
println(directory+s+tableFile);
}
catch(Exception e) {
println(“first table loading”);
}
}
void loadData2(String s) {
try {
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+tableFolder+"/");
table2 = loadTable(directory+s, “header”);
println(directory+s+tableFile2);
}
catch(Exception e) {
println(“first table loading”);
}
}
void saveData(String file_s, String folder_s) {
try {
String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(absolute_path+"/"+folder_s);
println(file);
if (!file.exists()) {
boolean success = true;
success = file.mkdirs();
}
saveTable(table, file+"/"+file_s);
println(“File saved successfully.”);
}
catch (Exception e) {
println(“Error while saving file: " + e);
}
}
void saveData2(String file_s, String folder_s) {
try {
String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(absolute_path+”/"+folder_s);
println(file);
if (!file.exists()) {
boolean success = true;
success = file.mkdirs();
}
saveTable(table, file+"/"+file_s);
println(“File saved successfully.”);
}
catch (Exception e) {
println("Error while saving file: " + e);
}
}
private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
private void requestParticularPermission() {
activity.requestPermissions(PERMISSIONS_STORAGE, 2020);
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 2020:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
println(“permissions granted”);
permissions_granted = true;
} else {
println(“permissions not granted”);
}
break;
default:
activity.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
void draw() {
highScore = max(scores);
if(p.score > highScore) {
highScore = p.score;
}
if(scores[scores.length-1] != 0) {
int savedScore = max(scores);
for(int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
scores[0] = savedScore;
saveScores();
}
if(lose()) {
if(save) {
addNewScore(p.score);
updateTable();
saveData(tableFile, tableFolder);
save = false;
}
}
}
void addNewScore(int score){
for(int i = 0; i < scores.length; i++) {
if(score >= scores[i]) {
for(int j = scores.length-1; j >= max(i,1); j–) {
scores[j] = scores[j-1];
}
scores[i] = score;
break;
}
}
}
void updateTable() {
for(TableRow row : table.rows()) {
int id = row.getInt(“id”);
table.setInt(id, “savedata”, scores[id]);
}
}
void saveScores(){
saveData(tableFile, tableFolder);
}
void saveGame() {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
game[i][j] = p.getValue(i, j);
}
}
}
void gameTable() {
saveGame();
table2 = null;
loadData2(tableFile2);
if(table2 != null) {
for(int i = 0; i < 4; i++) {
TableRow row = table2.getRow(i);
game[0][i] = row.getInt(“col0”);
game[1][i] = row.getInt(“col1”);
game[2][i] = row.getInt(“col2”);
game[3][i] = row.getInt(“col3”);
}
loadSave = checkTable();
} else {
makeTable2();
}
}
void makeTable2() {
table2 = new Table();
table2.addColumn(“col0”);
table2.addColumn(“col1”);
table2.addColumn(“col2”);
table2.addColumn(“col3”);
for(int i = 0; i < game.length; i++) {
TableRow newRow = table2.addRow();
newRow.setInt(“col0”, game[0][i]);
newRow.setInt(“col1”, game[1][i]);
newRow.setInt(“col2”, game[2][i]);
newRow.setInt(“col3”, game[3][i]);
}
loadSave = false;
}
void updateSave() {
saveGame();
for(int i = 0; i < 4; i++) {
TableRow row = table2.getRow(i);
row.setInt(“col0”, game[0][i]);
row.setInt(“col1”, game[1][i]);
row.setInt(“col2”, game[2][i]);
row.setInt(“col3”, game[3][i]);
}
}
boolean checkTable() {
for(int i = 0; i < 4; i++) {
if(table2.getInt(i, “col0”) > 0) {
return true;
}
if(table2.getInt(i, “col1”) > 0) {
return true;
}
if(table2.getInt(i, “col2”) > 0) {
return true;
}
if(table2.getInt(i, “col3”) > 0) {
return true;
}
}
return false;
}
void clearTable() {
for(int i = 0; i < 4; i++) {
table2.setInt(i, “col0”, 0);
table2.setInt(i, “col1”, 0);
table2.setInt(i, “col2”, 0);
table2.setInt(i, “col3”, 0);
}
saveData2(tableFile2, tableFolder);
}
void exit() {
gameTable();
updateSave();
saveData2(tableFile2, tableFolder);
super.exit();
}
That was all of the savedata code. This is the code that runs when the program exits:
void exit() {
gameTable();
updateSave();
saveData2(tableFile2, tableFolder);
super.exit();
}
@noel === i tested the code you have put for saving table; it runs fine (MM, Nougat)
Hello @SNICKRS,
Please format your code:
https://discourse.processing.org/faq < See section on formatting.
You can also use the “Hide Details”… click on the for that when editing.
Example
You can put formatted code here…
For example:
void setup()
{
size(640, 360);
}
void draw()
{
background(0);
}
:)
@SNICKRS === diifficult to help you because the code you put is wrong ( eg . what is player…what is game ), quite all var are undefined, some of them we can guess but that is not good…
Oh, well then maybe I should send the entire code for the game. Maybe that would make it easier. I’m new to this, so I don’t really know much about the process.
Hello again, I tested my game with the updated code that @noel posted, and I’m able to save the game’s data! Now, the problem is that it doesn’t load it, but it’s in the sdcard!
I am sure that you will encounter a solution because if you could save, then there is no reason to not be able to read. The problem is in your code above, which btw you did not edit yet, putting it in a real code block by selecting solely the whole code and pressing the <> button.
Okay, the problem is officially solved! I was doing something wrong with the loadTable function, but it is working now! Thank you to @noel, @akenaton, and @paulgoux! For anyone with the same problem, please use @noel’s code because it works!
In order to save to internal storage, you can use getContext().getFilesDir().getAbsolutePath() too.