Steelclaw Gap and Passing Strings into Variables from Global Arrays (Help!)

I’m stuck on Steelclaw Gap, with an error in this code section:

local summonTypes = {}
summonTypes[1] = "soldier"
summonTypes[2] = "soldier"
summonTypes[3] = "soldier"
summonTypes[4] = "soldier"
summonTypes[5] = "archer"
summonTypes[6] = "archer"
summonTypes[7] = "archer"
summonTypes[8] = "archer"


-- You start with 360 gold to build a mixture of soldiers and archers.
-- self.built is an array of the troops you have built, alive or dead
-- Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
function summonTroops()
    
    local troop = summonTypes[#self.built % #summonTypes]
    if self.gold >= self:costOf(troop) then
        self:summon(troop)
    end
end

This code is translated roughly from the (i assume) javascript sample code which you are given at the start of the level into Lua. I have defined the array summonTypes in the same manner as the array was constructed in Zoo Keeper, though i have used strings for the values. This may be one source of my issues. The issue i am having is that the variable troop SHOULD take the value given to it by the modulo statement and become a string, but it doesn’t. when i call costOf in the next line, i receive an error message telling me that costOf must be called on a string, either “soldier” or “archer”, implying that troop is not properly storing the strings from my array. How do I get my strings to pass from the array i have defined to the variable i need them to occupy?

1 Like

Oops, the Lua method of wrapping around an array is harder, because Lua arrays start at 1. So try something like this:

    local troop = summonTypes[(#self.built % #summonTypes) + 1]
1 Like