从订阅的处理程序事件排队

时间:2019-07-29 10:09:01

标签: c# queue handler

我有一个脚本,该脚本附加到掉落在地上的物品上,以查找该物品的库存编号何时更改,因此我在拿起物品后运行该事件,因此当有人捡起它时,我想运行一个函数,将其添加到队列中,以便以后做其他事情。

我已经取消了对destroy的订阅(因此,当物品被拿起并销毁时,它会从处理程序中取消订阅)

队列:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnjinCreateItemQueue : MonoBehaviour
{
    //When a Blockchain Item drops, it automatically sets the tokenNameP/ValueP,MeltFeep etc.
    //and then adds the item to the queue, then CreateItemOnDrop starts and creates the item and mints it
    //to the players wallet along with the URI. Queue keeps going every time the phpFunctions queueInUse is false
    //and if items to create exist.

    public static EnjinCreateItemQueue staticClass;

    public string tokenNameP;
    public string tokenValueP;
    public int tokenMeltFeeP;
    public string tokenFeeValueP;

    private void Awake()
    {
        staticClass = this;
    }

    struct ItemDetails
    {
        public string tokenName;
        public string tokenValue;
        public int tokenMeltFee;
        public string tokenFeeValue;
    }

    enum ItemCreation
    {
        createItem
    }

    private ItemDetails localItemDetails;
    Queue localItemQueue = new Queue();

    public void AddItemtoQueue()
    {
        localItemDetails.tokenName = tokenNameP;
        localItemDetails.tokenValue = tokenValueP;
        localItemDetails.tokenMeltFee = tokenMeltFeeP;
        localItemDetails.tokenFeeValue = tokenFeeValueP;
        localItemQueue.Enqueue(localItemDetails);

        //foreach (object obj in localItemQueue)
        //{
        //    Debug.Log(obj.ToString());
        //}
        StartCoroutine(CreateItemFromQueue());
    }

    IEnumerator CreateItemFromQueue()
    {
        yield return new WaitUntil(() => phpFunctions.phpClass.queueInUse == false);


        phpFunctions.phpClass.TokenName = localItemDetails.tokenName;
        phpFunctions.phpClass.TokenValue = localItemDetails.tokenValue;
        phpFunctions.phpClass.TokenMeltFee = localItemDetails.tokenMeltFee;
        phpFunctions.phpClass.TokenFeeValue = localItemDetails.tokenFeeValue;        
        CreateItemOnDrop.staticClass.CreateOnDrop();
        localItemDetails = (ItemDetails)localItemQueue.Dequeue();
    }
    private void LateUpdate()
    {
        Debug.Log(localItemQueue.Count);
        Debug.Log(localItemDetails.tokenName);
    }


}

事件处理程序:

public class EnjinDrop : MonoBehaviour
{
    public int orkItemId;
    public ItemDropType itemDropType;
    [Header("Create Item")]
    public string tokenName;
    public string tokenValue;
    public int tokenMeltFee;
    public string tokenFeeValue;



    // Start is called before the first frame update
    void Start()
    {

        ORK.Game.ActiveGroup.Leader.Inventory.ContentChanged += InventoryChangedCreate;



    }

    public void InventoryChangedCreate(Inventory inventory, ItemDropType type, int id, int level, int quantity)
    {
        if (type == itemDropType && id == orkItemId)
        {
            EnjinCreateItemQueue.staticClass.tokenNameP = tokenName;
            EnjinCreateItemQueue.staticClass.tokenValueP = tokenValue;
            EnjinCreateItemQueue.staticClass.tokenMeltFeeP = tokenMeltFee;
            EnjinCreateItemQueue.staticClass.tokenFeeValueP = tokenFeeValue;
            EnjinCreateItemQueue.staticClass.AddItemtoQueue();
        }
    }

    private void OnDestroy()
    {
        ORK.Game.ActiveGroup.Leader.Inventory.ContentChanged -= InventoryChangedCreate;
    }
}

现在,如果地面上有多个放置,那么当我拾取一个放置中的一个放置时,在同一处理程序中订阅了多个实例,它会将地面中的所有对象排队,例如,如果我在地面上有4个放置然后我拿起1,队列的计数器变为4,然后为每次拿起加1,所以最终我得到了4滴7的队列。

我想不出解决办法

0 个答案:

没有答案