tModLoader v2024.10
A mod to make and play Terraria mods
TagCompound Class Reference

Tag compounds contained named values, serializable as per the NBT spec: NBT spec wiki page
All primitive data types are supported as well as byte[], int[] and Lists of other supported data types
Lists of Lists are internally stored as IList<IList>
Modification of lists stored in a TagCompound will only work if there were no type conversions involved and is not advised
bool is supported using TagConverter, serialized as a byte. IList<bool> will serialize as IList<byte> (quite inefficient)
Additional conversions can be added using TagConverter The Saving and loading using TagCompoundteaches how to properly use the TagCompound class. More...

Inherits IEnumerable< KeyValuePair< string, object > >, and ICloneable.

Public Member Functions

void Add (KeyValuePair< string, object > entry)
 Use this to add a KeyValuePair to the TagCompound.
 
void Add (string key, object value)
 Use this to add values to the TagCompound indexed by the specified key .
 
void Clear ()
 
object Clone ()
 
bool ContainsKey (string key)
 Returns true if an entry with specified key exists. This is useful to check if a key is present prior to retrieving the value in the case that key potentially won't exist and the default behavior of Get<T>(string) would be undesirable. The Mod Version Updates section of the wiki guideshows an example of one such situation.
 
Get< T > (string key)
 Retrieves the value corresponding to the key of the Type T . If no entry is found, a default value will be returned. For primitives this will be the typical default value for that primitive (0, false, ""). For classes and structs the returned value will be the result of calling the appropriate deserialize method with an empty TagCompound. This will usually be a default instance of that class or struct. For List<T>, an empty list is returned. For arrays, an empty array would be returned. If the found entry is not of the Type T an exception will be thrown. More...
 
double GetAsDouble (string key)
 
int GetAsInt (string key)
 
long GetAsLong (string key)
 
short GetAsShort (string key)
 
bool GetBool (string key)
 
byte GetByte (string key)
 
byte[] GetByteArray (string key)
 
TagCompound GetCompound (string key)
 GetCompound can be used to retrieve nested TagCompounds. This can be useful for saving complex data. An empty TagCompound is returned if not present.
 
double GetDouble (string key)
 
IEnumerator< KeyValuePair< string, object > > GetEnumerator ()
 
float GetFloat (string key)
 
int GetInt (string key)
 
int[] GetIntArray (string key)
 
IList< T > GetList< T > (string key)
 
long GetLong (string key)
 
short GetShort (string key)
 
string GetString (string key)
 
bool Remove (string key)
 Removed the entry corresponding to the key . Returns true if the element is successfully found and removed; otherwise, false.
 
void Set (string key, object value, bool replace=false)
 Use this to set values to the TagCompound indexed by the specified key .
 
override string ToString ()
 
bool TryGet< T > (string key, out T value)
 Attempts to retrieve the value corresponding to the provided key and sets it to value . If found, true is returned by this method, otherwise false is returned. Unlike Get<T>(string), TryGet will not attempt to set value to a valid fallback default created by the deserialize method of classes and structs in the case where the key is not found. Use this instead of Get<T>(string) in situations where falling back to the default value for missing entries would be undesirable. More...
 

Properties

int Count [get]
 
object this[string key] [get, set]
 Use this to add values to the TagCompound, similar to working with a Dictionary<TKey, TValue>. An alternate to this is calling Add(string, object) directly. If is also possible to use this to retrieve entries from the TagCompound directly, but since the return value is Object this is rarely the best approach. Usually one of the Get<T>(string) methods should be used for this. One situation where this is necessary is described in the Updates to data Type section of the wiki guide.
 

Detailed Description

Tag compounds contained named values, serializable as per the NBT spec: NBT spec wiki page


All primitive data types are supported as well as byte[], int[] and Lists of other supported data types
Lists of Lists are internally stored as IList<IList>
Modification of lists stored in a TagCompound will only work if there were no type conversions involved and is not advised
bool is supported using TagConverter, serialized as a byte. IList<bool> will serialize as IList<byte> (quite inefficient)
Additional conversions can be added using TagConverter

The Saving and loading using TagCompound

teaches how to properly use the TagCompound class.

Member Function Documentation

◆ Get< T >()

T TagCompound.Get< T > ( string  key)

Retrieves the value corresponding to the key of the Type T . If no entry is found, a default value will be returned. For primitives this will be the typical default value for that primitive (0, false, ""). For classes and structs the returned value will be the result of calling the appropriate deserialize method with an empty TagCompound. This will usually be a default instance of that class or struct. For List<T>, an empty list is returned. For arrays, an empty array would be returned. If the found entry is not of the Type T an exception will be thrown.

Template Parameters
T
Parameters
key
Returns
Exceptions
IOException

◆ TryGet< T >()

bool TagCompound.TryGet< T > ( string  key,
out T  value 
)

Attempts to retrieve the value corresponding to the provided key and sets it to value . If found, true is returned by this method, otherwise false is returned. Unlike Get<T>(string), TryGet will not attempt to set value to a valid fallback default created by the deserialize method of classes and structs in the case where the key is not found. Use this instead of Get<T>(string) in situations where falling back to the default value for missing entries would be undesirable.

Template Parameters
T
Parameters
key
value
Returns
Exceptions
IOException