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:

ballA = Ball(x,y,w,h)


function Ball:printOut(a,b)
print("a", a, "b", b)

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()

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

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

Robot = class()

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

function Robot:speak(firstword,secondword)

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

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

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)