Page 1 of 1

Custom functions

Posted: Sun Dec 13, 2009 6:41 am
by Lazy Waffle
Is it possible to make my own functions? It's just to shorten certain things (find the distance between 2 sets of coordinates, etc.)

Re: Custom functions

Posted: Sun Dec 13, 2009 6:58 am
by TechnoCat
Of course!

Code: Select all

function love.load()
   local x, y, dis
   x = 10
   y = 5
   dis = math.sqrt(math.pow(x,2)+math.pow(y,2))
end
Is the same as

Code: Select all

function love.load()
   local dis
   dis = distance(10,5)
end

--Finds the distance from the origin to an (x,y) point.
function distance(x, y)
   return math.sqrt(math.pow(x,2)+math.pow(y,2))
end
I didn't actually test my distance function, but you probably get the function idea. :megagrin: