Friday, April 22, 2022

Coding in TTS (2)

 The routine for rebuilding the holding zone is similarly simple:

function rebuildHold(nfo)

  --the holding zone GUID is in nfo[1]

  zoneObjects = getObjectFromGUID(nfo[1]).getObjects()

  --the default starting position is in nfo[2]

  sPos = nfo[2]

  --the separation increment is in nfo[3]

  incVal = nfo[3]

  --the action zone is in nfo[4]

  actionObjects = getObjectFromGUID(nfo[4]).getObjects()

  --set up the count variable

  dieCount = 0

  --count the number of vanguard action dice currently in the action zone

  for _, die in ipairs(actionObjects) do

    if die.hasTag("action") then

      if die.getName() != "Footman" and die.getName() != "Breacher" and die.getName() != "Berserker" then

        dieCount = dieCount + 1

      end

    end

  end

  --find new starting position based on the number of dice being returned to the holding zone

  sPos = sPos + (incVal * dieCount)

  --move all objects currently in the holding zone arbitrarily

  for _, obj in ipairs(zoneObjects) do

    vPos = obj.getPosition()

    nPos = {sPos, vPos[2], vPos[3]}

    obj.setPosition(nPos)

    sPos = sPos + incVal

  end

end

This is the first routine I wrote which I passed multiple values through. I never doubted it was possible, I just didn't want to mess with it while trying to test out whether or not things were possible in TTS (so that I wasn't confused about where the errors might be coming from should there be any)

Same as before, there are two scripting zones at play (actionZone and holdingZone).

First, loop through the action zone and simply count any die which is an action die but is not a Battalion die. Since I've named the dice when they are generated, this is being done using the name. You probably can see this plainly, but just a "programming" note is the fact that writing die.getName() is completely arbitrary. If I had written for _, apples in ipairs(actionObjects) do then it would be written apples.getName(). Like I said, I am sure that's patently obvious, but sometimes when I read other people's code I'm like "is he writing die because that's a thing this program understands or just because he decided it would be called die and defined that elsewhere?"

Second, calculate how many die "positions" are going to be used by the number of Champion dice currently in the action zone (using the incVal which is holding the current increment value for die positioning). 

Third, move any dice currently in the holding zone down to make room for the dice which are about to be moved in. I use the vector vPos just becuase it is convenient to grab the current vector of whatever object already exists and simply move the X value to the new starting position without trying to remember or calculate the Y or Z values. Again, this is likely very, very standard practice. But I think very absolutely. I am most comfortable telling the script to put something at exactly these coordinates right here. So, I get really proud of myself any time I remember to program something in a relative way.

No comments:

Post a Comment