Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Vital Powers and Functions in Lua


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
-- 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:costOf("soldier") then
--    pass

-- 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)
    -- In your loop, you can "call" the functions defined above.
    -- The following line causes the code inside the "pickUpNearestCoin" function to be executed.

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

"A Fine Mint" issue. LUA

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!


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?


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?


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

    -- 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()

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