Skip to main content
gamedev.net
🔒 Locked

[java] Function Pointer Technique.

Started by rohitgonsalves Jan 16, 2005 at 8:49 PM 4 replies 850+ views
Original Post
rohitgonsalves
rohitgonsalves
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.
MOV AL, 0x00MOV AL, ME
CaptainJester
CaptainJester
Please post the C++ code you are trying to emulate so we can help you better. Your explanation is not very clear.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
rohitgonsalves
rohitgonsalves
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 {
*arr[obst_id]();
}
}

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

So using java how to implement function pointers.

MOV AL, 0x00MOV AL, ME
hplus0603
hplus0603
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.
enum Bool { True, False, FileNotFound };
rohitgonsalves
rohitgonsalves
can you give me simple example, please?
MOV AL, 0x00MOV AL, ME
CaptainJester
CaptainJester
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.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.)
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.