1
\$\begingroup\$

Hello I've been developing this game for awhile and now trying to implement save data via Json. I've gotten to where when the app is first launched it will write the Json file with the variables I would like the game to save, but it only writes part of the class that I am passing.

Here is my implementation

@Override
    public void create()
    {


        batch = new SpriteBatch();
        file = Gdx.files.external("/bin/save.json");
        create.setFirstSpawn(true);
        create.setSpawnX(-7);
        create.setSpawnY(417);
        create.setTime(0);
        create.setMap("cabinMap");


        if(!file.exists()) {
            Json json = new Json();

            json.setOutputType(JsonWriter.OutputType.json);
            file.writeString(json.prettyPrint(create), false);

        }
        if (file.exists()) {
            JsonReader jread = new JsonReader();
            JsonValue base = jread.parse(file);
            SpawnX = base.getFloat("SpawnX", -7);
            SpawnY = base.getFloat("SpawnY", 417);
            map = base.getString("map", "cabinMap");
            time = base.getFloat("time", 0);
firstSpawn = base.getBoolean("firstSpawn", true);
        }
        if(map == "cabinMap") {
            this.setScreen(new cabinMap(this));
        }
        if(map == "tMap") {
            this.setScreen(new tMap(this));
        }
        if(map == "lowerTMap") {
            this.setScreen(new lowerTMap(this));
        }
        if(map == "rightTMap") {
            this.setScreen(new rightTMap(this));
        }
        else {
            this.setScreen(new cabinMap(this));
        }
    }

Here is the class that I am passing

public class create
{
    boolean firstSpawn;
    float SpawnX;
    float SpawnY;
    float time;
    String map;

    public void setFirstSpawn(boolean firstSpawn)
    {
        this.firstSpawn = firstSpawn;
    }

    public void setTime(float time)
    {
        this.time = time;
    }

    public float getTime()
    {
        return time;
    }

    public void setMap(String map)
    {
        this.map = map;
    }

    public String getMap()
    {
        return map;
    }

    public boolean isFirstSpawn()
    {
        return firstSpawn;
    }

    public void setSpawnX(float spawnX)
    {
        SpawnX = spawnX;
    }

    public float getSpawnX()
    {
        return SpawnX;
    }

    public void setSpawnY(float spawnY)
    {
        SpawnY = spawnY;
    }

    public float getSpawnY()
    {
        return SpawnY;
    }
}

And this is my result

{
"map": "cabinMap",
"SpawnX": -7,
"SpawnY": 417
}

As you can see from the output file I am missing 2 values. float time and boolean firstSpawn are missing from the file. I've worked for hours trying to find out why. I have no clue why it omitted part of the code... If anyone knows why that would be very helpful to me.

\$\endgroup\$
3
\$\begingroup\$

Well I fixed my issue that I was having. Instead of using libGDX

Json

to write to file from the class I used

JSONObject

and used put() and surrounded it with try and catch to write the classes now the file is pretty and complete with the data that I want to pass.

if(!file.exists()) {

        JSONObject jobj = new JSONObject();

        try
        {
            jobj.put("SpawnX", -7);
            jobj.put("SpawnY", 417);
            jobj.put("time", 0);
            jobj.put("map", "cabinMap");
            jobj.put("firstSpawn", true);
        }
        catch (JSONException e)
        {}
        Json json = new Json();

        json.setOutputType(JsonWriter.OutputType.json);
        file.writeString(json.prettyPrint(jobj), false);

    }

Output

{
"nameValuePairs": {
    "SpawnX": {
        "class": "java.lang.Integer",
        "value": -7
    },
    "SpawnY": {
        "class": "java.lang.Integer",
        "value": 417
    },
    "time": {
        "class": "java.lang.Integer",
        "value": 0
    },
    "map": {
        "class": "java.lang.String",
        "value": "cabinMap"
    },
    "firstSpawn": {
        "class": "java.lang.Boolean",
        "value": true
    }
}
}

That is so much better. Well I hope that helps someone else in the future that has the same problem. I still would like to know what went wrong.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.