我在1个文件中有2个类
此字符
public class Character{
public static int Count = 0;
public string username, charname;
public int gender, level, money, playtime;
public Character(string _username, string _charname, int _gender, int _level, int _money, int _playtime)
{
username = _username;
charname = _charname;
gender = _gender;
level = _level;
money = _money;
playtime = _playtime;
Count++;
}
}
和播放器
public class Player : Script
{
public Dictionary<string, Character> CharacterList = new Dictionary<string, Character>();
}
我想从另一个类中调用 CharacterList 字典,就像这样
public class AmmuNation : Script
{
Player Ply = new Player();
public void Test(Client sender)
{
API.consoleOutput(Ply.CharacterList[sender.name].charname);
}
}
但是当 Test()执行时,即使Key在词典中,它也会给我这个错误
字典中没有给定的密钥。
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
当我从课程播放器中调用它时它会起作用并且会给我charname的值
但是当我从 AmmuNation 调用它时,它会说未找到密钥
当我尝试这个时,甚至字典都是空的
foreach (KeyValuePair<string, Character> kvp in Ply.CharacterList)
{
API.consoleOutput(kvp.Key);
}
但在播放器上,它会打印 CharacterList
中的每个键答案 0 :(得分:0)
我看了一些youtube视频后找到了解决方案 我需要做的就是将CharacterList Dictionary设置为static,然后使用类名这样从AmmuNation调用它
public class AmmuNation : Script
{
public void Test()
{
API.consoleOutput(Player.CharacterList[sender.name].charname);
}
}
我也不需要
Player Ply = new Player();
了