Page 1 of 1

LUA Unexpected Character Error

PostPosted: Tue May 28, 2013 18:40
by Defenestrator
Hi, I'm new to these forums, but not to modding mt or to lua.
I'm currently hunting for an "unexpected character" near the comma on the last line.
Relevant code:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node("cannon:cannon", {
    tiles={"cannon_cannon.png","cannon_cannon.png","cannon_cannon.png","cannon_cannon.png","cannon_cannon.png","cannon_cannon.png","cannon_front.png",},
    groups={cracky=1,level=3},
    paramtype2="facedir",
    on_rightclick=function(pos, node, clicker, itemstack)
        if (itemstack:get_name() == "cannon:ball_item") then
            if not minetest.setting_getbool("creative_mode") then
                itemstack:take_item()
            end
            local dir = node["param2"] / 4
            local obj = minetest.env:add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, "cannon:ball")
            local shootdir = {x=0,y=0,z=0}
            local rotation = math.pi * (90 * dir) / 180
            if (dir == 0) then
                shootdir.y = 1
            else if (dir == 1) then
                shootdir.z = 1
            else if (dir == 2) then
                shootdir.z = -1
            else if (dir == 3) then
                shootdir.x = 1
            else if (dir == 4) then
                shootdir.x = -1
            else
                shootdir.y = 1
            end
            obj:setvelocity({x=shootdir.x*19, y=shootdir.y*19, z=shootdir.z*19})
            obj:setacceleration({x=shootdir.x*-3, y=-10, z=shootdir.z*-3})
            obj:setyaw(rotation + math.pi)
            minetest.sound_play("cannon_fire", {pos=pos})
            return itemstack
        end
    end,
})


Thanks,
Defenestrator

PostPosted: Tue May 28, 2013 18:43
by 12Me21
Sometimes when it says "unexpected character" it means something else, like (example) you typed "end" too many times or something.

PostPosted: Tue May 28, 2013 18:53
by sfan5
Use elseif not else if

PostPosted: Wed May 29, 2013 05:00
by webdesigner97
I get this kind of error when the encoding of my file is UTF-8 instead of ANSI :/

PostPosted: Wed May 29, 2013 16:59
by Defenestrator
Thanks sfan. I think that would be my error. I feel rather derp right now :P