calling a function from an argument of another function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
a7s1h1
Prole
Posts: 12
Joined: Sat May 25, 2013 2:15 pm

calling a function from an argument of another function

Post by a7s1h1 »

Is it possible to call a function from an argument of another function, like this: ?

Code: Select all

function love.load()
   tmr=4
end

-- function "timer" is called from love.update(dt): 
-- The function print('Hi') prints the word "Hi"
function love.update(dt)
     timer(tmr,print('Hi'),dt)
end

-- the timer counts back and when it reaches 0, the function print('Hi') should be performed from an argument "func"
function timer(timer,func,dt)
     if timer>0 then           
          timer=timer-1*dt   
           if timer<=0 then
                return func
           end
     end
     tmr=timer
end
User avatar
Starry
Prole
Posts: 5
Joined: Tue May 21, 2013 1:12 am
Location: The End of Time

Re: calling a function from an argument of another function

Post by Starry »

I'm gunna say yes but i'm not certain this is what you're asking. code follows

Code: Select all

function love.load()

	timerx = 4

	function timecount(x,y)
	love.graphics.print(x, 10, 10)
	love.graphics.print(y, 10, 20)
	end

	function testing()
		return "Hi"
	end

	 func = testing()

end

function love.update(dt)
bonzer = dt
end

function love.draw()
timecount(func, bonzer)
end
you can't call dt from draw without problems so I bonzered it to save time and reading, i got stuff to do today >_>

(I would think there is more to this since your return wouldn't always be a string...)
ed: timerx is supposed to be in there too but i forgot to put it back in after fixing dt..my bad
Jesus saves, everyone else takes full damage
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: calling a function from an argument of another function

Post by micha »

In Lua function are first class values. That means that you can call a function and give a function as an argument. See for example the generic sorting function in Lua.

However, "print('Hi')" is not a function. That is why your code would not work. "print" is a function. For your purpose you need some sort of function-factory, that makes a function, that does "print('Hi')".

Code: Select all

function factory(text)
  return function() print(text) end
end
(disclaimer: this is untested, maybe I forgot some brackets or something)
a7s1h1
Prole
Posts: 12
Joined: Sat May 25, 2013 2:15 pm

Re: calling a function from an argument of another function

Post by a7s1h1 »

function factory(text)
return function() print(text) end
end
Well, I see, I can set an argument of a function2 as an argument of a function1, which runs the function2. But what if the function does not have any arguments? How can I call it from another function? (The issue is not about printing function, as a matter of fact, it does not even matter, what shoul the function do, the question is how to make it perform)

It won't work, just to describe what I need:

Code: Select all

function1 (function2)
   <perform function2>
end
Or the larger example:

Code: Select all

if a=1 then
   func_1(func_2)
end

function func_1(func)
  <returns func(in that case func=func_2, so the function func_2() should be performed)>
end

function func_2()
   <does not matter what>
end
Of course, neither of these codes will work, but they describe what I'm trying to find.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: calling a function from an argument of another function

Post by micha »

I just realize, my example was overly complicated.

To your question: You can for example define a function, that displays some text, and that does not take any input, as follows:

Code: Select all

fun = function () print('foo') end
Now you have a function, called "fun". If you type

Code: Select all

fun()
It will be executed and display "foo".
And for a simple example of a function, that has a function as argument you could have this:

Code: Select all

function runFunctionIfPositive(a,f)
  if a>0 then
    f()
  end
end
This example is just for illustration. The function checks if a is positive and then runs the function you hand over to it. In this case, it is assumed that f has not input arguments.
a7s1h1
Prole
Posts: 12
Joined: Sat May 25, 2013 2:15 pm

Re: calling a function from an argument of another function

Post by a7s1h1 »

Now I understood! :awesome:
For example, if I create these two functions

Code: Select all

function print(text,x,y)
	love.graphics.setFont(font)
	love.graphics.print(text,x,y)
end

function performfunction(func,ar1,ar2,ar3)
   func(ar1,ar2,ar3)
end
Then calling performfunction(print,'It works!',300,200) will perform function print(text,x,y) and so print the text according to the related arguments.
That is easier than I thought - It appears that just I have to set every element of a function (it's name and each argument) as a separate argument of a function, which calls it.
Thanks a lot for the help! :awesome:
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: calling a function from an argument of another function

Post by Plu »

On a sidenote, I'm not sure it's a good idea to overwrite the "print" function as it's a basic part of Lua and LÖVE. Weird things might happen. Plus you won't be able to print to the console window anymore.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: calling a function from an argument of another function

Post by Robin »

a7s1h1 wrote:it's name and each argument
Note you're not passing function names, though: you're passing the actual function. You can do:

Code: Select all

performFunction(function(a,b,c) print("Wee", a, b, c) end, 1, 2, 3)
-- prints Wee     1       2       3
-- assuming you didn't bind the name "print" to another function, like you did in your sample
And you don't need to pass each argument to the function calling the other function, if you want it to be the same every time. You can do, for example:

Code: Select all

function performfunction(func,ar1,ar2)
   func(ar1,ar2,ar3, 200)
end
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests