Stuck In For Loop?

114 views Asked by At

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();
        }
2

There are 2 answers

6
shruti1810 On

You should have the code inside first for loop as :

int j = 0;
if (temp[i] == null) { //If there is no item then continue to the next one
    i+= 1
}
else{
    items[j] = temp[i];
    j++;
}

If the next item is null, then it would get assigned without the else block.

0
coinreturn On

I think I've finally fixed it, thanks for all the help! It was definitely a tough nut to crack!

 public void loadItems(Player p) {

      Item[] temp = SaveGame.loadItems();


      for (int i = 0; i < items.length; i++) {
       this.removeByIndex(i);  // Remove all current items
      }

      if (this.lastItemInList(temp) == -1) { // If the loaded item list has no items
       return;
      }
      for (int j = 0; j < temp.length; j++) {
       items[j] = temp[j];items
      }
      for (int t = 0; t < items.length; t++) { 
       if (items[t] == null) {
        if (t != items.length) {
         for (int i = t; i < items.length; i++) {
          if (items[i] != null) {
          t = i;       //Gets the next time there is an item that isn't null
          break;
          }
         }




        }
       } 

       items[t].basicInitialization();

       if (items[t] instanceof EquipableItem) {
        items[t].basicInitialization(((EquipableItem)items[t]).slot);
       }

       if (t == this.lastItemInList(items)) { //Once it hits the last item, then return
        System.out.println(":::FULLY LOADED ITEMS >> RETURNING:::");
        return;
       }
      }
     }