Loop for counting

Exide

I am amazingly focused right now!
Reaction score
448
Hi.

I believe I've done something similar to this a long time ago, in WC3. However I can't really remember how I did it, so here I am.

I have got a global string variable; "InventoryItem" with an array of 99.
I made this trigger as a test:

Trigger:
  • createInventory
    • Events
      • Game - Map initialization
    • Local Variables
    • Conditions
    • Actions
      • Variable - Set InventoryItem[1] = "potion"
      • Variable - Set InventoryItem[2] = "ether"
      • Variable - Set InventoryItem[3] = "phoenix down"
      • Variable - Set InventoryItem[4] = "testtest"
      • Variable - Set InventoryItem[12] = "potion"
      • Variable - Set InventoryItem[25] = "ether"
      • Variable - Set InventoryItem[85] = "phoenix down"
      • Variable - Set InventoryItem[99] = "phoenix down"


This creates an inventory.

Now I want to count how many of each items I have, so that the list will look like this:

Item 1: Potion - amount: 2
Item 2: Ether - amount: 2
Item 3: Phoenix Down - amount: 3

and so on..

To do this I somehow need to create a loop that compares each InventoryItem with every other InventoryItem.
Help appriciated.

(I will update this thread when/if I make progress on how to do this myself.)
 

Exide

I am amazingly focused right now!
Reaction score
448
I decided to update the thread by adding another post, to make it easier to read.

Here's the trigger I've made so far:

Trigger:
  • InventoryItemCheckAmount
    • Events
    • Local Variables
      • loop = 0 <Integer>
      • secondloop = 0 <Integer>
      • checkname = "" <String[99]>
    • Conditions
    • Actions
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Variable - Set InventoryAmount[loop] = 0
      • Variable - Set loop = 0
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Variable - Set checkname[loop] = InventoryItem[loop]
          • General - For each integer secondloop from 1 to 99 with increment 1, do (Actions)
            • Actions
              • General - If (Conditions) then do (Actions) else do (Actions)
                • If
                  • InventoryItem[secondloop] == checkname[loop]
                • Then
                  • Variable - Set InventoryAmount[secondloop] = (InventoryAmount[secondloop] + 1)
                  • **DISABLED**Variable - Set InventoryItem[secondloop] = ""
                  • General - If (Conditions) then do (Actions) else do (Actions)
                    • If
                      • InventoryItem[secondloop] != ""
                    • Then
                      • **DISABLED**Debug - Display ("Number of " + ((Text(InventoryItem[secondloop])) + (" = " + (Text(InventoryAmount[secondloop]))))) as debug output using Type 1, and Do display it in the game window
                    • Else
                • Else


This trigger actually does count the items in my inventory successfully, as far as I've tried it.
I wish to add a feature that: after counting an item twice - remove the second item (so that it doesn't get listed twice, or more).

I tried doing: **DISABLED**Variable - Set InventoryItem[secondloop] = ""
When I enable it, it clears ALL the items, though. I am currently working on fixing this.

Help appriciated.
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
Let's see if I understood what the code in your second loop does.

It starts looping through all items and for each item, it starts a second loop to count how many other items there are of it. If it finds another item of the same name in the second loop, it increases the amount in that index with 1.

The problem is that this code is that if we have 5 items of one type, it'll count those 5 items 5 times. This is not the reason the code isn't working but it's unnecessary and will mess things up later.

The reason it clears all the items is simple, in the second loop it finds all references of the item and clears them, even the first one. I'll edit my post with an improvement of your trigger in a minuter or so.

Edit: Do you really want to remove an item after counting it twice? If you do this, the second time you run the trigger, you'll have removed every item except one of each. In my code, I do remove an item after counting it twice but I do it in my temporary array so it's still there in the global array, to change this, change all references to checkName to InventoryItem.

Code:
Trigger:
  • InventoryItemCheckAmount
    • Events
      • Game - Player Any Player types a chat message containing "aqq", matching Exactly
    • Local Variables
      • loop = 0 <Integer>
      • secondloop = 0 <Integer>
      • checkName = "" <String[99]>
    • Conditions
    • Actions
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Variable - Set InventoryAmount[loop] = 0
          • Variable - Set checkName[loop] = InventoryItem[loop]
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • checkName[loop] != ""
            • Then
              • General - For each integer secondloop from 1 to 99 with increment 1, do (Actions)
                • Actions
                  • General - If (Conditions) then do (Actions) else do (Actions)
                    • If
                      • checkName[loop] == checkName[secondloop]
                    • Then
                      • Variable - Set InventoryAmount[loop] = (InventoryAmount[loop] + 1)
                      • General - If (Conditions) then do (Actions) else do (Actions)
                        • If
                          • loop != secondloop
                        • Then
                          • Variable - Set checkName[secondloop] = ""
                        • Else
                    • Else
              • Debug - Display (Combine ("Item ", ((Text(loop)) + " "), (Text(checkName[loop])), " - amount: ", (Text(InventoryAmount[loop])))) as debug output using Type 1, and Do display it in the game window
            • Else


I don't know how your map works but if I were you I would probably use a record(like a struct) with a string(called "name") and integer(called "amount") variable. Then make InventoryItem an array of that record and when you want to add an item to InventoryItem, you go through it and check if it's already in the array, if it is you add +1 to the amount, otherwise you add it to the end.

Still, I don't know how your map actually works, so this might not be viable.
 

Exide

I am amazingly focused right now!
Reaction score
448
I tried your trigger and it works really well.
It counted items properly, but it listed them in a strange way:

potion: 2
ether: 2
phoenix down: 3
testtest: 1
potion: 0
ether: 0
phoenix down: 0
phoenix down: 0


I fixed this by checking in the other trigger: if Amount[loop] == 0 then destroy *variable connected to dialog item*

It's a crude fix but it works so far.

I'm gonna test the trigger a little by adding and removing items from my inventory and see what happens.

Here's what my triggers look like at the moment:

Trigger:
  • Menu Inventory
    • Events
    • Local Variables
      • loop = 0 <Integer>
      • i = 35 <Integer>
    • Conditions
    • Actions
      • Trigger - Turn Open Menu Off
      • Trigger - Run InventoryItemCheckAmount (Ignore Conditions, Don't Wait until it finishes)
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Dialog - Destroy InventoryItemLabelName[loop]
          • Dialog - Destroy InventoryItemLabelAmount[loop]
          • Dialog - Destroy InventoryItemLabelValue[loop]
      • Variable - Set loop = 0
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • InventoryItem[loop] != ""
            • Then
              • Dialog - Create a label for dialog MenuInventoryDialog with the dimensions (200, 50) anchored to Top Left with an offset of (100, (85 + i)) with the text (Text(InventoryItem[loop])) color set to White text writeout set to False with a writeout duration of 0.0
              • Variable - Set InventoryItemLabelName[loop] = (Last created dialog item)
              • Dialog - Create a label for dialog MenuInventoryDialog with the dimensions (200, 50) anchored to Top Left with an offset of (700, (85 + i)) with the text (Text(InventoryAmount[loop])) color set to White text writeout set to False with a writeout duration of 0.0
              • Variable - Set InventoryItemLabelAmount[loop] = (Last created dialog item)
              • Dialog - Create a label for dialog MenuInventoryDialog with the dimensions (200, 50) anchored to Top Left with an offset of (900, (85 + i)) with the text (Text((InventoryItemCheckValue(InventoryItem[loop])))) color set to White text writeout set to False with a writeout duration of 0.0
              • Variable - Set InventoryItemLabelValue[loop] = (Last created dialog item)
              • Variable - Set i = (i + 20)
              • General - If (Conditions) then do (Actions) else do (Actions)
                • If
                  • InventoryAmount[loop] == 0 //Crude solution
                • Then
                  • Dialog - Destroy InventoryItemLabelName[loop]
                  • Dialog - Destroy InventoryItemLabelAmount[loop]
                  • Dialog - Destroy InventoryItemLabelValue[loop]
                • Else
            • Else
      • Dialog - Show MenuInventoryDialog for (All players)


(The reason I'm creating/destroying Dialog items like this is because every time I open the inventory menu I need to re-populate the list, in case an item has been added/removed.)


Trigger:
  • InventoryItemCheckAmount
    • Events
    • Local Variables
      • loop = 0 <Integer>
      • secondloop = 0 <Integer>
      • checkname = "" <String[99]>
    • Conditions
    • Actions
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Variable - Set InventoryAmount[loop] = 0
      • General - For each integer loop from 1 to 99 with increment 1, do (Actions)
        • Actions
          • Variable - Set checkname[loop] = InventoryItem[loop]
          • General - For each integer secondloop from 1 to 99 with increment 1, do (Actions)
            • Actions
              • General - If (Conditions) then do (Actions) else do (Actions)
                • If
                  • checkname[secondloop] == checkname[loop]
                  • checkname[secondloop] != ""
                • Then
                  • Variable - Set InventoryAmount[secondloop] = (InventoryAmount[secondloop] + 1)
                  • General - If (Conditions) then do (Actions) else do (Actions)
                    • If
                      • loop != secondloop
                    • Then
                      • Variable - Set checkname[loop] = ""
                    • Else
                • Else




EDIT: It works for adding and removing items the way I want (as far as I know). I'm gonna keep the triggers like this, unless anyone comes up with a better way of fixing my "crude fix".
Thanks, Siretu. =)
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
The reason it's listed as 0 for the other items is because it already counted that kind of item before. This means that it doesn't even go in and do the second for loop for the other items(which is good since that would basically be repeating things it had already done.

I would keep the triggers as they are. You might want to change it later if you run into some problems with it but I think you will be fine.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top