Player API

Modified on Tue, 09 May 2023 at 08:19 AM

The Player API implements the IVoxelPlayPlayer interface which provides a basic interface. If you wish to provide your own player component, you can implement the interface and provide your own methods/properties. Read more about it here.


To access player info and modify its state:


using VoxelPlay;
public class MyScript : MonoBehaviour {
     void Start() {
          VoxelPlayPlayer.instance.totalLife = 5;
     }
}


Player class events

  • OnItemSelectedChanged: triggered when current selected item is changed by using selectedItemIndex property. Received new selected item index and also the previous selected item index.
  • OnPlayerGetDamage(ref int damageTaken, int remaningLifePoints): triggered just before player is getting damage by a voxel (ie. lava). Damage can be modified by writing to damageTaken.
  • OnPlayerIsKilled: triggered when the player's life reaches 0.
  • OnItemAdded: triggered when a new item is added to player inventory.
  • OnItemConsumed: triggered when an item is consumed.
  • OnItemsClear: triggered when all items are cleared, for example, when calling ConsumeAllItems()


Player class properties

  • playerName: an optional name for this player.
  • life: current life points.
  • totalLife: maximum life points.
  • hitDelay: delay between attacks.
  • hitRange: range for the attack.
  • hitDamage: amount of damage per hit.
  • hitDamageRadius: radius of the hit impact and damage (defaults to 1 voxel).
  • items: player inventory items. This public property will return all currently available items. If build mode is ON, this property will return all available items in the world, not only in player inventory.
  • playerItems: only player inventory items. While items can return all world items in build mode, this property will always return just the items in the original player inventory.
  • selecteditemIndex: returns or set the current item.


Player class methods


public void AddInventoryItem (ItemDefinition[] newItems)

Adds an item or items to player inventory.


public InventoryItem GetSelectedItem()

Returns a copy of the currently selected item. If none selected it returns InventoryItem.Null.
Use SetSelectedItem or selectedItemIndex property to set the current selected item.


public bool SetSelectedItem(...)

Sets the active item by index or by passing an InventoryItem.

public void UnSelectItem()

Unselected curren item.


public InventoryItem ConsumeItem()

Consumes one unit of currently selected item. If none selected it returns InventoryItem.Null.


public void ConsumeItem(ItemDefinition item)

Consume an item.


public void ConsumeAllItems()

Empties the player inventory.


public bool HasItem(ItemDefinition item)

Returns true if player has at least one unit of the item.


public bool GetItemQuantity(ItemDefinition item)

Returns the amount of the item the player has.


public int GetHitDamageRadius()

Returns the hit damage radius of current item.


public int GetHitDamage()

Returns the hit damage of current item.


public float GetHitRange()

Returns the hit range of current item.


public float GetHitDelay()

Returns the hit delay of current item (time delay between consecutive hits)


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article