Sign in to follow this  

[java] Function Pointer Technique.

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How one can used function pointer technique used in c++ through java? So that I can avoid lots of if- else structures for making a check. If I get particular object then I want to directly call a method associated with it. Please Help.

Share this post


Link to post
Share on other sites
So if I have say 10 obstacles in my game. And suppose my game is screen based and i know that in particular screen only two objects are there whose Identifier I know.

Suppose in c
10 obstacles
const int obst_1 = 0;
const int obst_2 = 1;
const int obst_3 = 2;
.....
const int obst_10 = 9;

and suppose I have arr1[10] array of pointers which contains pointers to different
ten functions which are used to handle specific obstacle collisions.

so if I am on screen number two and my game script gave me the information about that screen that obstacle 10 and 5 are present on this screen. then for collision and response I just want to used two function calls whithout any if else structure.

like instead of

chk_collision()

{
if(obst == obst_1)
then obst1_Collision();
else if(obst == obst_2)
then obst2_Collision();
.....

}
something like

chk_collision()
{
for(i=0;i<obst_num;i++)
{
*arr[obst_id[i]]();
}
}

where obst_id[] contains identifier for the obstacles present in the current screen.

So using java how to implement function pointers.

Share this post


Link to post
Share on other sites
In C++, you'd probably do that using virtual functions.

In Java, you can use interfaces for the same thing. Implement an interface with a single function, which is the method signature you want, and store a pointer to interface in your array. Then call that.

Note: you'd probably want to store the pointer to the obstacle, rather than the obstacle id. That way, you can call the method directly.

Share this post


Link to post
Share on other sites
Here is an interface:

public interface Environment {
//only method definitions here, no code
public void collision();
}


The your objects would implement this interface:

public class Axe implements Environment {
public void collision() {
//collision code for Axe
}
}
public class Broom implements Environment {
public void collision() {
//collision code for Broom
}
}


Then your array would be:

Environment myEnv[] = new Environment[10];
myEnv[0] = new Axe();
myEnv[1] = new Broom();
.
.
.
for(int i=0;i<myEnv.length;i++) {
myEnv[i].collision();
}



If you are using a script file, you can use Reflection to create objects from their name.(Don't use Reflection in speed critical code. It should be used only in load up times as it is slow.)

Share this post


Link to post
Share on other sites

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this