Vital Powers and Functions in Lua


#1

My code does not work. In the lua version of this level, functions are not documented yet and the sample code has not been translated to lua. This aside, I have attempted to figure out how to use functions from external sources. the issues i am having are that my character does not respond at all. Apparently I don’t know how to call functions in lua. Here is my code, can any more experienced players and coders tell me what i’m doing wrong?

local function pickUpNearestCoin()
    local items = self.findItems()
    local nearestCoin = self.findNearest(items)
    if nearestCoin then
        self:move(nearestCoin.pos)
    end
end    
-- This function has your hero summon a soldier.
local function summonSoldier()
    -- Fill in code here to summon a soldier if you have enough gold.
    if self.gold > self:costOf("soldier") then
        self:summon("soldier")
    else
        
    end
--    pass
end

-- This function commands your soldiers to attack their nearest enemy.
local function commandSoldiers()
    local friends = self:findFriends()
    
    for x=1, #friends do
        local soldier = friends[x]
        local enemy = soldier:findNearestEnemy()
        if enemy then
            self:command(soldier, "attack", enemy)
        end
    end
end
loop
    -- In your loop, you can "call" the functions defined above.
    -- The following line causes the code inside the "pickUpNearestCoin" function to be executed.
    pickUpNearestCoin()
    self:summonSoldier()
    self:commandSoldiers()
    
end 

can anyone see what i’m doing wrong? Thanks in advance.


"A Fine Mint" issue. LUA
#2

So basically this has halted my process through this game. Until someone crosses functions off this list:

i wont be able to continue in my quest to learn. Bumping for help!


#3

Hope to get someone with better Lua than mine to document that soon (any volunteers?).

I don’t think you need the “local” keyword in front of the functions, but maybe that works. But make sure you are calling methods like self:findItems() instead of self.findItems() – the : makes sure the self gets passed to the function, I think. Maybe that’s it?


#4

thanks for the response! my problem now is that any time I attempt to call a function, it says that when calling the function that i’m calling a nil value. any advice for making my function NOT be nil?


#5

RESOLVED! Here is the working version of calling the previously defined functions!

loop
    -- In your loop, you can "call" the functions defined above.
    -- The following line causes the code inside the "pickUpNearestCoin" function to be executed.
    f = pickUpNearestCoin()
    g = summonSoldier()
    h = commandSoldiers()
    
end 

turns out LUA functions are values just like numbers and strings, and must be set to a variable to be properly called i suppose.