Add customer user data via code?

Is there a way to add custom user data via code instead of the scene editor ? I see a get and set user data function but no addUserData function.

Thanks

To add user data you call setUserData. What would an addUserData do differently?

Yeah, write your own struct-like class - a data capsule which holds a collection, then set this as user data, then add / remove sub-elements to that collection. I think you can add anything, so making your own data collection capsule should be quite straight forward. It would be a simple struct-like class holding for example a List or Set or Map.
EDIT: damned, clicked delete instead of edit ^^

I was under the impression it worked like a List which has a set , get and add function. I didn’t realize it added on the set method if the data wasn’t present.

spatial.setUserData("information", information);

Shouldn’t this set a new data class called information if one is not present ?

No, I think it’s like a Map of (String,Savable).

If that’s not what you want, then writing your own Savable which holds any collection you desire is an option - that’s what I proposed.

@Ogli 'm actually rewriting some code. I’m experimenting with custom controls to see how much of a game engine can be completed using the scene editor. My old code used customer user data tags to tell the engine how to handle different spatials.

I could use the getControl method but this requires reworking so much code and I’m not entirely sure if the is the way I want to go yet but so far the custom controls do look very promising.

Ah okay, now I know how to type pointy brackets:

Map<String, Savable>

Still not perfect, but at least it works. ^^

And now the default statement for that topic which is still missing:
In a sophisticated jME app, the Spatials should only be for visual presentation stuff.
The data should not be stored in userData but rather in a sophisticated system.
One of the sophisticated systems that experienced coders use: Entity systems (ES).

Here is an example of my custom user data structure that I use to define my space assets in the scene composer. This will give you an idea of how to create a complex user data structure. I have designed my space game so that the entire game is defined in the scene composer. Including the network logic (but that’s another topic) :slight_smile:

/**
*

  • @author Zissis
    */
    public class AssetInfo implements Savable, Cloneable, java.io.Serializable {

    private String ownerGuid;

    @Override
    protected Object clone() throws CloneNotSupportedException {
    AssetInfo clone = (AssetInfo) super.clone();
    return clone;
    }

    public AssetInfo() {
    setGuid(new RandomGUID(true).toCompressedString());
    }
    private int shield = 0;

    private float mass = 0f;

    private float moveSpeed = 0f;

    private float rotationSpeed = 0f;

    @Override
    public String toString() {
    return name + " [" + (getType() == null ? “” : getType().getLabel()) + “]”;
    }

    private String allianceGuid;

    /**

    • @return the type
      */
      public Type getType() {
      return type;
      }

    /**

    • @param type the type to set
      */
      public void setType(Type type) {
      this.type = type;
      }
      private String guid;

    /**

    • @return the guid
      */
      public String getGuid() {
      return guid;
      }

    /**

    • @param guid the guid to set
      */
      public void setGuid(String guid) {
      this.guid = guid;
      }

    /**

    • @return the name
      */
      public String getName() {
      return name;
      }

    /**

    • @param name the name to set
      */
      public void setName(String name) {
      this.name = name;
      }

    /**

    • @return the health
      */
      public int getHealth() {
      return health;
      }

    /**

    • @param health the health to set
      */
      public void setHealth(int health) {
      this.health = health;
      }

    /**

    • @return the shield
      */
      public int getShield() {
      return shield;
      }

    /**

    • @param shield the shield to set
      */
      public void setShield(int shield) {
      this.shield = shield;
      }

    /**

    • @return the allianceGuid
      */
      public String getAllianceGuid() {
      return allianceGuid;
      }

    /**

    • @param allianceGuid the allianceGuid to set
      */
      public void setAllianceGuid(String allianceGuid) {
      this.allianceGuid = allianceGuid;
      }

    /**

    • @return the ownerGuid
      */
      public String getOwnerGuid() {
      return ownerGuid;
      }

    /**

    • @param ownerGuid the ownerGuid to set
      */
      public void setOwnerGuid(String ownerGuid) {
      this.ownerGuid = ownerGuid;
      }

    /**

    • @return the mass
      */
      public float getMass() {
      return mass;
      }

    /**

    • @param mass the mass to set
      */
      public void setMass(float mass) {
      this.mass = mass;
      }

    /**

    • @return the moveSpeed
      */
      public float getMoveSpeed() {
      return moveSpeed;
      }

    /**

    • @param moveSpeed the moveSpeed to set
      */
      public void setMoveSpeed(float moveSpeed) {
      this.moveSpeed = moveSpeed;
      }

    /**

    • @return the rotationSpeed
      */
      public float getRotationSpeed() {
      return rotationSpeed;
      }

    /**

    • @param rotationSpeed the rotationSpeed to set
      */
      public void setRotationSpeed(float rotationSpeed) {
      this.rotationSpeed = rotationSpeed;
      }

    public static enum Type {

     OrbitalPath,
     Orbit,
     SolarSystem {
                 public String getLabel() {
                     return "Solar System";
                 }
             },
     Planet,
     Star,
     Astroid,
     Fleet,
     Ship,
     Raw,
    
     Weapon,
     Shield,
     SpaceStation {
                 public String getLabel() {
                     return "Space Station";
                 }
             },
     Sector,
     CameraNode,
     UNKNOWN;
    
     public String getLabel() {
         return toString();
     }
    

    }
    private Type type = Type.UNKNOWN;

    private String name;
    private int health = 0;

    @Override
    public void write(JmeExporter ex) throws IOException {
    OutputCapsule out = ex.getCapsule(this);
    out.write(getType(), “t”, null);
    out.write(getGuid(), “g”, null);
    out.write(getName(), “n”, null);
    out.write(getHealth(), “h”, 0f);
    out.write(getShield(), “s”, 0f);
    out.write(getAllianceGuid(), “ag”, null);
    out.write(getOwnerGuid(), “og”, null);
    out.write(getMass(), “m”, 0f);
    out.write(getMoveSpeed(), “ms”, 0f);
    out.write(getRotationSpeed(), “rs”, 0f);
    }

    @Override
    public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    setType(ic.readEnum(“t”, Type.class, null));
    setGuid(ic.readString(“g”, null));
    setName(ic.readString(“n”, null));
    setHealth(ic.readInt(“h”, 0));
    setShield(ic.readInt(“s”, 0));
    setAllianceGuid(ic.readString(“ag”, null));
    setOwnerGuid(ic.readString(“og”, null));
    setMass(ic.readFloat(“m”, 0f));
    setMoveSpeed(ic.readFloat(“ms”, 0f));
    setRotationSpeed(ic.readFloat(“rs”, 0f));
    }
    }

1 Like