繁体   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