Good news! To provide faster support and grow the community, we'll be changing the forum to "Read Only" and turn to Discord instead for support.
Join here: https://discord.gg/zjNj5zZ

How to auto-consume

altug
Posts: 4
Joined: Sun Feb 05, 2017 10:01 pm

How to auto-consume

Postby altug » Mon Jul 24, 2017 6:51 pm

I have experience consumables that can be dropped by enemies, so I need them to auto-consumed after picked up automaticly. Is there a way to do that?

User avatar
PeterBibinin
Posts: 194
Joined: Mon Oct 24, 2016 10:55 pm
Location: Poland
Contact:

Re: How to auto-consume

Postby PeterBibinin » Thu Jul 27, 2017 12:57 pm

Customs item type. That's your way ;)

There is on Pickup action so in this place you need to consume item.
I don't have time to make that but look in my Photon Integration. There is item type. And I use NotifyItemEquiped to access to other script when I equip item. You can also make something like this but in your way just add experience and Drop(false on ground) ‹- there is bool for drop from inventory but no to ground. Or something like this.
Waiting for sunrise
-------------------------------------------
Try make world beter ;]

My Advanced Save System tutorial with screen capture like in The Forest !
Advanced Save System
-------------------------------------------
BAZINGA !

User avatar
PeterBibinin
Posts: 194
Joined: Mon Oct 24, 2016 10:55 pm
Location: Poland
Contact:

Re: How to auto-consume

Postby PeterBibinin » Thu Jul 27, 2017 1:03 pm

Or... Just in pickup methode use Use(); when item has been picked up the automatically Use() this.

Maybe this can work. But there is one problem. If inventory is full this can't be done bcz you first add to inventory and then consume.

Leave items and make custom script where OnTriggerEnter() get access to player experience script and add experience.
Waiting for sunrise
-------------------------------------------
Try make world beter ;]

My Advanced Save System tutorial with screen capture like in The Forest !
Advanced Save System
-------------------------------------------
BAZINGA !

Shipright
Posts: 68
Joined: Tue Jul 12, 2016 7:24 am

Re: How to auto-consume

Postby Shipright » Thu Jul 27, 2017 7:13 pm

Hey, I use a similar system but they are "studied" via a timer to get experience rather than consumed on pickup. Joris helped me with this over a year ago now, its a start for your purposes.

Code: Select all

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Devdog.General;
using Devdog.InventoryPro;

/// <summary>
/// This item is a timed item that is only used and then destroyed when the timer expires.
/// EX. You study a scroll for twenty minutes after which it is consumed and gives you experiance.
/// </summary>

public partial class CuriostityItemType : InventoryItemBase
{
    /// <summary>
    /// When the item is used, play this sound.
    /// </summary>
    public AudioClipInfo audioClipWhenUsed;


    public float expirationTime = 10f;
    public InventoryItemBase afterTimerItem;

    protected ITimerHelper timer;
    private int _timerHandle;

    protected void Awake()
    {
        if (afterTimerItem != null)
        {
            timer = TimerUtility.GetTimer();

            // Start a timer, after expirationTime. After that OnTimerExpired will be called.
            _timerHandle = timer.StartTimer(expirationTime, OnTimerExpired);
        }
    }

    protected void OnDestroy()
    {
        timer.StopTimer(_timerHandle); // When this object is destroyed stop the timer
    }

    // Called when the timer has expired adn it's time to swap out the item.
    protected void OnTimerExpired()
    {
        if (itemCollection != null)
        {
            Use();
        }
        else
        {
            // Item is in the world (not in a collection), maybe completely destroy it and re-create it at this spot?
            var inst = UnityEngine.Object.Instantiate<InventoryItemBase>(afterTimerItem);
            inst.currentStackSize = currentStackSize;
            inst.transform.SetParent(transform.parent);
            inst.transform.localPosition = transform.localPosition;
            inst.transform.localRotation = transform.localRotation;
            inst.transform.localScale = transform.localScale;

            // Destroy the original (this object)
            Destroy(gameObject);
        }
    }

    public override void NotifyItemUsed(uint amount, bool alsoNotifyCollection)
    {
        base.NotifyItemUsed(amount, alsoNotifyCollection);
        PlayerManager.instance.currentPlayer.inventoryPlayer.stats.SetAll(stats);
    }

}


You could make this work right away by just setting the timer to a very small increment and putting all your experiance items into a dedicated player inventory window that is never actually called to the viewable canvass. They will appear off screen for an instant before expiring and being used.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 5 guests