View Full Version : method parameters
Bonechilla
05-18-2009, 02:48 PM
is it possible to call a method into a parameter to be used in the consecutive method such as input.addAction(thisMove.move(Direction.FORWARD));
vrnunes
05-18-2009, 03:31 PM
In this case, it'll pass to input.addAction() the returning value from thisMove.move().
Lets say thisMove.move() returns a boolean, then input.addAction() must accept a boolean as parameter.
If you want to add the function call itself, you better creating a scripting language and interpreting it at runtime.
Hope this makes sense to you.
.oisyn
05-18-2009, 04:15 PM
If you want to add the function call itself, you better creating a scripting language and interpreting it at runtime.
That sounds a bit overkill, don't you think?
You could create an interface dubbed something like "Action", which has an execute() method. This interface can be implemented by a MoveAction, which takes a Direction as parameter. In the execute() method implementation, you simply call 'thisMove.move(direction)'. If the 'thisMove' object is not something you can get to from within the execute() method, you should obviously store that instance in the MoveAction as well... Anyway, the point is that input.addAction() takes an Action interface as parameter. That way, everyone can supply it's own Action implementation and thus allow you to add a method call indirectly. If you want to call the code in the Action, simply call it's execute() method.
starstutter
05-18-2009, 05:27 PM
basicly, yes
so long as the return values of the functions you are calling inside the parameters roughly match the data types being asked for.
ie :
bool funcA (void)
{
}
bool funcB (bool)
{
}
funcB( funcA() ); <----- this works
funcA( funcB() ); <----- this does not
In .oisyn's answer, it doesn't *have* to be the exact data type (for instance passing in a float for a double usually works fine), but its good practice to do so. Now passing something like a float in for a boolean... yeah, that's not a good idea.
.oisyn
05-19-2009, 02:19 AM
starstutter, I think you are misinterpreting both the question and my post. Well, at least the latter, I might be wrong about the former. What I think he wants is to have the actual method call passed to the addAction() method as some form of function pointer or functor. He is not asking whether he can use the return value of the call as parameter to another function.
Based on the small piece code I assume it is Java, and since Java does not support function objects or functors you have to mold it into an interface.
Bonechilla
05-19-2009, 03:51 AM
thx for the quick replies, .oisyn is right though I'm trying to have the actual method call passed to the addAction() method as a function pointer. It is java i'm using in fact it's the java Monkey Engine (http://www.jmonkeyengine.com/) that i'm using.
i'll try the interface way but thatnks for the replies
vrnunes
05-19-2009, 10:14 AM
Oh yes, the interface solution is better for this case.
alphadog
05-19-2009, 11:29 AM
Given Java as a limiting point (ie. no closures), you have two options:
1) .oisyn's suggestion of using an interface. An example in Java is java.lang.Runnable. You pass an object with a known interface.
2) Reflection. Pass a java.lang.reflect.Method, but that may be slow. See here for example: http://forums.sun.com/thread.jspa?threadID=270474&messageID=1422510
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.