Jump to content


method parameters


7 replies to this topic

#1 Bonechilla

    New Member

  • Members
  • Pip
  • 6 posts

Posted 18 May 2009 - 09: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));



#2 imerso

    Senior Member

  • Members
  • PipPipPipPip
  • 428 posts
  • LocationBrasil

Posted 18 May 2009 - 10: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.

#3 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 18 May 2009 - 11:15 PM

vrnunes said:

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.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#4 starstutter

    Senior Member

  • Members
  • PipPipPipPip
  • 1039 posts

Posted 19 May 2009 - 12:27 AM

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.
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.
bunny also wants to fight spam: Click Here Bots!

#5 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 19 May 2009 - 09: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.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#6 Bonechilla

    New Member

  • Members
  • Pip
  • 6 posts

Posted 19 May 2009 - 10: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 that i'm using.

i'll try the interface way but thatnks for the replies

#7 imerso

    Senior Member

  • Members
  • PipPipPipPip
  • 428 posts
  • LocationBrasil

Posted 19 May 2009 - 05:14 PM

Oh yes, the interface solution is better for this case.

#8 alphadog

    DevMaster Staff

  • Moderators
  • 1641 posts

Posted 19 May 2009 - 06:29 PM

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.co...ssageID=1422510





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users