JavaScript Help for setting z position

I am working on a terrain generator for a game and cant find a z parameter for 3d objects in the documentation. Does anyone know how to change this value?

Is it setZ()?

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.RuntimeObject3D.html

no it throws an error and idk why

How are you using it? Make sure to include the [0] for the object.

This works for me with an object named “Player” and a scene variable “VariableZ”

const ZZZ = runtimeScene.getVariables().get("VariableZ").getAsNumber();
/** @type {gdjs.Model3DRuntimeObject} */
const obj = runtimeScene.getObjects("Player");
obj[0].setZ(ZZZ);

hmm I’ll try that Keith_1357

TypeError: Cannot read properties of undefined (reading ‘setZ’)

The code works for me with 1 object.

Can you post your events? Make sure all of the object names match exactly including case.

ok the javascript

if(runtimeScene.getOnceTriggers().triggerOnce("javaScript2")){
    function worldGen(X, Y, Z, object){
        let obj = runtimeScene.createObject(object)
        X *= 20
        Y *= 20
        let savedY = Y
        let savedZ = Z
        while(X >= 0){
            obj.setPosition(X, Y); 
            Y = savedY
            while(Y >= 0){
                obj.setPosition(X, Y); 
                Y -= 20; 
                
            }
            X -= 20;
        }
        
    }
    worldGen(20, 20, 20, "Grass");  
}

In that context, it doesn’t seem to need brackets.

I tested your events in the racing template and it works.

My version (I used random because I wanted an easy way to test different values)

if(runtimeScene.getOnceTriggers().triggerOnce("javaScript2")){
    function worldGen(X, Y, Z, object){
        /** @type {gdjs.RuntimeObject3D} */
        let obj = runtimeScene.createObject(object)
        //X *= 20
        //Y *= 20
        
        let savedY = Y
        let savedZ = Z
        while(X >= 0){
            obj.setPosition(X, Y); 
            Y = savedY
            while(Y >= 0){
                obj.setPosition(X, Y); 
                Y -= 20; 
                obj.setZ(Math.random()*200)
            }
            X -= 20;
        }
        
    }
    worldGen(300, 300, 20, "Player"); 
}

I triggered the function with a key released event in my scene.

ya its still not working. rq could you add the Walk3D, Jump3D, and Collision3D extensions? because that might be interfering with the Z pos thing

wait the event change z position event works so if I can execute that with the js then that might work

I don’t think those extensions would cause an error. They might interfere and push back.

hmm i dont know i will try to debug tomorrow b/c i gtg

YES I got it to work!!!

1 Like

Glad to hear. What eventually worked for you.

Maybe it’s just a small code to test, but it can be easily done with events.

Yes it may be, but I am better at JavaScript than Gdevelop events : |

the working JavaScript

if(runtimeScene.getOnceTriggers().triggerOnce("javaScript2")){
    function worldGen(X, Y, Z, object){
        let obj = runtimeScene.createObject(object)
        X *= 20
        Y *= 20
        let savedY = Y
        let savedZ = Z
        while(X >= 0){
            Z = Math.floor(Math.random() * savedZ)
            obj = runtimeScene.createObject(object)
            obj.setPosition(X, Y);
            obj.setZ(Z)
            Y = savedY
            while(Y >= 0){
                Z = Math.floor((Math.random() * 20) * savedZ) / 2
                obj = runtimeScene.createObject(object)
                obj.setPosition(X, Y);
                obj.setZ(Z)
                Y -= 20; 
                
            }
            X -= 20;
        }
        
    }
    worldGen(20, 20, 2, "Grass");  
}

1 Like