user673906
user673906

Reputation: 847

Lua 'end' expected to close 'if'

I have this code here in lua

function onMouseDown(event)
    print(event.x, event.y)
    if event.x>160 then
        print("what")
        return 1
        print("whatwhat") 
    else 
        return 0
        print("WhatWhatWhat")
    end


    fruit:setX(event.x)
    fruit:setY(event.y)
end

But it is moaning that main.lua:59: 'end' expected (to close 'if' at line 56) near 'print' so it is expecting an end at the end of my if below print("whatwhat"). But I put an end there and it still moans at me if I do that.

I am pretty new to lua and I am a bit confused, I normally program in c#

Upvotes: 3

Views: 2288

Answers (2)

Mankarse
Mankarse

Reputation: 40613

See the Lua manual §3.3.4:

The return statement can only be written as the last statement of a block. If it is really necessary to return in the middle of a block, then an explicit inner block can be used, as in the idiom do return end, because now return is the last statement in its (inner) block.

Your code has calls to print that occur after the return statement, and so is invalid.

Upvotes: 9

Anachronda
Anachronda

Reputation: 184

I suspect your problem is the print after the return. That's dead code; Lua wants returns at the end of a block.

Upvotes: 0

Related Questions