Custom Player component

Modified on Fri, 11 Oct 2019 at 09:27 AM

The Player component represents the character player and its attributes (life, hit damage, inventory, etc.). This component must provide a basic set of events, methods and properties called "Player API".


The Player component is a MonoBehaviour that inherits from IVoxelPlayPlayer interface. This component must be attached to the character controller and represent the active character player. When Voxel Play starts, it will look for a Voxel Play Player component attached to the character controller.


The IVoxelPlayPlayer interface provides a few basic useful methods like events for picking up items (when player approach an item, Voxel Play will send an event to your player component to notify it). The Player API is also responsible for providing data about the current player hit range, hit damage, etc.


In Voxel Play 5.4 and above you can customize or create your own player component.


Creating your own Player component


By default, Voxel Play provides a Voxel Play Player component. To customize, replace or extend the default player API you can:


1) Modify the VoxelPlayPlayer script. However if you update Voxel Play in the future, you will lose your changes when this file gets replaced.


2) Create a VoxelPlayPlayer class and inherit from the VoxelPlayPlayer class provided by Voxel Play. All base methods are marked as virtual so you can replace them with your own implementations.


3) Create a whole new class that implements IVoxelPlayPlayer interface.


In any case, remember the VoxelPlayPlayer component must be attached to the character controller so Voxel Play can find it and use it at runtime.



The IVoxelPlayPlayer interface


This interface provides inventory and combat related methods:


        event OnPlayerInventoryEvent OnSelectedItemChange;
        event OnPlayerGetDamageEvent OnPlayerGetDamage;
        event OnPlayerIsKilledEvent OnPlayerIsKilled;


The above events must be exposed by all Voxel Play Player components so other scripts can be notified when your character changes active item in the inventory, gets damage or is killed. The Voxel Play Player class is responsible for raising these events.


        void AddInventoryItem (ItemDefinition [] newItems);
        bool AddInventoryItem (ItemDefinition newItem, float quantity = 1);
        void PickUpItem (ItemDefinition newItem, float quantity = 1);
        void UnSelectItem ();
        bool SetSelectedItem (int itemIndex);
        bool SetSelectedItem (InventoryItem item);
        bool SetSelectedItem (VoxelDefinition vd);
        InventoryItem GetSelectedItem ();
        List<InventoryItem> items { get; }
        int selectedItemIndex { get; set; }
        List<InventoryItem> GetPlayerItems ();
        bool HasItem (ItemDefinition item);
        InventoryItem ConsumeItem ();
        void ConsumeItem (ItemDefinition item);
        float GetItemQuantity (ItemDefinition item);


These methods implement basic inventory functionality. Feel free to replace them or extend them in your own class.


        void DamageToPlayer (int damagePoints);
        float GetHitDelay ();
        float GetHitRange ();
        int GetHitDamage ();
        int GetHitDamageRadius ();


These methods implement basic combat related functionality. They provide data to other scripts. For example, when you call RayHit method from your character controller, it will determine the amount of damage from the player component.



Examples


You can find the default VoxelPlayPlayer component in VoxelPlay/Scripts folder. The IVoxelPlayPlayer interface can be found in VoxelPlay/Scripts/Private/Player folder.





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