Skip to content

Commit 0c5284c

Browse files
committed
docs: scriptable dictionary docs
1 parent 96f6ac3 commit 0c5284c

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

docs/src/content/docs/types/scriptable-dictionary.mdx

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,106 @@ sidebar:
55
order: 35
66
---
77

8-
TODO: Scriptable Dictionary
8+
`ScriptableDictionary<TKey, TValue>` is a type of scriptable object that holds a dictionary of values. It acts just like a regular [`Dictionary<TKey, TValue>`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2), but it is a scriptable object that can be used in the inspector and be passed around to other objects.
9+
10+
## Usage
11+
12+
Using a Scriptable Dictionary in your code is really straight forward. You use the `Add`, `Remove`, and `Clear` methods to manage the dictionary.
13+
:::note
14+
To actually asign the dictionary to a field you need to create an instance of the dictionary in the editor. These instances need to be created in code first. See [Creating a Dictionary](#creating-a-dictionary) for more information.
15+
:::
16+
17+
`Add` will add an object to the dictionary. The key must be unique, otherwise it will throw an exception.
18+
19+
```csharp title="Add Example"
20+
using UnityEngine;
21+
using Hertzole.ScriptableValues;
22+
23+
public class Inventory : MonoBehaviour
24+
{
25+
[SerializeField]
26+
private ScriptableDictionary<string, Item> items;
27+
28+
public void AddItem(string key, Item item)
29+
{
30+
items.Add(key, item);
31+
}
32+
}
33+
```
34+
35+
`Remove` will remove an object from the dictionary.
36+
37+
```csharp title="Remove Example"
38+
using UnityEngine;
39+
using Hertzole.ScriptableValues;
40+
41+
public class Inventory : MonoBehaviour
42+
{
43+
[SerializeField]
44+
private ScriptableDictionary<string, Item> items;
45+
46+
public void RemoveItem(string key)
47+
{
48+
items.Remove(key);
49+
}
50+
}
51+
```
52+
53+
`Clear` will remove all objects from the dictionary. This is useful for cleaning up the dictionary when it is no longer needed, like when quitting or changing scenes.
54+
55+
```csharp title="Clear Example"
56+
using UnityEngine;
57+
using Hertzole.ScriptableValues;
58+
59+
public class Inventory : MonoBehaviour
60+
{
61+
[SerializeField]
62+
private ScriptableDictionary<string, Item> items;
63+
64+
public void Clear()
65+
{
66+
items.Clear();
67+
}
68+
}
69+
```
70+
71+
You can also access the dictionary directly using the indexer. This is useful for getting or setting values in the dictionary.
72+
73+
```csharp title="Indexer Example"
74+
using UnityEngine;
75+
using Hertzole.ScriptableValues;
76+
77+
public class Inventory : MonoBehaviour
78+
{
79+
[SerializeField]
80+
private ScriptableDictionary<string, Item> items;
81+
82+
public void SetItem(string key, Item item)
83+
{
84+
items[key] = item;
85+
}
86+
87+
public Item GetItem(string key)
88+
{
89+
return items[key];
90+
}
91+
}
92+
```
93+
94+
There are many more methods available in the dictionary, such as `ContainsKey`, `TryGetValue`, and `Count`. You can find the full list of methods in the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2).
95+
96+
## Creating a Dictionary
97+
98+
You need to create a dictionary before you can use it in the editor. Fortunately, this is really easy to do! All you need to do is inherit from `ScriptableDictionary<TKey, TValue>`.
99+
100+
```csharp title="Creating a Dictionary"
101+
using UnityEngine;
102+
using Hertzole.ScriptableValues;
103+
104+
[CreateAssetMenu(fileName = "New Item Dictionary", menuName = "Scriptable Values/Dictionary")]
105+
public class ItemDictionary : ScriptableDictionary<string, Item>
106+
{
107+
// You can add any custom methods or properties here.
108+
// There are no methods to override, but you can add your own methods.
109+
}
110+
```

docs/src/content/docs/types/scriptable-list.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ public class Inventory : MonoBehaviour
7070
}
7171
```
7272

73+
You can also access the list directly using the `Value` property. This will return the list of values, which you can use just like a regular list.
74+
75+
```csharp title="Accessing the List"
76+
using UnityEngine;
77+
using Hertzole.ScriptableValues;
78+
79+
public class Inventory : MonoBehaviour
80+
{
81+
[SerializeField]
82+
private ScriptableList<Item> items;
83+
84+
public void PrintItems()
85+
{
86+
for (int i = 0; i < items.Value.Count; i++)
87+
{
88+
Debug.Log(items.Value[i]);
89+
}
90+
}
91+
}
92+
```
93+
7394
There are many more methods available on the list, such as `Contains`, `IndexOf`, and `Sort`. It aims to have full parity with the regular `List<T>` class. See the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1) for more information.
7495

7596
## Creating a List

0 commit comments

Comments
 (0)