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 The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top