我的目标:我想创建一个可以有键值对的系统,但我不需要预先设置密钥或值的数据类型
我试图在一定程度上模仿蟒蛇词典
我想要的示例
{
"hotbar"=
{
{"slot_1"={"item"="dirt","amount"=3},
{"slot_3"={"item"="shovel","amount"=1,"usesLeft"=135}
}
"walkspeed"=10
"jumpPower"=15
"position"={"x"=10,"y"=3,"z"=31.3}
类似于Minecraft的nbt系统是我的目标
语法不需要完全相同,只要我可以用密钥和值存储它,而不必在意它是什么类型的密钥和值
我希望能够从中读取和写入单个值(包括嵌套值(并且还循环通过所有键值对以及
请给我推荐更好的标签
关键价值在于字典https://www.tutorialsteacher.com/csharp/csharp-dictionary但在OOP中,您可以只使用对象列表
,例如。//设计对象(类(
public class Hotbar { //Hotbar class
public string key_name {get;set;}
public stirng item {get;set;}
public int amount{get;set;}
public int usesLeft{get;set;}
}
public class YourMainClass{
public List<Hotbar> hotbar_List {get;set;}
public int walkspeed {get;set;}
public int jumpPower {get;set;}
public PositionObj position {get;set;} // go create object of position
public YourMainClass(){
// constructor Init List
hotbar_List =new List<hotbar>();
}
}
示例如何使用
YourMainClass targetObj =new YourMainClass();
// create new Hotbar
Hotbar hotbar1 =new Hotbar();
hotbar1.key_name = "slot_1";
hotbar1.item ="dirt";
hotbar1.amount= 3;
// add hotbar #1
targetObj.hotbar_List.Add(hotbar1);
// get hot bar slot_1
Hotbar thatHot = targetObj.hotbar_List.Where(o=>o.key_name =="slot_1").FirstOrDefault();
// for looping - will loop if there are obj in List
foreach( Hotbar oneHotBar from targetObj.hotbar_List ){
// do something with hotbar Obj call oneHotBar
string name_of_item = oneHotBar.key_name ;
// try minus amount
if( oneHotBar .key_name =="slot_1" )
{
oneHotBar. amount = oneHotBar. amount-1;
}
}
p.s.很抱歉,我不熟悉Dictionary和Dynamic类型,所以提供的代码只适用于强类型类,请等待其他代码。