forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic Function Calls

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
JC



Joined: 02 Jun 2006
Posts: 16

PostPosted: Wed Aug 09, 2006 4:45 pm    Post subject: Dynamic Function Calls Reply with quote

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
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Wed Aug 09, 2006 4:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
daurnimator



Joined: 11 Dec 2005
Posts: 38
Location: melbourne, australia

PostPosted: Wed Aug 09, 2006 5:00 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
daurnimator



Joined: 11 Dec 2005
Posts: 38
Location: melbourne, australia

PostPosted: Wed Aug 09, 2006 5:13 pm    Post subject: Reply with quote

damn, beat me ;)
Back to top
View user's profile Send private message MSN Messenger
JC



Joined: 02 Jun 2006
Posts: 16

PostPosted: Wed Aug 09, 2006 5:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Thu Aug 10, 2006 4:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group