| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Mischief.Assets.Tutorial.Kdl
Synopsis
Chapter 4: Kdl Assets
Kdl is the official file format used by Mischief.
The KdlAsset Class
The KdlAsset typeclass is a helper for defining assets that are loaded from .kdl files without needing to parse their fields manually.
Generally, only types with named fields (records) can automatically derive it:
data Foo = Foo
{
a :: Int
b :: Float
c :: V2 Int
}
deriving (Generic, KdlAsset)
You can now use the loadKdl function provided by the typeclass to quickly make a loadable asset:
instanceAssetFoo where typeSaveableFoo =FalseloadAsset=loadKdl"foo"
The asset itself should look something like this:
foo {
a 6
b 3.2
c 2 4
}
Manual Decoding
You can write custom kdl decoders using the kdl-hs library.
For instance, this is how you could decode the asset from the previous section:
instanceKdlAssetFoo wheredecodeKdlpath = KDL.children$ do a <- KDL.argAt"a" b <- KDL.argAt"b" c <- KDL.nodeWith"c" KDL.nodeDecoderpure $ Foo {a, b, c}
Note that here we can use "KDL.nodeDecoder" because V2 a has a KDL. instance. Otherwise you'd need
to parse it manually, like the others.DecodeNode
The path argument is used to decode references to other assets. For instance, if Foo also had this field:
bar :: AssetRef Bar
You'd decode it like this:
bar <-argAtWith$ "bar" $decodeRefpath
For KdlRefs you use decodeKdlRef instead.
Collections
A Kdl Collection is a map of nodes of the same type.
For instance, this field:
a ::KdlCollectionInt
Expects something that looks like this:
a {
name1 4
name2 6
name3 5
...
}
You can use to try and get the value of a node from a collection by name. Or colGet to get the
value of all nodes with the given name.colGetMany