如何将数组返回给子类?

问题描述 投票:0回答:1

我正在尝试从我的游戏中实现一个保存系统,我决定通过在基类中填充一个数组,然后将整个数组返回到子类的数组来实现此目的。但我不断收到错误:无法将类型“PlayerBodyPart[]”隐式转换为“PlayerBodyPart”。难道是因为child的数组类型是结构体吗?

public class LoadData : MonoBehaviour
{
    public PlayerBodyPart LoadBPdata(string filePath, PlayerBodyPart[] bodyPart)
    {
        int id; int type; string name; int skill; string spritePath; int price; bool isUnlocked;

        int k = -1;

        var lines = File.ReadAllLines(filePath);

        for (int i = 0; i < lines.Length; i++)
        {           
            if (lines[i].Contains("id"))
            {
                k += 1;
                id = Int32.Parse(Regex.Match(lines[i], @"\d+").Value);

                string typ = Regex.Replace(lines[i + 1], "type ", "");
              
                switch(typ)
                {
                    case ("Head"):
                        type = 0;
                        break;
                    case ("Body"):
                        type = 1;
                        break;
                    case ("Arms"):
                        type = 2;
                        break;
                    case ("Legs"):
                        type = 3;
                        break;
                    default:
                        type = 0;
                        break;
                }

                name = lines[i + 2];
                skill = Int32.Parse(Regex.Match(lines[i + 3], @"\d+").Value);
                spritePath = Application.dataPath + lines[i + 4];
                price = Int32.Parse(Regex.Match(lines[i + 5], @"\d+").Value);

                string unlockBool = Regex.Replace(lines[i + 6], "unlocked ", "");

                if (unlockBool == "True") isUnlocked = true;
                else isUnlocked = false;

                Debug.Log($"{id}, {type}, {name}, {skill}, {spritePath}, {price}, {isUnlocked}");
                
                bodyPart[k] = new PlayerBodyPart(id, type, name, skill, spritePath, price, isUnlocked);
            }          
        }
        return bodyPart;
    }
}

儿童密码:

public struct PlayerBodyPart
{
    public enum PartType { Head, Body, Arms, Legs };

    public int id;
    public PartType type;
    public string name;
    public int skill;
    public Sprite sprite;
    public int price;
    public bool isUnlocked;

    public PlayerBodyPart(int id, int type, string name, int skill, string spritePath, int price, bool isUnlocked)
    {
        this.id = id;
        this.type = (PartType)type;
        this.name = name;
        this.skill = skill;
        this.sprite = Resources.Load("spritePath") as Sprite;
        this.price = price;
        this.isUnlocked = isUnlocked;
    }
}

public class PlayerBPCatalogue : LoadData
{
    public PlayerBodyPart[] PBpart;

    public int n = 1000;

    public void Start()
    {
        PBpart = new PlayerBodyPart[n];
        string filePath = Application.dataPath + "/Data/bodyParts.txt";

        base.LoadBPdata(filePath, PBpart);           
    }   
}
c# unity-game-engine
1个回答
0
投票

如果你想返回一个数组,你应该返回一个数组。将您的退货类型更改为

PlayerBodyPart[]
,然后就可以开始了。

public PlayerBodyPart[] LoadBPdata(string filePath, PlayerBodyPart[] bodyPart)

但我确实认为这是一个 xy 问题,因为这里有两个问题:

  1. 传递数组始终是通过引用,因此传递它并返回它是没有意义的。您只需将其传递进去就可以了。
  2. 为什么不使用
    List<PlayerBodyPart>
    来代替分配具有 1000 个条目的数组呢?你也不需要归还它,这样你就省去了分配这么多东西的麻烦。
© www.soinside.com 2019 - 2024. All rights reserved.