Function Parameters in LUA

This seems like such an elementary question, but I seem to be missing something. I have an object with a function. I pass it two parameters, and the first one is always tossed. This only occurs on non-constructor functions.

For example:

Main.file
...
ballA = Ball(x,y,w,h)
ballA.printOut(10,20)
...

~

Ball.file
...
function Ball:printOut(a,b)
print("a", a, "b", b)
end
...

~
Output:
a 20 b nil

Something similar happens when I pass a vec2 as a parameter too. I only get the y value of the vector.

Have you created the Ball class? Look at this code below, which works fine, and see if you can find what you are missing.

function setup()
    myRobot = Robot()
    myRobot:speak("Hello","There")
    myRobot:move(vec2(50,200))
end

function draw()
    background(0, 0, 0, 255)
end

--You can put the class code below in a separate file if you like

Robot = class()

function Robot:init()
    print "new robot created."
end

function Robot:speak(firstword,secondword)
    print("1:",firstword,"2:",secondword)
end

function Robot:move(newLocation)
    print ("Moved. New location is " .. newLocation.x .. ", " .. newLocation.y)
end

I believe function Ball:printOut(a, b) (with a colon) is shorthand for

Ball["printOut"] = function (self, a, b) ... end

By using a dot rather than the colon, you are calling the function with only self set to 10 and a set to 20 (and b set as nil).

I found that the error was caused by creating my function with a “:” instead of a “.”.

Found that info here http://www.lua.org/pil/16.html

Still don’t really understand it though.

Oh, I didn’t refresh. Thanks for the pointers guys.

Actually, you should create the function with a colon : and then call it with a colon as well. You call it with BallA.printOut(10,20) but it should be BallA:printOut(10,20)