I have a class where I set up a BulletAppState and use it, then later I access the same variable from another class and suddenly it is null. I’m gonna be a bit annoyed if this is just a stupid mistake from my inexperience with java, but honestly that is what I expect.
First class (GameAppState)
[java]
public GameAppState(AppStateManager appStateManager, InputManager inputManager, AssetManager assetManager, Node rootNode, Camera cam) {
this.assetManager=assetManager;
this.rootNode=rootNode;
this.inputManager=inputManager;
this.appStateManager=appStateManager;
/** Set up Physics */
bulletAppState = new BulletAppState();
appStateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 10, 0));
//bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
public void addControl(Object obj) {
if(bulletAppState==null) {
System.out.println(“Null at addControl”);
}
bulletAppState.getPhysicsSpace().add(obj);
}
public BulletAppState getAppState() {
System.out.print("AppState returning.");
return bAppState;
}
[/java]
Second class (ChunkLoader),
Then in another class (I use the appstate on lines 50, 64, 120, and 122),
[java]package mygame;
import java.io.*;
import java.util.Arrays;
import java.util.Random;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Vector3f;
import com.jme3.scene.BatchNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.WireFrustum;
import java.util.Scanner;
/**
*
-
@author Isaac
*/
public class ChunkLoader {private float R = 1.01f;
private float W;
private float S;
private Double h;
private float H;
private float X;
private float Z;
private int o;
private int p;public BatchNode batch2;
private Spatial hexModel;
private RigidBodyControl landscape;
WireFrustum frustum;
Geometry frustumMdl;
private Vector3f[] points;
private AssetManager assetManager;
private Node rootNode;
private BulletAppState bulletAppState;
private GameAppState gAppState;
public ChunkLoader(AssetManager assetManager, Node rootNode) {
this.assetManager=assetManager;
this.rootNode=rootNode;gAppState = new GameAppState();
}
String[] hexBuild = new String[32768];
private int columnCounter;
private int rowCounter;
private int verticleCounter;
private Integer currentHex;
private String toStringVar = “0”;public void loadChunk(String fileName) throws IOException {
batch2 = new BatchNode("theBatchNode1"); bulletAppState = gAppState.getAppState(); columnCounter = 0; rowCounter = 0; verticleCounter = 0; String file = fileName; SystemSave chunkLoad = new SystemSave(); try { hexBuild = chunkLoad.gridBuilder(file); } catch(IOException a) { System.out.println("Chunk load failed."); } for(int l = 0; l<32768; l++) { rowCounter++; if(rowCounter == 63) { columnCounter++; rowCounter = 0; } if(columnCounter == 63) { verticleCounter++; columnCounter = 0; } toStringVar = hexBuild[l]; currentHex = Integer.parseInt(toStringVar); if(currentHex==1) { makeHex(); } } DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal()); rootNode.addLight(sun); batch2.batch(); rootNode.attachChild(batch2);
}
private void makeHex() {
hexModel = assetManager.loadModel("Models/hexfade_02/hexfade_02.j3o"); CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(hexModel); landscape = new RigidBodyControl(sceneShape, 0); batch2.attachChild(hexModel); hexLocation(); hexModel.setLocalTranslation(Z, (verticleCounter*.25f)+.01f, X); hexModel.addControl(landscape); if (bulletAppState==null) { System.out.println("bulletAppState null in ChunkLoader"); gAppState.getAppState(); } gAppState.addControl(landscape);
}
private void hexLocation() {
W = 2*R; S = 1.5f*R; h = Math.sqrt(3)*R; H = h.floatValue(); X = ((rowCounter))*S; Z = (((columnCounter))*H)+(((rowCounter))%2)*(H/2);
}
}
[/java]
I've tried changing my hierarchy so that they all inherit from my main method, and I've tried several arrangements of methods and code. Any help is appreciated.