Page 1 of 1

Can anybody explain why shortcuts doesn't work?

Posted: Fri May 10, 2013 8:14 am
by Kasperelo
I had an idea that I could write

Code: Select all

function quad(x,y,w,h,sw,sh)
love.graphics.newQuad(x,y,w,h,sw,sh)
end
insted of writing

Code: Select all

love.graphics.newQuad()
every single time, so I could instead write quad(), not love.graphics.newQuad(). But it doesn't work. Why?

Re: Can anybody explain why shortcuts doesn't work?

Posted: Fri May 10, 2013 8:26 am
by ferdielance
That function doesn't return anything. So if you say:

Code: Select all

myQuad = quad(etc etc etc)
...after defining that function, it evaluates quad to nil.

Fortunately, .lua lets us treat functions like any other kind of data, so you don't need to go through so many hoops to make a convenient alias! Try this:

Code: Select all

quad = love.graphics.newQuad
myQuad = quad(etc etc etc etc)
Notice that you don't put parentheses in that first line, since you're referring to the function itself as data, not calling it.

EDIT: Of course, this kind of aliasing is reallllly handy for making markup-language style stuff:

Code: Select all

disp = table.moreTable.wowLotsOfTablesHere.myRidiculouslyLongwindedTextDisplayFunction
disp"Wow, that was terse."

Re: Can anybody explain why shortcuts doesn't work?

Posted: Fri May 10, 2013 8:28 am
by kikito
In other words, you are missing a return:

Code: Select all

function quad(x,y,w,h,sw,sh)
  return love.graphics.newQuad(x,y,w,h,sw,sh)
end
But it would work just the same if you defined quad like this:

Code: Select all

quad = love.graphics.newQuad

Re: Can anybody explain why shortcuts doesn't work?

Posted: Fri May 10, 2013 11:25 am
by Kasperelo
Aaah. Okay. Thank you!