簡體   English   中英

未分配的局部變量問題

[英]Unassigned local variable issue

我一直在制作 RTS 游戲,並且剛剛開始編寫代碼來定義某些單位的基本統計數據。 代碼中的所有內容都是正確的,我使用了正確的名稱空間和鏈接等等。 在未指定使用術語unit的情況下,一個問題不斷出現。 我已經分配了我的 BasicUnit 術語來定義我的單位類型,即步兵、工人等。在我的返回 function 中,我開始輸入單位統計信息。 return (unit.cost, unit.attack) 出於某種原因,這一行中的第一個unit術語顯然未分配。 我在整個腳本中多次使用該術語,並且沒有任何問題。 但正是這一行特別說明了第一個unit項是未分配的。 不管是 10 unit.stat還是 1,第一個總是帶下划線的。 請幫我。

namespace LP.FDG.Units
{
    public class UnitHandler : MonoBehaviour
    {

        public static UnitHandler instance;

        [SerializeField]
        private BasicUnit worker, infantry, archer, cavalry, priest;

        private void Start()
        {

            instance = this;

        }

        public (int cost, int attack, int health, int range, int size, int speed, int healing) GetBasicUnitStats(string type)
        {

            BasicUnit unit; // <--------
            switch (type)
            {
                case "worker":
                    unit = worker;
                    break;

                case "infantry":
                    unit = infantry;
                    break;

                case "priest":
                    unit = priest;
                    break;

                case "archer":
                    unit = archer;
                    break;

                case "cavalry":
                    unit = cavalry;
                    break;
            }

            **return (unit.cost, unit.attack, unit.healing, unit.health, unit.range, unit.speed, unit.size);**

        }

    public void SetBasicUnitStats(Transform type)
        {

            foreach(Transform child in type)
            {
                foreach (Transform unit in child)
                {

                    string unitName = child.name.Substring(0, child.name.Length - 1).ToLower();
                    var stats = GetBasicUnitStats(unitName);
                    Player.PlayerUnit pU;
                    
                    if(type == FDG.Player.PlayerManager.instance.playerUnits)
                    {
                        pU = unit.GetComponent<Player.PlayerUnit>();

                        pU.cost = stats.cost;
                        pU.attack = stats.attack;
                        pU.health = stats.health;
                        pU.healing = stats.healing;
                        pU.size = stats.size;
                        pU.speed = stats.speed;
                        pU.range = stats.range;
                    }
                    else if (type == FDG.Player.PlayerManager.instance.enemyUnits)
                    {

                    } 
                }
            }

        }

    }
}

問題是您的 switch 語句不能保證它將unit設置為任何東西。 例如,就編譯器而言,“horsey”的值將不匹配任何內容,因此未分配單元。 即使可能知道編譯器不會出現這種情況。

處理這個問題的方法是在你的 switch 語句中添加一個default分支。 在這種情況下,您可以拋出一個ArgumentException說明該類型無效。 它永遠不應該擊中這個默認分支(如果你的代碼的 rest 是正確的),但它會滿足編譯器,因為如果沒有分配單元,它將不再可能到達你的返回(如果你擊中默認分支,你正在拋出一個例外,所以不要進入違規行)。

代碼看起來像這樣:

switch (type)
{
    case "worker":
        unit = worker;
        break;
    /* All your other case statements still stay here */
    default:
        throw new ArgumentException($"Unrecognised unit type: {type}", "type");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM