JSON 파일의 일반적인 개념과 쓰기에 몇 가지 문제가 있습니다. 현재, 게임 아이템의 ObjectMap을 가지고 있는데, 문제는이 맵을 변경하는 것입니다. 아래에 표시된 항목이라는 클래스가 있습니다.ObjectMap을 json Libgdx에 저장하도록 변경
public static class Item {
public String name;
public String type;
public String rarity;
public int reqLevel;
int priceBuy;
int priceSell;
int amount = 0;
public item() {}
public void setAmount(int amnt) {amount = amnt;}
}
그래서이 게임의 모든 항목에 대한 json 파일을 가지고 있기 때문에 완벽하게로드 할 수 있습니다. 내가하고 싶은 일은 setAmount 또는 다른 방법으로 사용하는 항목의 양을 변경하는 것입니다. 현재 모든 항목이 ObjectMap에로드됩니다. save() 및 load() 아래에 게시했습니다. 필자는지도를 작성하기 전에지도 내에서 항목을 변경해야한다고 생각하지만 아무 것도하지 못하게하는 데 어려움을 겪고 있습니다.
public void save()
{
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
file.writeString(json.prettyPrint(items), false);
}
public void load() {
Json json = new Json();
items = json.fromJson(ObjectMap.class, file);
}
편집 : @Sneh 사실 나는 그래서 당신은 등 여러 항목
을 가질 수있다, 그것이 ObjectMap.class의 fromJson을 완벽하게 수용,이 JSON 파일이 어떻게 생겼는지 생각* 잘못 될 수 있지만 의도적으로 작성하는 시나리오에서 완벽하게로드 및 쓰기하고 있습니다. 문제가있는 항목은 특정 항목 금액 변수를 변경하는 것입니다.
//Test file with small amount of items
{
"SwordOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Sword of Death",
"type" : "Sword",
"rarity": "Common",
"reqLevel": 1,
"proficiency": "Strength",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Death",
"type": "Dagger",
"rarity": "Rare",
"reqLevel": 3,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfLife": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Life",
"type": "Dagger",
"rarity": "Epic",
"reqLevel": 5,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"LifeSword": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Life Sword",
"type": "Sword",
"rarity": "Legendary",
"reqLevel": 45,
"proficiency": "Strength",
"damageMax": 100,
"damageMin": 99,
"priceBuy": 1000,
"priceSell": 2000,
"bonusMagic": 0,
"bonusStrength": 5,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 10,
"amount": 2
}
}
항목 개체를 만들고 변경하는 위치를 표시 할 수 있습니까? – Sneh