My goal: I want to create a system that can have key value pairs but that I don't need to beforehand set either the key or the value's data type
What I am trying to emulate to an extent is pythons dictionaries
example of what I want
{
"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}
similar to Minecraft's nbt system is my goal
the syntax does not need to be perfectly the same just as long as I can store it with a key and value without it mattering what type of key and value it is
And I want to be able to read and write individual values from this (including nested values) and also to loop through all of the key value pairs as well
please recomend me better tags
Key value things is Dictionary https://www.tutorialsteacher.com/csharp/csharp-dictionary but in OOP you can just use List of objects
eg //Design Objects (classes)
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>();
}
}
example how to use
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;
}
}
ps sorry I'm not fluent with Dictionary please wait for others.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.