ERROR - function arguments expected near '='

I’m fairly new to Codea and Lua, so this might be something that’s easy to fix, but I’ve looked around for an answer and I can’t find one. I have written the following code:

for k,touch in pairs(touches) do
        if Point(touch.x, touch.y).inside(Rect(0, 0, 110, 100)) then
            if #enemies ~= 0 then
                closest = {enemies[1].center().distance(Point(player.x, player.y)), enemies[0]}
                for enemy in enemies do
                    if enemy.center().distance(player.center()) < closest[1]:
                    c = enemy.center() --error on this line: function arguments expected near '='
                    d = c.distance(Point(player.x, player.y))
                    closest = {d, enemy}
                    end
                end
                lightning(closest[1].center().x, closest[1].center().y, player.x+player.w/2, player.y+player.h/2)
                closest[1].health = closest[1].health - 10
            end
        end
    end

Point, Rect, and everything in the table “enemies” are classes I’ve made. In the table “enemies”, all items are instances of the class Enemy, which has a method called center, which takes no arguments, and returns a Point class. The Point class has a method called distance, which takes another Point as the only argument. I don’t understand why this doesn’t work.

Note: This script was previously written in python. I’m trying to translate the program over into Codea. This section of the program had no problem running the way I had it set up in Python, so I think it must be some syntax specific to Lua, maybe? Again, I’m not very experienced.

Use ==

A single = is used to set the value of a variable, two = is used to test equality

oh god! I didn’t notice that all my code was on one line! I’ve fixed that now, and actually I’m trying to assign the variable c with the value that the center function returns, which is a Point class.

for enemy in enemies do isn’t correct syntax. Could be causing the error a few lines after

if i change it to

for i, enemy in enemies do 

is that correct syntax? The same error still happens

Nope. Are you trying to iterate through an array called enemies? Use pairs or ipairs, same as you’re doing for the touches table

Also, you have the incorrect syntax for an if statement: in Lua, it’s if x < y then ... end, you have it written as if x < y: ... end

Also, center().distance is wrong. You can’t have dot notation after a method call. What are you trying to do here? I think we need to see more code

@yojimbo2000 You can have a dot notation after a method call if the function returns something, but again, we can’t fix all the errors without seeing all the code. @DarthXander is there any way you could post the whole code? Lua is very different from Python and you would probably need to re-code it to get it right.

thanks for all your help =) and yes, here’s the whole code:


--# Main
function setup()
    for a = 1, #leveldict do
        keys = {"walls", "portal", "spikes"}
        for b, key in keys do
            for i = 1, #leveldict[a][key] do
                leveldict[a][key][i].y = leveldict[a][key][i].y + 100
            end
            leveldict[a]["spawn"][2] = leveldict[a]["spawn"][2] + 100
        end
    end
    leveldict = {beginning}
    level = 1
    enemies = {}
    animation = 3.0
    table.insert(enemies, Enemy(500, 500))
    peach = nil
    peach2 = nil
    max_speed = 7
    max_jump = 11
    touchy = false
    player = Rect(20, 620, 50, 90)
    move = {0, 0}
    reference = nil
    sprite = peach
    skiptime = true
    blood = {}
    touches = {}
    beginning = {["spawn"] = {20, 620},
           ["walls"] = {Rect(0, 500, 700, 20), Rect(700+player.w+10, 500, 600, 20), Rect(680, -100, 20, 600),               Rect(700+player.w+10, -100, 20, 600)},
           ["portal"] = {Rect(680, 0, 100, 1)},
           ["spikes"] = {},
           ["text"] = ""}
end
function draw()
    background(54, 54, 54, 255)
    fill(255, 255, 255, 255)
    for i = 1, #enemies do
        enemies[i]:update()
    end
    for i = 1, #leveldict[level]['walls'] do
        rect(leveldict[level]['walls'][i].x, leveldict[level]['walls'][i].y, leveldict[level]['walls'][i].w, leveldict[level]['walls'][i].h)
    end
    for i = 1, #leveldict[level]['spikes'] do
        spikey(leveldict[level]['spikes'][i])
    end
		fill(1.0, 0.141, 0.173)
