| View previous topic :: View next topic |
| Author |
Message |
JC
Joined: 02 Jun 2006 Posts: 16
|
Posted: Wed Aug 09, 2006 4:45 pm Post subject: Dynamic Function Calls |
|
|
Hi Folks,
I not sure if what I am trying to do is possible, hopefully someone will be able to shed some light. I want to pass a function name to a second function and get the second function to call the first one.
eg:
| Code: | Main(SubMain,"blah")
function Main(pName,pstuff)
execute function (pName)
stuff........
end
function SubMain()
stuff here........
end |
Can it be done ?
TIA,
JC _________________ Remember, don't sweat the petty things
and don't pet the sweaty things....... |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Wed Aug 09, 2006 4:58 pm Post subject: |
|
|
Yes
| Code: | Variables are handled as functions.
So any variable can be passed off as a function
If it doesnt error out that is.
function Main(pName, pStuff)
if (type(pName) == "function") then
result = pName() -- result is whatever pName returns
else
-- Not a function!@#?!
end
print(result)
end
function MySub()
-- blah blah blah
return "value" -- returns back to main.
end |
For more information http://www.lua.org/manual/5.0/manual.html#2.5.8 |
|
| Back to top |
|
 |
daurnimator

Joined: 11 Dec 2005 Posts: 38 Location: melbourne, australia
|
Posted: Wed Aug 09, 2006 5:00 pm Post subject: |
|
|
why wouldn't you?
try: (code works)
| Code: |
function SubMain()
screen:print(0,0,"test",Color.new(255,0,0))
end
function Main(func)
while true do
screen:clear(Color.new(255,255,255))
func()
screen.flip()
end
end
Main(SubMain)
|
|
|
| Back to top |
|
 |
daurnimator

Joined: 11 Dec 2005 Posts: 38 Location: melbourne, australia
|
Posted: Wed Aug 09, 2006 5:13 pm Post subject: |
|
|
| damn, beat me ;) |
|
| Back to top |
|
 |
JC
Joined: 02 Jun 2006 Posts: 16
|
Posted: Wed Aug 09, 2006 5:23 pm Post subject: |
|
|
Wonderful. Thanks for the help (and the quick replies).
JC _________________ Remember, don't sweat the petty things
and don't pet the sweaty things....... |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Thu Aug 10, 2006 4:56 pm Post subject: |
|
|
| daurnimator wrote: | why wouldn't you?
try: (code works)
| Code: |
function SubMain()
screen:print(0,0,"test",Color.new(255,0,0))
end
function Main(func)
while true do
screen:clear(Color.new(255,255,255))
func()
screen.flip()
end
end
Main(SubMain)
|
|
No error-checking.
No way to terminate loop.
screen:clear() doesnt need to be called.
screen.flip() -- might not be needed.
Too much extra code and not enough explination.
Sorry I dont mean to nitpick at your code, but I thought it might help! |
|
| Back to top |
|
 |
|