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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top