--rect(900, 700, 50, 50)
		fill(0.217, 0.725, 0.276)
		tint(255, 255, 255, 255)
    if move[1] < 0 then
        scale(-1, 1)
        sprite("Documents:peach", player.x+player.w, player.y, player.w, player.h)
        scale(-1, 1)
    end
    if move >= 0 then
        sprite("Documents:peach", player.x, player.y, player.w, player.h)
    end
    if player.intersects(leveldict[level]['portal'][1]) then
        level = level + 1
        if level > #leveldict then
            level = 1
        end
        player.x, player.y = leveldict[level]['spawn'][0], leveldict[level]['spawn'][1]
        animation = 3.0
    end
    for i = 1, #leveldict[level]['spikes'] do
        if player.intersects(leveldict[level]['spikes'][i]) then
            for a = 0, 20 do
                table.insert(Drop(player.x+player.w/2, player.y))
            end
            for i = 1, #enemies do
                if enemies[i] == self then
                    table.remove(enemies, i)
                end
            end
        end
    end
    for i = 1, #leveldict[level]['walls'] do
        if Rect(player.x+move[1], player.y+.5, player.w, player.h-.5).intersects(leveldict[level]['walls'][i]) then
            if player.x < wall.x then
                player.x = wall.x-player.w-.5
                player.x = wall.x + wall.w+.5
            else
                move[1] = 0
            end
        end
        if Rect(player.x, player.y+move[2], player.w, player.h).intersects(leveldict[level]['walls'][i]) then
            if player.y < leveldict[level]['walls'][i].y then
                player.y = leveldict[level]['walls'][i].y-player.h-.5
            else
                player.y = leveldict[level]['walls'][i].y + leveldict[level]['walls'][i].h
                move[2] = 0
            end
        end
    end
    player.y = player.y + move[1]
    player.x = player.x + move[2]
    if player.y+player.h > HEIGHT then
        move[2] = 0
    end
    player.x = math.min(WIDTH - player.w, math.max(0, player.x))
    player.y = math.min(HEIGHT+20 - player.h, math.max(0, player.y))
    move[2] = math.max(move[2] - .5, -20)
    fill(0, 255, 0, math.max(math.min(255, animation), 0))
    animation = animation - 1
    fontSize(50)
    text(leveldict[level]['text'], 512, 384) --size 50
    a = 288
    fill(0,0,0)
    rect(0,0,1024,100)
    strokeWidth(1)
    stroke(129, 0, 255, 255)
    for x = 500+a, 550+a do
        line(x, (x-500-a)*(40.0/50)+50, x, -(x-500-a)*(40.0/50)+50)
    end
    for x = 650+a, 700+a do
        line(x, (x-700-a)*(40.0/50)+50, x, -(x-700-a)*(40.0/50)+50)
    end
    for x = 10, 90 do
        line(-(x-10)*(40.0/80)+150, 100-x, (x-10)*(40.0/80)+150, 100-x)
    end
    strokeWidth(3)
    ellipse(10, 10, 80, 80)
    line(5, 50, 20, 50)
    line(80, 50, 95, 50)
    line(50, 5, 50, 20)
    line(50, 80, 50, 95)
    fill(128, 0, 255, 255)
    fontSize(30)
    text("skip", 500, 50) -- size 30
    strokeWidth(0)
    for k,touch in pairs(touches) do
        if Point(touch.x, touch.y).inside(Rect(780, 0, 100, 100)) then
            move[1] = -max_speed
        end
        if Point(touch.x, touch.y).inside(Rect(900, 0, 100, 100)) then
            move[1] = max_speed
        end
        if Point(touch.x, touch.y).inside(Rect(110, 0, 190, 100)) then
            for wall in leveldict[level]['walls'] do
                if player.y == wall.y+wall.h and (player.x + player.w > wall.x and player.x < wall.x+wall.w) then
                    move[1] = max_jump
                end
            end
        end
       --[[
        if Point(touch.x, touch.y):inside(Rect(0, 0, 110, 100)) then
            if #enemies ~= 0 then
                closest = {enemies[1]:center():distance(Point(player.x, player.y)), enemies[1]}
                for i, enemy in pairs(enemies) do
                    if enemy:center():distance(player:center()) < closest[0] then
                    c = enemy:center()
                    d = c:distance(Point(player.x, player.y))
                    closest = {d, enemy}
                    end
                end
                lightning(closest[1].center().x, closest[1].center().y, player.x+player.w/2, player.y+player.h/2)
                closest[1].health = closest[1].health - 10
            end
        end
        --]]
        if Point(touch.x, touch.y).distance(Point(500, 50)) < 40 then
            if skiptime == true then
                level = level + 1
                if level > #leveldict then
                    level = 0
                end
                player.x, player.y = leveldict[level]['spawn'][1], leveldict[level]['spawn'][2]
                animation = 3.0
                skiptime = False
            end
        end
        if touch.y > 100 then
            lightning(touch.location.x, touch.location.y, player.x+player.w/2, player.y+player.h/2)
        end
    end
    for i, drop in blood do
        drop.x = drop.x + drop.vx
        drop.y = drop.y + drop.vy
        drop.vy = drop.vy - drop.g
        drop.color[3] = math.max(drop.color[3] - .02, 0)
        fill(unpack(drop.color))
        ellipse(drop.x, drop.y, drop.w, drop.h)
        table.remove(drop, i)
    end
end
			
function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
        skiptime = True
		move[1] = 0
    elseif touch.state == BEGAN then
        if touch.y > 100 then
			table.insert(enemies, Enemy(touch.x, touch.y))
        end
        touches[touch.id] = touch
    else
        touches[touch.id] = touch
        end
end

actually that’s just the Main tab, the rest won’t fit… and I think I’ve implemented your fixes

You know what? It was the if…then thing that I forgot to fix from when it was in python. Thanks for all your help!!! By the way, going back and looking at the code now, there’s a lot of things that need fixing; my code here is a huge mess. I’ll try and fix all of it and see if I still have any problems.