简体   繁体   English

如何使用 Boolean 比较动作<int></int>

[英]How to Use a Boolean to Compare Action<int>

I have a method as below我有一个方法如下

public void RequestTPCost(Action<int> callback, Action<int> enemyCallback)
 {
        if (OnTPSelectionNeeded == null)
        {
            callback?.Invoke(0);
            enemyCallback?.Invoke(0);
            return;
        }
        if (TurnSystem.Instance.IsPlayerTurn())
        {
            //Player turn use shooting unit is selected and target is enemy
            OnTPSelectionNeeded?.Invoke(this, new TPSelectionInfo()
            {
                callback = callback,
                enemyCallback = enemyCallback,
                TPAvailable = selectedUnit.GetTacticalPoints(),
                enemyTPAvailable = targetUnit.GetTacticalPoints(),
            });
        }
        else
        {
            OnTPSelectionNeeded?.Invoke(this, new TPSelectionInfo()
            {
                callback = callback,
                enemyCallback = enemyCallback,
                TPAvailable = targetUnit.GetTacticalPoints(),
                enemyTPAvailable = selectedUnit.GetTacticalPoints()
            });
        }
    }

and I use it elsewhere like this我像这样在其他地方使用它

private void UnitActionSystem_OnTPSelectionNeeded(object sender, UnitActionSystem.TPSelectionInfo e)
    {
        maxTP = e.TPAvailable;
        maxEnemyTP = e.enemyTPAvailable;
        callback = e.callback;
        enemyCallback = e.enemyCallback;
        TPAmount = 1;
        enemyTPAmount = 0;
    }

public void TPConfirm()
    {
        callback?.Invoke(TPAmount);
        UnitActionSystem.Instance.GetSelectedUnit().
                SpendTacticalPoints(TPAmount);
        enemyTPAmount = Random.Range(0, (maxEnemyTP + 1));
        enemyCallback?.Invoke(enemyTPAmount);
        UnitActionSystem.Instance.GetTargetUnit().
                SpendTacticalPoints(enemyTPAmount);
    }
private void NextState()
{
    switch (state)
    {
        case State.Aiming:
            state = State.Choosing;
            stateTimer = 0.1f;
            UnitActionSystem.Instance.RequestTPCost(ConfirmTPCost, 
            ConfirmEnemyTPCost);
            break;
        case State.Choosing:
            state = State.Shooting;
            float shootingStateTime = 0.1f; //Not a frame
            stateTimer = shootingStateTime;
            break;
        case State.Shooting:
            state = State.Cooloff;
            float coolOffStateTime = 0.5f;
            stateTimer = coolOffStateTime;
            Debug.Log("Shooting");
            break;
        case State.Cooloff:
            ActionComplete();
            break;
    }
}

private void ConfirmTPCost(int value)
{
    Debug.Log($"TP = {value}");

    NextState();
}

private void ConfirmEnemyTPCost(int value)
{
    Debug.Log($"EnemyTP = {value}");

    //NextState();
}

Now I want to check if ConfirmTPCost < ConfirmEnemyTPCost but am not sure how to simply do that.现在我想检查是否ConfirmTPCost < ConfirmEnemyTPCost但我不确定如何简单地做到这一点。

I have a roundabout way using events and more UnityActionSystem functions to call the instance and work through that but it seems very clunky and prone to breaking.我有一种迂回的方式使用事件和更多UnityActionSystem函数来调用实例并完成它,但它看起来非常笨重并且容易损坏。 Is there a better way to get these values and check which is more?有没有更好的方法来获取这些值并检查哪个更多?

var cost = 0;
var enemyCost = 0;
UnitActionSystem.Instance.RequestTPCost(i => cost = i, i => enemyCost = i);
Debug.Log($"TP = {cost}");
Debug.Log($"EnemyTP = {enemyCost}");
if (cost < enemyCost)
{
    //Do your stuff here
}

As said it sounds like there is actually some asynchronous parts involved.如前所述,听起来实际上涉及一些异步部分。

So you either will want to properly implement async and await patterns and wait for both values to be available.因此,您要么想要正确地实现asyncawait模式,要么等待这两个值都可用。

Or alternatively you could somewhat fake that behavior using eg nullables and a local function ike或者,您可以使用例如可空值和本地 function ike 在某种程度上伪造该行为

int? cost = null;
int? enemyCost = null;

UnitActionSystem.Instance.RequestTPCost(i => 
{
    cost = i;
    if(enemyCost != null)
    {
        Validate();
    }
}, i =>
{
    enemyCost = i;
    if(cost != null)
    {
        Validate();
    }
});

// Can be a local function 
// otherwise pass in the two int values via parameters  
void Validate()
{
    Debug.Log($"TP = {cost.Value}");
    Debug.Log($"EnemyTP = {enemyCost.Value}");
    if (cost.Value < enemyCost.Value)
    {
        //Do your stuff here
    }
}

Idea here: No matter which of both callbacks fires first, it is ignored and only for the second one both values are already available and the actual handler method can be invoked这里的想法:无论两个回调中的哪个先触发,它都会被忽略,只有第二个回调的两个值都已经可用并且可以调用实际的处理程序方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM