Abstract control not taking values in SDK

This is rehash of an old topic but I’m still having the same problem in 3.1. When I set a controls values using the SDK it doesn’t take. I’m probably doing something wrong so here is my code.

Control class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myGame;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;

/**
 *
 * @author me
 */
public class NewControl extends AbstractControl {
    //Any local variables should be encapsulated by getters/setters so they
    //appear in the SDK properties window and can be edited.
    //Right-click a local variable to encapsulate it with getters and setters.
    public int testing;

    public int getTesting() {
        return testing;
    }

    public void setTesting(int testing) {
        this.testing = testing;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        //TODO: add code that controls Spatial,
        //e.g. spatial.rotate(tpf,tpf,tpf);
    }
    
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //Only needed for rendering-related operations,
        //not called when spatial is culled.
    }
    
    public Control cloneForSpatial(Spatial spatial) {
        NewControl control = new NewControl();
        //TODO: copy parameters to new Control
        return control;
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule in = im.getCapsule(this);
        //TODO: load properties of this Control, e.g.
        //this.value = in.readFloat("name", defaultValue);
        this.testing = in.readInt("testing",getTesting());
    }
    
    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule out = ex.getCapsule(this);
        //TODO: save properties of this Control, e.g.
        //out.write(this.value, "name", defaultValue);
        out.write(this.testing,"testing",getTesting());
    }
    
}

I change the value of testing in the scene explorer to 75 then print it like so.

System.out.println(myWorld.getSpatial("player").getControl(NewControl.class).testing);

The output is always 0. It doesn’t seem to take. I appreciate any help.

You don’t copy the values in cloneForSpatial.

3.1 doesn’t use cloneForSpatial() anymore. If he’s really using 3.1 then this shouldn’t be an issue, I guess. (Unless the SDK is someone going around the new cloning stuff for some reason.)

normen made a nice tutorial. It exactly describes what you need.

No it doesn’t do anything special. Back to you engine guys ^^

Thanks , but I already watched that video and it doesn’t help. Does anyone have a working custom control for 3.1 they can paste the code so I can take a look at it ?

I’ve played with the clone for spatial and clone methods just like in the video and still nothing.

I am currently double checking this but I guess I found the error already:

What is the behavior of defaultValue in the Output Capsule?
If this.testing == defaultValue, don’t serialize.

Then in read(), it has nothing to read in, so it takes getTesting() as defaultValue, which in turn is zero.

So the defaultValue ONLY works, when using a constant value. Replace those two with 5 or leave out the defaultValue in write at all, so it’s always written.

I will edit my post when it works for me with constant default values, but it definately looks like this is wrong at least.

Edit: Yep works, should really be this issue.
Somtimes it also helps to crosscheck this in an own application (the engine can also save and load from j3o)

Yeah I figured that out last night after opening every single control class in the library and browsing the code. On the up side I have a much better understanding of how the other controls work now :slight_smile:

1 Like

if only more monkeys would follow your lead, I have learned so much just by digging through the code, it helps on so many levels