SC2Mapster Forums

Development > Triggers

[Solved] Trigger to write out "army tab" showing how many of each unit a player has

  • 8 posts
    #1 Jan 20, 2013 at 23:00 UTC - 0 likes

    I am trying to get the player's army composition (e.g. 10 drones, 1 overlord, 3 larva, etc.) written out to a bank file when a new unit is created. What I have currently is pasted below. My current issue is I'm only able to get the total number of units, and I'm not able to get the unit name/identifier written out. Ideally, I'd be writing data out:

    "drones" : 10 "overlord" : 1 "larva" : 3

    Can anyone offer some direction on how to get this working properly? It's not critical I get the actual unit name. So long as it's some unique identifier, I can figure out what those identifiers are and then create a mapping between those and "human-readable" names farther down in my stack.

    Many thanks!

    When a unit is built write out current unit counts
        Events
            Unit - Any Unit creates a unit with ability Any or behavior No Game Link
        Local Variables
            i = 0 <Integer>
            unitPickedUnit = No Unit <Unit>
            unitPickedName = "" <String>
            unitPickedUnitTotalCount = 0 <Integer>
            allUnitsGroup = No Unit Group <Unit Group>
        Conditions
        Actions
            Player Group - For each player i in (All players) do (Actions)
                Actions
                    Variable - Set allUnitsGroup = (Any units in (Entire map) owned by player i matching Excluded: Missile, Dead, Hidden, with at most Any Amount)
                    Bank - Open bank "unitsBuiltBank" for player i
                    Unit Group - Pick each unit in allUnitsGroup and do (Actions)
                        Actions
                            Variable - Set unitPickedUnit = (Picked unit)
                            Variable - Set unitPickedName = "how_do_i_get_this"
                            Variable - Set unitPickedUnitTotalCount = (Number of Living units in allUnitsGroup)
                            Bank - Store integer unitPickedUnitTotalCount as unitPickedName of section "data" in bank (Last opened bank)
                    Bank - Save bank (Last opened bank)
    
    #2 Jan 21, 2013 at 05:36 UTC - 0 likes

    switch depending on unit type of unitpicked unit
    if (drone)
    set unitpickedname = drone

    there might be a way to return as the name of the unit but i don't have editor open right now and i'm too lazy to do so right now, i'll edit this in the morning and post a new reply if i find an easier method.

    I'm a triggerer
    I can't make awesome terrains
    I dabble in data

    I suck at poems
    That is why i do haiku
    i suck at them too :(

    #3 Jan 21, 2013 at 06:44 UTC - 0 likes

    @willuwontu: Go

    Cool! Thanks! Would appreciate that. I saw some code on this site for doing it for a specific unit, but doing it for every race and unit in that race seems a bit redundant, but if it's the only way I suppose it'll have to be done that way.

    #4 Jan 21, 2013 at 08:41 UTC - 0 likes

    here's a map that returns the unit's name, you'll have to convert it to what you want in order to use it, but it gives you the idea

    Name Size MD5
    Getting_unit_name.SC2Map 9.8 KiB f0973c7ed736...
    #5 Jan 21, 2013 at 21:42 UTC - 0 likes

    @willuwontu: Go

    Thank you thank you thank you! That worked great! For anyone who stumbles upon this later, here is my trigger to get this working:

    When a unit is built write out current unit counts
        Events
            Unit - Any Unit creates a unit with ability Any or behavior No Game Link
        Local Variables
            player = 0 <Integer>
            xmlKey = 0 <Integer>
            unit = No Unit <Unit>
            unitPickedName = "" <String>
            allUnitsGroup = No Unit Group <Unit Group>
        Conditions
        Actions
            Player Group - For each player player in (All players) do (Actions)
                Actions
                    General - If (Conditions) then do (Actions) else do (Actions)
                        If
                            (Owner of (Created unit)) == player
                        Then
                            Bank - Open bank "unitsBuiltBank" for player player
                            Variable - Set allUnitsGroup = (Any units in (Entire map) owned by player player matching Excluded: Missile, Dead, Hidden, with at most Any Amount)
                            Unit Group - For each unit unit in allUnitsGroup do (Actions)
                                Actions
                                    Variable - Set unitPickedName = (Value of Units (Unit type of unit) Name for player Any Player)
                                    Bank - Store string unitPickedName as (String(xmlKey)) of section "data" in bank (Last opened bank)
                                    Variable - Set xmlKey = (xmlKey + 1)
                            Bank - Save bank (Last opened bank)
                        Else
    

    This will write out data to the bank that looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <Bank version="1">
        <Section name="data">
            <Key name="4">
                <Value string="Unit/Name/SCV"/>
            </Key>
            <Key name="6">
                <Value string="Unit/Name/SCV"/>
            </Key>
            <Key name="3">
                <Value string="Unit/Name/SCV"/>
            </Key>
            <Key name="0">
                <Value string="Unit/Name/SCV"/>
            </Key>
            <Key name="2">
                <Value string="Unit/Name/SCV"/>
            </Key>
            <Key name="7">
                <Value string="Unit/Name/CommandCenter"/>
            </Key>
            <Key name="5">
                <Value string="Unit/Name/SCV"/>
            </Key>
        </Section>
    </Bank>
    

    And at the next layer in my stack (I'll share this project once it's complete), I'm able to condense this into the desired format of: SCV : 7 CommandCenter : 1 Marine : 1

    I didn't see anyway to iterate over each type of unit and write this out directly from SC2 to the bank files without doing if (Marine ) if (SCV), etc. Does anyone know if there are any optimizations between my code and the final desired product?

    Thanks all!

    #6 Jan 21, 2013 at 23:29 UTC - 0 likes

    you can get rid of the Unit/name/ by removing the first 10 characters of each string btw.

    #7 Jan 22, 2013 at 00:03 UTC - 0 likes

    @willuwontu: Go

    Ahh sorry - should've been clearer. This doesn't have to do with the Unit/Name stuff - that can easily be removed in the Editor or later on as I'm doing now.

    I meant currently I write out SCV, SCV, SCV, SCV, Command Center to my bank file. Is there any way to make it so I can write out SCV 6, Command Center 1, Marines 17, etc. Right now I just write out a list of every unit.

    I suppose I could do as I'm doing now, but for every unit type, get the total of that unit and write that, but then I'd still be writing N times if I have N units no?

    #8 Jan 22, 2013 at 02:46 UTC - 0 likes

    what you're doing is fine i was just commenting on how the example above had:

    Quote from marktronic: Go

    "Unit/Name/SCV"

    and how you could get rid of that easily

    Last edited Jan 22, 2013 by willuwontu
  • 8 posts

You must login to post a comment. Don't have an account? Register to get one!