Shooting Game: Reduce health points

I am creating a shooting game, and I want my targets to be removed from the scene every time a target is destroyed.

I created a target

Box box10 new Box(Vector3f.Zero, 1,1,1);

Geometry shootable1 = new Geometry(“Box”, box10);

shootable1.setUserData(“health”, 10);//give 10 health



Then, I am using collision to detect when the bullets hit the target

CollisionResults results = new CollisionResults();

bullet.collideWith(shootable1.getWorldBound(), results);



if (results.size() > 0){

//in this line I want to reduce the health of my targets by 1 every time a bullet hits it

//But I am getting an error message that says that it is a bad operand because the first

//is an object type and the second one is an int type.

shootable1.setUserData(“health”, shootable1.getUserData("health)-1);

}



Can any body give me an advice of what should I do in order to reduce the health of

my targets when they get hit?



Thanks

Just get the user data from it again:

Integer health = (Integer)shootable1.setUserData(“health”);

health -= damage;

if (health <= 0)

// remove object from seen with shootable1.removeFromParent();

1 Like

It sounds like you’ve hit a limitation in java’s autoboxing: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

1 Like

Just add a cast to Integer:

[java]shootable1.setUserData(“health”, (Integer) shootable1.getUserData(“health)-1);[/java]

Works fine under java 7, not sure about other versions.



The reason you have to cast to Integer is that Integer can be treated as primitive (autoboxing to int) and mathematical operations are supported for primitives only. Otherwise the return type is Object, and mathematical operations can not be applied to objects.

1 Like

Actally the return type is not defined (through a nifty trick by pspeed) so you always have to define it when its ambiguous. Integer a = spat.getUserData(“blah”); works without casting though.

1 Like
@normen said:
Actally the return type is not defined

'undefined' might not even be the correct term here (or is it? english is not my native language). The compiler autmatically infers the type in the same way as ist would infer the type when you type [java]ArrayList<String> foo = new ArrayList<>();[/java].

So, in your case, the compiler would infer the type to
[java]Integer a = spat.<Integer>getUserData(“blah”);[/java]

But you're absolutely correct, if it is ambiguos, you have to explicitly define it, otherways the compiler infers it for ya.
1 Like

Diamond notation <> is only supported in the latest version of Java, however yes your understanding matches mine.

1 Like
@zarch said:
Diamond notation <> is only supported in the latest version of Java, however yes your understanding matches mine.

Yeah, you're right, at least for the ArrayList example of mine, that works only with Java 7 (and higher) (i sometimes forget about that, since i use this feature heavily... :D). But the statement about type inference for methods with generic return types should be true for earlier java versions. too.

And, there is another way to do this (this post just gave me the idea):
[java]shootable1.setUserData(“health”, shootable1.<Integer>getUserData("health")-1);[/java]

I think this might even be the most "elegant" solution. At least I'd use it ^^
1 Like

Thanks for every body’s response…

I have created a method:

public void targetHit(){

CollisionResults results = new CollisionResults();

//Ray ray = new Ray(cam.getLocation(), cam.getDirection());

bullet.collideWith(target1.getWorldBound(), results);

Integer life = target1.getUserData(“health”);



if(results.size() > 0){

target1.setUserData(“health”, life -1);

if(life <= 0){

target1.removeFromParent();

}

}

}

This method checks when a bullet(sphere) collides with a target(cube). Each time the bullet hits the target it decrements the life of the target by 1. As soon as the target runs out of life it supposed to be removed from the scene. However, when I called this method from the update loop I get the following error:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at mygame.ShootingGame.targetHit(ShootingGame.java:763)

at mygame.ShootingGame.simpleUpdate(ShootingGame.java:630)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:244)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:662)

It’s a Null Pointer Exception. Basic Java coding skills should let you track it down.



(Hint, look at ShootingGame.java line 763, look for anything in there that could be null. Breakpoint or add logging to confirm and work out why it’s null)



Most likely you never attached a health user data to the object so the get is returning null.

1 Like