You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/content/docs/guides/best-practices.mdx
+60-2Lines changed: 60 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,66 @@ sidebar:
5
5
order: 50
6
6
---
7
7
8
-
TODO: Best practices for using Scriptable Values
8
+
Scriptable Values is simple to use, but there are some best practices that you should follow to make your code cleaner and easier to maintain. This page will cover some of the best practices for using Scriptable Values.
9
9
10
10
## Events
11
11
12
-
## Property Tracking
12
+
### Unsubscribing
13
+
14
+
When you subscribe to an event, you should always unsubscribe from it when you are done using it. This is especially important when using events in Unity, as they can cause memory leaks if not unsubscribed properly. Fortunately, Scriptable Values has a built-in warning system that will warn you if you forget to unsubscribe from an event. This warning will only show up in the editor, so it won't affect your game in any way. It will also clear subscribers when the game starts. **But you should still always unsubscribe from the event when you are done with the event.**
Property Tracking can be used to update UI, track changes, and more. Invoking a property change event requires you to create a new `PropertyChangedEventArgs` and `PropertyChangingEventArgs` object. This can cause more allocations than necessary. Fortunately, all these objects are cached and reused in Scriptable Values to minimize allocations. However, when [creating your own types](/scriptable-values/guides/creating-custom-types) you should also cache your event args. `RuntimeScriptableObject.SetField` allows you to pass in your own event args to be used.
44
+
45
+
Scriptable Values will try to avoid creating these event args when possible, but it is still a good idea to cache them yourself. This will help reduce allocations and improve performance.
0 commit comments