输出参数的函数
out a function which out's parameters
我试图通过在他们调用的函数中添加响应函数来让其他开发人员更轻松。我无法真正解释我在这里想要实现的目标,但我以前见过它,所以我知道这是可能的。
我想要的结果是:
InventoryController.Instance.AddItemToInventory(0, 5, out (InventoryOperations operation) =>
{
switch (operation)
{
case InventoryOperations.Success:
Debug.Log("Successfully added the item!");
break;
case InventoryOperations.InventoryIsFull:
Debug.Log("Inventory is full!");
break;
}
});
他们调用的函数是:
/// <summary>
/// Adds the item times the amount to the inventory.
/// </summary>
/// <param name="itemID">Item I.</param>
/// <param name="amount">Amount.</param>
public void AddItemToInventory(int itemID, int amount, out OnTaskComplete onTaskComplete)
{
onTaskComplete = _onTaskComplete;
//if we have the item, stack it
if(itemsInInventory.ContainsKey(itemID))
{
//increment it
itemsInInventory[itemID] += amount;
//successfully return the operation
_onTaskComplete(out InventoryOperations.Success);
}
else
{
if(itemsInInventory.Count < maxInventorySpaces)
{
itemsInInventory.Add(itemID, amount);
_onTaskComplete(out InventoryOperations.Success);
}
else
{
_onTaskComplete(out InventoryOperations.InventoryIsFull);
}
}
}
这行不通。我只是尝试创建一个委托并输出参数以及委托本身。但是没用。
感谢 Lee,我所要做的就是使用
System.Action
所以新代码是:
InventoryController.Instance.AddItemToInventory(0, 5, (InventoryOperations operation) =>
{
switch(operation)
{
case InventoryOperations.Success:
Debug.Log("Successfully added the item!");
break;
case InventoryOperations.InventoryIsFull:
Debug.Log("Inventory is full!");
break;
}
});
而其背后的功能是:
/// <summary>
/// Adds the item times the amount to the inventory.
/// </summary>
/// <param name="itemID">Item I.</param>
/// <param name="amount">Amount.</param>
public void AddItemToInventory(int itemID, int amount, Action<InventoryOperations> OnComplete)
{
//if we have the item, stack it
if(itemsInInventory.ContainsKey(itemID))
{
//increment it
itemsInInventory[itemID] += amount;
//successfully return the operation
OnComplete(InventoryOperations.Success);
}
else
{
if(itemsInInventory.Count < maxInventorySpaces)
{
itemsInInventory.Add(itemID, amount);
OnComplete(InventoryOperations.Success);
}
else
{
OnComplete(InventoryOperations.InventoryIsFull);
}
}
}
要到达你想去的地方,你真的不需要使用输出参数。只是 return 来自方法的值:
public InventoryOperations AddItemToInventory(int itemId, int amount)
否则直接传入函数(不带关键字):
InventoryController.Instance.AddItemToInventory(0, 5, (InventoryOperations operation) =>
{
switch (operation)
{
// do something here
}
});
public void AddItemToInventory(int itemID, int amount, Action<InventoryOperations> onTaskComplete)
{
var result = do some business logic here;
if(result == successful)
onTaskComplete(InventoryOperations.Success);
else
onTaskComplete(InventoryOperations.InventoryIsFull);
}
自由的伪代码,但你应该明白了。
我试图通过在他们调用的函数中添加响应函数来让其他开发人员更轻松。我无法真正解释我在这里想要实现的目标,但我以前见过它,所以我知道这是可能的。
我想要的结果是:
InventoryController.Instance.AddItemToInventory(0, 5, out (InventoryOperations operation) =>
{
switch (operation)
{
case InventoryOperations.Success:
Debug.Log("Successfully added the item!");
break;
case InventoryOperations.InventoryIsFull:
Debug.Log("Inventory is full!");
break;
}
});
他们调用的函数是:
/// <summary>
/// Adds the item times the amount to the inventory.
/// </summary>
/// <param name="itemID">Item I.</param>
/// <param name="amount">Amount.</param>
public void AddItemToInventory(int itemID, int amount, out OnTaskComplete onTaskComplete)
{
onTaskComplete = _onTaskComplete;
//if we have the item, stack it
if(itemsInInventory.ContainsKey(itemID))
{
//increment it
itemsInInventory[itemID] += amount;
//successfully return the operation
_onTaskComplete(out InventoryOperations.Success);
}
else
{
if(itemsInInventory.Count < maxInventorySpaces)
{
itemsInInventory.Add(itemID, amount);
_onTaskComplete(out InventoryOperations.Success);
}
else
{
_onTaskComplete(out InventoryOperations.InventoryIsFull);
}
}
}
这行不通。我只是尝试创建一个委托并输出参数以及委托本身。但是没用。
感谢 Lee,我所要做的就是使用
System.Action
所以新代码是:
InventoryController.Instance.AddItemToInventory(0, 5, (InventoryOperations operation) =>
{
switch(operation)
{
case InventoryOperations.Success:
Debug.Log("Successfully added the item!");
break;
case InventoryOperations.InventoryIsFull:
Debug.Log("Inventory is full!");
break;
}
});
而其背后的功能是:
/// <summary>
/// Adds the item times the amount to the inventory.
/// </summary>
/// <param name="itemID">Item I.</param>
/// <param name="amount">Amount.</param>
public void AddItemToInventory(int itemID, int amount, Action<InventoryOperations> OnComplete)
{
//if we have the item, stack it
if(itemsInInventory.ContainsKey(itemID))
{
//increment it
itemsInInventory[itemID] += amount;
//successfully return the operation
OnComplete(InventoryOperations.Success);
}
else
{
if(itemsInInventory.Count < maxInventorySpaces)
{
itemsInInventory.Add(itemID, amount);
OnComplete(InventoryOperations.Success);
}
else
{
OnComplete(InventoryOperations.InventoryIsFull);
}
}
}
要到达你想去的地方,你真的不需要使用输出参数。只是 return 来自方法的值:
public InventoryOperations AddItemToInventory(int itemId, int amount)
否则直接传入函数(不带关键字):
InventoryController.Instance.AddItemToInventory(0, 5, (InventoryOperations operation) =>
{
switch (operation)
{
// do something here
}
});
public void AddItemToInventory(int itemID, int amount, Action<InventoryOperations> onTaskComplete)
{
var result = do some business logic here;
if(result == successful)
onTaskComplete(InventoryOperations.Success);
else
onTaskComplete(InventoryOperations.InventoryIsFull);
}
自由的伪代码,但你应该明白了。