Blind distance help! [LUA]


#1

So I’m currently in the Blind Distance course and everything was written on javascript and I’m kind of confused as to what to do. I’m using Lua for my programming language, I managed to successfully convert one mission from java to lua but this one I can’t :confused:

any help ?

– This function finds the nearest enemy and returns the distance to it.
function nearestEnemyDistance()
local enemy = hero:findNearestEnemy()
–If there is no enemy, the function returns 0.
local result = 0
if enemy then
result = hero:distanceTo(enemy)

end

while true do
nearestEnemyDistance()
–save the result in the variable enemyDistance.
local enemyDistance = nearestEnemyDistance()
if enemyDistance > 0 then
hero:say(enemyDistance)
–If the enemyDistance is greater than 0:

    --// Say the value of enemyDistance variable.
    
end 

end
end


#2

bumperino I really need this solved :confused:


#3

what is your problem


#4

are you on lua or javavscript


#5

if the level is in javascript but you have lua selected, click on “restart” in the level’s menu (top right). This should reload the code and put it in lua!


#6

No, doesn’t reload in LUA. However, here is my try for the translation.
I think the bug is in the definition of the function???

function nearestEnemyDistance() 
    local enemy = hero:findNearestEnemy()
    local result
    if (enemy) then
        result = hero:distanceTo(enemy)
    else
        result = 0
    end
    return result
end


while true do
    local enemyDistance = nearestEnemyDistance()   
    if enemyDistance > 0 then
        hero:say(enemyDistance)
    else
        hero:shield(nearestFriend())    
    end 
end

A short solution without defining a function,to move on…

while true do
   local enemy = hero:findNearestEnemy()
   if enemy then
        local enemyD = hero:distanceTo(enemy)
        if enemyD > 0 then
        hero:say(enemyD)
        else   
        end 
    else
    end
end