Can anybody explain why shortcuts doesn't work?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Can anybody explain why shortcuts doesn't work?

Post 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?
ferdielance
Prole
Posts: 6
Joined: Tue May 07, 2013 10:35 am

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

Post 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."
Last edited by ferdielance on Fri May 10, 2013 8:31 am, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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
When I write def I mean function.
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

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

Post by Kasperelo »

Aaah. Okay. Thank you!
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests