Okay so, I added saving into my game (through serialization), which works completely fine, but these items have things like sprites and stuff that I logically wouldn't save, how I got around that was pretty simple.
I made a method in my Item class (every item extends it) that assigns everything it needs to (called basicInitialization()). This works great!
However, I noticed that any code placed after the loading of items wouldn't run. I investigated and realized I was stuck in an infinite for-loop:
public void loadItems(Player p) {
Item[] temp = SaveGame.loadItems();
for (int i = 0; i < items.length; i++) {
this.removeByIndex(i);
}
for (int j = 0; j < temp.length; j++) {
items[j] = temp[j];
}
for (int t = 0; t < items.length; t++) {
if (items[t] == null) {
t += 1;
}
items[t].basicInitialization();
if (items[t] instanceof EquipableItem) {
items[t].basicInitialization(((EquipableItem)items[t]).slot);
}
}
}
When I removed the:
items[t].basicInitialization();
if (items[t] instanceof EquipableItem) {
items[t].basicInitialization(((EquipableItem)temp[t]).slot);
}
portion and the problem went away.
Am I missing something really obvious here?
Thanks for any help you can give, if anymore code is needed I will happily give it!
Edit: - Re-structured some code Here is an example of basicInitialization:
public void basicInitialization() {
this.sprite = Sprite.HealthPotion;
this.name = "Health Potion";
this.value = "25";
this.desc = "Heals Up to 5 HP";
level = Game.getGame().getLevel();
}
You should have the code inside first
for
loop as :If the next item is null, then it would get assigned without the
else
block.