Class GameObject

java.lang.Object
net.risingworld.api.worldelements.GameObject
Direct Known Subclasses:
Area3D, Light, Model, PostProcessingVolume, Prefab, Text3D

public class GameObject extends Object
"Empty" base class for all custom elements. You can still use this class to set up parent/child hierarchies, and you can also assign colliders to it (which may act as invisible barriers, interaction areas or triggers).
Example: Spawn GameObject and a child
1//Example to create a game object
2GameObject gameObject = new GameObject();
3
4//Assign a box collider (size: 2x2x2) to it
5gameObject.setCollider(new BoxCollider(2f, 2f, 2f));
6
7//Set position of the game object
8gameObject.setLocalPosition(2000f, 90f, -2000f);
9
10//Register to player (so it shows up for this player)
11player.addGameObject(gameObject);
12
13
14//Now we want to create a child object
15GameObject child = new GameObject();
16
17//The position is always relative to the parent
18child.setLocalPosition(0f, 5f, 0f);
19
20//Add child to parent object. NOTE: No need to add this object to
21//the player as well, because this is handled automatically
22gameObject.addChild(child);
  • Constructor Details

    • GameObject

      public GameObject()
      Creates a new, empty game object using the DEFAULT layer.
    • GameObject

      public GameObject(int layer)
      Creates a new, empty game object using the provided layer.
      Parameters:
      layer - the layer you want to assign. See Layer
  • Method Details

    • getID

      public int getID()
      Gets the unique ID of this object. Note that this ID is only valid during the session!
      Returns:
      the unique ID of this object.
    • getPluginID

      public int getPluginID()
      Gets the ID of the plugin which created this element.
      Returns:
      the plugin ID.
    • isDisposed

      public boolean isDisposed()
      Gets whether or not this game object is disposed.
      Returns:
      true if this game object is disposed, false if not.
    • setActive

      public void setActive(boolean set)
      Toggles the active state of this object. Setting this to false will disable the object in the game scene, i.e it is no longer visible and no longer collides with the player or the world. This automatically affects all child elements as well.
      By default, an object is set to active.
      Parameters:
      set - true to set the object active, false to disable it.
    • isActive

      public boolean isActive()
      Gets whether or not this object is currently active. Does not check the hierarchy recursively! If the parent of this element is not active, but childs are active, they are treated as non-active elements by the game, but this method would still return true.
      Returns:
      true if this element is set to active, false if not.
    • setLayer

      public void setLayer(int layer)
      Sets the Layer for this game object.
      Parameters:
      layer - the layer you want to set.
      See Also:
    • getLayer

      public int getLayer()
      Gets the layer that is assigned to this game object.
      Returns:
      the layer.
      See Also:
    • setLocalPosition

      public void setLocalPosition(Vector3f position)
      Sets the position of this object, relative to its parent. If this object has no parent, this is the world position.
      Parameters:
      position - a vector which holds the new position coordinates.
    • setLocalPosition

      public void setLocalPosition(float x, float y, float z)
      Sets the position of this object, relative to its parent. If this object has no parent, this is the world position.
      Parameters:
      x - the x position (horizontally).
      y - the y position (vertically).
      z - the z position (horizontally).
    • getLocalPosition

      public Vector3f getLocalPosition()
      Gets the local position of this object, relative to its parent. If this object has no parent, this is the world position.
      Returns:
      the local position, represented as a Vector3f.
    • setLocalRotation

      public void setLocalRotation(Quaternion rotation)
      Sets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Parameters:
      rotation - the quaternion which represents the new rotation.
    • setLocalRotation

      public void setLocalRotation(float pitch, float yaw, float roll)
      Sets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Parameters:
      pitch - the pitch value (rotation around X axis).
      yaw - the yaw value (rotation around Y axis, i.e heading).
      roll - the roll value (rotation around Z axis).
    • getLocalRotation

      public Quaternion getLocalRotation()
      Gets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Returns:
      a quaternion which represents the current rotation.
    • setLocalScale

      public void setLocalScale(Vector3f scale)
      Sets the local scale of this object, relative to its parent. If this object has no parent, this is the world scale.
      Parameters:
      scale - the new scale along the X, Y and Z axis.
    • setLocalScale

      public void setLocalScale(float x, float y, float z)
      Sets the local scale of this object, relative to its parent. If this object has no parent, this is the world scale.
      Parameters:
      x - the scale along the X axis.
      y - the scale along the Y axis.
      z - the scale along the Z axis.
    • getLocalScale

      public Vector3f getLocalScale()
      Gets the current local scale of this game object.
      Returns:
      a Vector3f representing the local scale of the object (along the X, Y and Z axis).
    • moveToLocalPosition

      public void moveToLocalPosition(Vector3f position, float speed)
      Smoothly moves this object to a target local position.
      Parameters:
      position - the new target position.
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      Example: Move game object 20 blocks upwards within 5 seconds
      1Prefab prefab = new Prefab(PrefabAsset.loadFromAssetbundle(bundle, "Test.prefab"));
      2prefab.setLocalPosition(player.getPosition());
      3player.addGameObject(prefab);
      4
      5//New target position (20 blocks upwards)
      6Vector3f targetPosition = prefab.getLocalPosition().add(0f, 20f, 0f);
      7
      8//Move to target. It should take 5 seconds ("20 * 5" is the speed factor then)
      9prefab.moveToLocalPosition(targetPosition, 20 * 5);
    • moveToLocalPosition

      public void moveToLocalPosition(float x, float y, float z, float speed)
      Smoothly moves this object to a target local position.
      Parameters:
      x - the target x position (horizontally).
      y - the target y position (vertically).
      z - the target z position (horizontally).
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
    • moveToLocalPosition

      public void moveToLocalPosition(Vector3f position, float speed, Callback<Consumer<Vector3f>> callback)
      Smoothly moves this object to a target local position.
      Parameters:
      position - the new target position.
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      callback - optional callback that gets invoked every tick (while the object is moving). This enables you to provide a new position through the Consumer object (see Consumer.accept(java.lang.Object) method).
    • moveToLocalPosition

      public void moveToLocalPosition(float x, float y, float z, float speed, Callback<Consumer<Vector3f>> callback)
      Smoothly moves this object to a target local position.
      Parameters:
      x - the target x position (horizontally).
      y - the target y position (vertically).
      z - the target z position (horizontally).
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      callback - optional callback that gets invoked every tick (while the object is moving). This enables you to provide a new position through the Consumer object (see Consumer.accept(java.lang.Object) method).
    • rotateToLocalRotation

      public void rotateToLocalRotation(Quaternion rotation, float speed)
      Smoothly rotates this object to a target local rotation.
      Parameters:
      rotation - the new target rotation.
      speed - the speed at which the object should rotate.
    • moveToLocalTransform

      public void moveToLocalTransform(Vector3f position, Quaternion rotation, float speed)
      Smoothly moves and rotates this object to a target local position and rotation.
      Parameters:
      position - the new target position.
      rotation - the new target rotation.
      speed - the speed at which the object should move and rotate.
    • readWorldPosition

      public void readWorldPosition(Player player, Callback<Vector3f> callback)
      Reads the actual world position of the game object from a particular player. Once the result is available, the callback is invoked (containing the world position of the element as Vector3f).
      This method is only useful if this object is attached to a target or child of a parent: in this case, the local position (see getLocalPosition()) only returns the local position relative to the parent (or attach target), which makes it difficult to calculate the actual world position, so you can use this method instead to get the correct world position easily. However, if the element is neither attached to a parent nor an attach target, you can use getLocalPosition() instead.
      Parameters:
      player - the player you want to read the information from. If null, the position is read from the first player this game object is currently linked to. If this game object is not linked with any players yet, an exception is thrown.
      callback - callback that is invoked once the result is available from the client.
    • readWorldRotation

      public void readWorldRotation(Player player, Callback<Quaternion> callback)
      Reads the actual world rotation of the game object from a particular player. Once the result is available, the callback is invoked (containing the world rotation of the element as Quaternion).
      This method is only useful if this object is attached to a target or child of a parent: in this case, the local rotation (see getLocalRotation()) only returns the local rotation relative to the parent (or attach target), which makes it difficult to calculate the final world rotation, so you can use this method instead to get the correct world rotation easily. However, if the element is neither attached to a parent nor an attach target, you can use getLocalRotation() instead.
      Parameters:
      player - the player you want to read the information from. If null, the rotation is read from the first player this game object is currently linked to. If this game object is not linked with any players yet, an exception is thrown.
      callback - callback that is invoked once the result is available from the client.
    • attachTo

      public void attachTo(Player player, GameObject.AttachTarget target)
      Attaches the game object to a player. The game object will then follow the target automatically with no delay. The currently set local position and rotation is kept as offset - so when attaching the element, you may want to update the local position (or set it to 0 0 0) etc.
      Parameters:
      player - the player you want to attach this element to.
      target - the attach target, i.e where it should be attached to specifically.
    • attachTo

      public void attachTo(Npc npc, GameObject.AttachTarget target)
      Attaches the game object to an npc.
      Parameters:
      npc - the npc you want to attach this element to.
      target - the attach target, i.e where it should be attached to specifically.
      Example: Attach a plant to an npc head
      1//In our example, we simply use the nearest npc to our player
      2Npc npc = World.findNearestNpc(player.getPosition());
      3
      4//Only continue if an npc was found (result was not null)
      5if (npc != null) {
      6 //Get npc definition
      7 Npcs.NpcDefinition def = npc.getDefinition();
      8 System.out.println("Attaching object to npc " + def.name + " (id: " + npc.getGlobalID() + ")");
      9
      10 //We want to attach a dandelion to our npc. To get the
      11 //asset path, we can get this from a plant definition
      12 Plants.PlantDefinition plantDef = Definitions.getPlantDefinition("dandelion");
      13
      14 //Load prefab asset from game files (use assetpath from definition)
      15 PrefabAsset prefabAsset = PrefabAsset.loadFromGame(plantDef.assetpath);
      16
      17 //Create new prefab and assign a prefab asset
      18 Prefab prefab = new Prefab();
      19 prefab.setPrefab(prefabAsset);
      20
      21 //Set layer to IGNORE_RAYCAST (so it doesn't collide with anything)
      22 prefab.setLayer(Layer.IGNORE_RAYCAST);
      23
      24 //We want to attach the prefab to the npc head. However, depending on the npc model,
      25 //the head may have a specific rotation. To take this into account, we can use
      26 //the head rotation from the npc definition (it contains the default rotation of the ncp head).
      27 //We want to "remove" the default head rotation from our rotation, so we have to calculate the
      28 //inverse of the head rotation. If we apply it to our game object, it will be properly aligned to the head
      29 Quaternion headRotationInv = def.headrotation.inverse();
      30 prefab.setLocalRotation(headRotationInv);
      31
      32 //We also want to add a small offset. Again, we have to multiply the offset vector
      33 //with the inverse head rotation
      34 prefab.setLocalPosition(headRotationInv.multLocal(new Vector3f(0f, 0.35f, 0.2f)));
      35
      36 //Attach prefab to the npc head
      37 prefab.attachTo(npc, GameObject.AttachTarget.Head);
      38
      39 //Remember to add the game object to the player
      40 player.addGameObject(prefab);
      41}
    • isAttached

      public boolean isAttached()
      Gets whether or not the game object is currently attached to a target (e.g a player or npc etc).
      Returns:
      true if the element is attached (via one of the attachTo() methdos), false if not.
    • setCollider

      public void setCollider(Collider collider)
      Assigns a collider to this game object.
      Parameters:
      collider - the new collider. Set to null to remove the collider.
      Example: Add a box collider with size 2x2x2 to a game object
      1//Create a new game object
      2GameObject go = new GameObject();
      3
      4//Assign new box collider (size: 2x2x2)
      5go.setCollider(new BoxCollider(2f, 2f, 2f));
      6
      7//Set position of game object
      8go.setLocalPosition(2000f, 90f, -2000f);
      9
      10//Register to player
      11player.addGameObject(go);
    • getCollider

      public Collider getCollider()
      Gets the collider that was assigned via setCollider(net.risingworld.api.collider.Collider). Returns null if no collider was assigned.
      Returns:
      the collider assigned to this game object, or null if no collider has been assigned yet.
    • setColliderVisible

      public void setColliderVisible(boolean set)
      A debug method to visualize the collider of this game object. Only works if a collider is assigned.
      Parameters:
      set - true to show a debug visualization of this game object, false to hide it.
    • isColliderVisible

      public boolean isColliderVisible()
    • addComponent

      public void addComponent(String component)
    • removeComponent

      public void removeComponent(String component)
    • setComponentEnabled

      public void setComponentEnabled(String component, boolean enabled)
    • setComponentProperty

      public void setComponentProperty(String component, String property, Object value)
      Assigns a value to any component property via reflection.
      Parameters:
      component - the name/type of the component (e.g "Animator" if you want to access an animator component).
      property - the name of the property you want to change.
      value - the new value you want to assign.
    • invokeComponentMethod

      public void invokeComponentMethod(String component, String method, Object... parameters)
    • addChild

      public void addChild(GameObject child)
    • removeChild

      public void removeChild(GameObject child)
      Removes a child from this object. It will also be removed from the player world.
      Parameters:
      child - the child object you want to remove.
    • removeAllChilds

      public void removeAllChilds()
      Removes all children from this element. They will also be detached from the player UI.
    • getParent

      public GameObject getParent()
      Gets the current parent UI element, or null if this element has no parent.
      Returns:
      the parent element, or null if no parent is set.
    • removeFromParent

      public void removeFromParent()
      Removes this object from its parent. Same as calling removeChild() on the parent object.
    • getChilds

      public List<GameObject> getChilds()
      Gets an unmodifiable list of all children of this object.
      Returns:
      a new, unmodifiable list containing all child objects.
    • getChildCount

      public int getChildCount()
      Gets the number of attached child objects on this object.
      Returns:
      the amount of children this object has. 0 if this object has no child objects.
    • setAttribute

      public void setAttribute(String key, Object value)
      Stores an attribute (user data) for the game object. You can store any type. Note that all attributes will be reset upon server restart!
      Parameters:
      key - the name of the attribute.
      value - the value/object you want to store. Set null to remove the particular attribute.
    • getAttribute

      public <T> T getAttribute(String key)
      Gets the value to which the specified key is mapped, or null if no attribute is stored for that key.
      Type Parameters:
      T - type/class of the attribute.
      Parameters:
      key - the name of the attribute.
      Returns:
      the attribute that is stored for that key, or null of the key does not exists.
      Example:
      1boolean isMarked = "marked";
      2gameObject.setAttribute("marked", isMarked);
      3
      4//... somewhere else in your code ...
      5
      6if (gameObject.hasAttribute("marked", Boolean.class)) {
      7 boolean marked = player.getAttribute("marked");
      8}
    • hasAttribute

      public boolean hasAttribute(String key)
      Gets whether or not the provided attribute exists.
      Parameters:
      key - the name of the attribute.
      Returns:
      true if the attribute exists, false if not.
    • hasAttribute

      public boolean hasAttribute(String key, Class type)
      Gets whether or not the provided attribute exists (also checks if it has a specific type).
      Parameters:
      key - the name of the attribute.
      type - the type of the attribute
      Returns:
      true if the attribute exists and if it has the desired type, false if not.
      Example: Check if a certain Boolean attribute is stored
      1if (gameObject.hasAttribute("MyAttribute", Boolean.class)) {
      2 boolean value = getAttribute("MyAttribute");
      3 System.out.println("Attribute value: " + value);
      4}
    • deleteAttribute

      public void deleteAttribute(String key)
      Removes an attribute. Does nothing if the attribute doesn't exist.
      Parameters:
      key - the name of the attribute you want to delete.
    • getAttributes

      public Set<Map.Entry<String,Object>> getAttributes()
      Gets a set containing all keys and attributes.
      Be careful when modifying the set, because that affects the attributes.
      Returns:
      a set containing all keys and attributes.
      Example: Print all keys and their values
      1var attributes = gameObject.getAttributes();
      2for (var entry : attributes) {
      3 System.out.println("Entry " + entry.getKey() + " = " + entry.getValue());
      4}