Beginning Code doesn't declare functions?

Hello!

I looked into this issue and can confirm there was a problem with it. But not anymore! I fixed it!

I apologize for the confusion, I think I was the designer who added the Lua code but, because I’m not too familiar with it, I typed it incorrectly.

Functions are just like variable and need to be scoped properly. What you were doing:

function hero:findAndAttackEnemy()

was scoping this function directly to the hero, so you’d always have access to it. Not a bad strategy, but, there is a better way.

local function findAndAttackEnemy()

This appropriately scopes the function to the current block of code. local means you can access it from any other blocks that exist in the same block as itself. Note I also fixed the enemy variable not being scoped properly:

local function findAndAttackEnemy()
    local enemy = hero:findNearestEnemy() -- This only exists inside the function.
end
local foo = findAnyAttackEnemy()
hero.say(enemy) -- This is an error! Enemy isn't defined within this outer-block.

Also, some levels are not properly translated to Lua yet. Some of the designers here aren’t too familiar with Lua, so we usually leave Lua sample code ‘translations’ up to the community. We give people the sample code from another language to start, then hope they go into the levels and fix it and submit a patch for us to accept! We used to have better Lua coverage until we started producing up to 8(!) levels a week, in which Lua tends to fall behind. We apologize, but if you’d like to contribute Lua sample code, it’s easy!

  1. Go to the level editor: http://direct.codecombat.com/editor/level/
  2. Find which level you want to edit: http://direct.codecombat.com/editor/level/village-rover
  3. Double-click the Hero (or gray-ghost Hero Placeholder) to access the Thang’s components.
  4. Go to the Thang’s Programmable Component
  5. Expand the Thang’s Plans object
  6. Expand Languages
  7. Add Lua, or edit the Lua sample code to better match the correct results.
  8. Go to the top right corner and click the Disk icon
  9. Create a commit message (something like: ‘adds Lua sample code’) and submit a patch

When the designers have time, we go through and verify the content of the patch and accept them! Then you’ll officially be an Open Source Contributor!

But, still, I appreciate your patience during that error, and I apologize about the confusion. Thank you for reporting it so we can fix it, instead of just giving up.

1 Like