getChunk method not working (Blocks Library)

Hello, I was having trouble with the getChunk method because it didn’t return anything although there was a chunk at these coordinates. Could anyone explain to me how it works?

It is hard to tell without knowing more info.

What do you mean by “not work”?

What do you pass to the method?

Is there an exception or stack trace?

Please show the code.

My guess is that you are passing it a cell location instead of a chunk location.

2 Likes

The thing is that I call the method with a chunk location “chunkManager.getChunk(new Vec3i(0,0,0));” And it returns empty. But there is a chunk at that location because I use the chunk generator and the chunk is loaded.

Here is the code. If you need more just tell me.

    @Override
    public void simpleUpdate(float tpf) {
        //Camera mov
        camDir.set(cam.getDirection()).multLocal(0.6f);
        camLeft.set(cam.getLeft()).multLocal(0.4f);
        walkDirection.set(0, 0, 0);
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (up) {
            walkDirection.addLocal(camDir);
        }
        if (down) {
            walkDirection.addLocal(camDir.negate());
        }
        player.setWalkDirection(walkDirection);
        cam.setLocation(player.getPhysicsLocation());
        // get a chunk and apply collisions
        stateManager.getState(ChunkPagerState.class).setLocation(new Vector3f(cam.getLocation().x, 255, cam.getLocation().z));
        int CamX = (int)Math.floor(cam.getLocation().x);
        int CamY = (int)Math.floor(cam.getLocation().y);
        int CamZ = (int)Math.floor(cam.getLocation().z);
        Vec3i camPos = new Vec3i(CamX,CamY,CamZ);
        BulletAppState bulletAppState;
        ChunkManager chunkManager = new ChunkManager();
        chunkManager.initialize();
        Vec3i chunkPos = new Vec3i(chunkManager.getChunkLocation(camPos.toVector3f()));
        bulletAppState = stateManager.getState(BulletAppState.class);
        chunkManager.requestChunk(new Vec3i(0,0,0));
        Optional<Chunk> chunkOptional = chunkManager.getChunk(chunkPos);

        if(chunkOptional.isPresent()) {
            System.out.println("Present");
        }
        Chunk chunk;
        //System.out.println(chunkManager.getChunk(new Vec3i(1,0,1)));
        /*if (bulletAppState.isInitialized() && chunk != null) {
            System.out.println("Test");
            PhysicsRigidBody physicsChunk = new PhysicsRigidBody(new MeshCollisionShape(chunk.getCollisionMesh()), 0);
            physicsChunk.setPhysicsLocation(chunk.getWorldLocation());
            bulletAppState.getPhysicsSpace().addCollisionObject(physicsChunk);
        }*/

    }

I think you need to update ChunkManager by calling chunkManager.update() after calling chunkManager.requestChunk.

like that ?

@Override
    public void simpleUpdate(float tpf) {
        //Camera mov
        camDir.set(cam.getDirection()).multLocal(0.6f);
        camLeft.set(cam.getLeft()).multLocal(0.4f);
        walkDirection.set(0, 0, 0);
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (up) {
            walkDirection.addLocal(camDir);
        }
        if (down) {
            walkDirection.addLocal(camDir.negate());
        }
        player.setWalkDirection(walkDirection);
        cam.setLocation(player.getPhysicsLocation());
        // get a chunk and apply collisions
        stateManager.getState(ChunkPagerState.class).setLocation(new Vector3f(cam.getLocation().x, 255, cam.getLocation().z));
        int CamX = (int)Math.floor(cam.getLocation().x);
        int CamY = (int)Math.floor(cam.getLocation().y);
        int CamZ = (int)Math.floor(cam.getLocation().z);
        Vec3i camPos = new Vec3i(CamX,CamY,CamZ);
        BulletAppState bulletAppState;
        ChunkManager chunkManager = new ChunkManager();
        chunkManager.initialize();
        Vec3i chunkPos = new Vec3i(chunkManager.getChunkLocation(camPos.toVector3f()));
        bulletAppState = stateManager.getState(BulletAppState.class);
        chunkManager.requestChunk(chunkPos);
        chunkManager.update();
        Optional<Chunk> chunkOptional = chunkManager.getChunk(chunkPos);
        if(chunkManager.getChunk(chunkPos).isPresent()) {
            System.out.println("Present");
        }
        //System.out.println(chunkManager.getChunk(new Vec3i(1,0,1)));
        /*if (bulletAppState.isInitialized() && chunk != null) {
            System.out.println("Test");
            PhysicsRigidBody physicsChunk = new PhysicsRigidBody(new MeshCollisionShape(chunk.getCollisionMesh()), 0);
            physicsChunk.setPhysicsLocation(chunk.getWorldLocation());
            bulletAppState.getPhysicsSpace().addCollisionObject(physicsChunk);
        }*/

    }

because it still doesn’t work.

In case you need it here’s the chunk manager config

ChunkManager chunkManager = ChunkManager.builder()
                .generatorPoolSize(2)
                .meshPoolSize(2)
                .generator(new ChunkNoiseGenerator(System.currentTimeMillis()))
                .triggerAdjacentChunkUpdates(true)
                .build();

I think Chunk generation is done in a background thread, what you can do is to register a ChunkManagerListener into ChunkManager to get notified when a chunk is available.

How do I register that?

ChunkManager.addListener()

I can use the chunkManagerListener but it wouldn’t fit my needs. So, is there a way to use the getChunk method in the same thread?

I do not know. Sorry!

and is there another method to gert a chunk?

Is there a way to get the location of a chunk?

I think you need to go back to jME basics. You’re not using the update loop the way its meant to. Read up on it here: jMonkeyEngine 3 Tutorial (4) - Hello Update Loop :: jMonkeyEngine Docs - but I suggest you go back even further and look at tutorials first.

If I move the initialization process in the init method it throws an Illegal state exception because the chunk manager isn’t initialized.

Did you take a look to the examples in the Block framework, e.g. the endless runner https://github.com/rvandoosselaer/Blocks/blob/master/examples/src/main/java/com/rvandoosselaer/blocks/examples/EndlessRunner.java ?