Bundling Data
There are two ways to bundle data in Ingot:
Structs
Compounds
Structs
Structs are a way to store multiple values in a single variable. The variables can be of different types.
Declaration
Structs are defined with the struct
keyword.
src/main.ing
Instantiation
Instances of structs can be made by specifying the struct name and then the values for each field.
src/main.ing
Specific fields can be accessed with the .
operator.
src/main.ing
Sample Output
Methods
Structs can have methods. The syntax is similar to that of Rust:
src/main.ing
Note that here, even though &self
is a reference value, we still use the dot notation. This is because the compiler will automatically dereference the reference value.
Sample Output
Compounds
Structs will store scoreboard-storable values in a scoreboard, and data storage-storable values in data storage.
They are not stored as compounds, meaning that in order to convert it into a compound to use in NBT, you need to convert it to a compound, which can be performance intensive.
Instead, if you want a type-safe compound, you may add a struct to a compound like so:
src/main.ing
Now, if you try to access a field that does not exist, the compiler will give an error.
If you want to convert a compound to a struct, you can use the to_struct
macro, and use the to_compound
macro to convert a struct to a compound.
src/main.ing