Beginning Code doesn't declare functions?

Ok, so something that wasn’t made very clear during my playthrough was how to declare functions (in Lua). For example, the game would spawn me in with code like this:

function findAndAttackEnemy()

and the game would give me reference error, saying it’s not defined. However, if I changed it to

function hero:findAndAttackEnemy()

everything works just dandy. I guess you might be asking, “Well, if you found the solution, what’s the issue?”. The issue I guess is that the 1st function is what you spawn in with, before you type another line of code, which I believe is confusing to new players (or at least to those new to a language)

The current levels I’ve seen in on are world 2, from Village Rover to A Fine Mint. Also, on A Fine Mint, it displays the hints in CoffeeScript for some reason (aka, the comments that help the player). I’d also like to know the deal about that too.

Thank you for reading, and I hope we can make some progress, or at least reach and understanding.

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

Thanks for the response! In the meantime, I actually did beat the level with how I did it, but I decided to amend it to what you said since that actually seems better. Also, thanks for the links to the level editor and stuff! I’ll certainly look it over when I get the chance/get better at Lua.