Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit dfeeb13

Browse files
committed
Add documentation for mutable objects.
1 parent b1b7e69 commit dfeeb13

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

Documentation/README.md

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Documentation
22

33
## Game Events
4-
The library provides two types of game events:
4+
Game events allow to decouple prefabs from direct dependencies such as scene or nested prefab game objects. The biggest benefit of events is that they can be referenced in isolated prefabs, this allows to later avoid having to set them up in the scene.
5+
6+
The package provides two types of game events:
57
- `GameEvent` without arguments.
6-
- `ArgumentGameEvent` which accepts arguments.
8+
- `ArgumentGameEvent` which accepts arguments (includes default implementations for commonly used types).
79

810
### Game event assets
9-
The core of events is game event `ScriptableObject` assets. They store game event listeners and notify them when an event is raised. To use game events, create an appropriate game event type asset (where _"Argument Name"_ is type of the argument that you are going to be passing to your events):
11+
The core of events is game event `ScriptableObject` assets. They store game event listeners and notify them when an event is raised. To use game events, create an appropriate game event type asset (where _"Argument Name"_ is the type of the argument that will be to passed to events):
1012
- `GameEvent` - _Right Click -> Create -> Game Events -> Game Event_
1113
- `ArgumentGameEvent` - _Right Click -> Create -> Game Events -> "Argument Name" Game Event_
1214

@@ -35,15 +37,15 @@ public class SceneManager : MonoBehaviour
3537
```
3638

3739
### Custom game events
38-
If you need to define a game event which accepts custom arguments, extend `GameEvents.Generic.ArgumentGameEvent` class:
40+
To define a game event which accepts custom arguments, extend `GameEvents.Generic.ArgumentGameEvent`:
3941
```cs
4042
[CreateAssetMenu(fileName = "CustomGameEvent", menuName = "Game Events/Custom Game Event")]
4143
public class CustomGameEvent : ArgumentGameEvent<Custom>
4244
{
4345
}
4446
```
4547

46-
In order to enable raising of custom game events from the inspector, create a `CustomEditor` script where the argument fields are going to be drawn. Extend `GameEvents.Generic.ArgumentGameEventEditor` and override `DrawArgumentField(Custom)`. Make sure to place this script under the `Editor` folder:
48+
In order to enable raising of custom game events from the inspector, create a `CustomEditor` script and specify how the argument fields are going to be drawn. Extend `GameEvents.Generic.ArgumentGameEventEditor` and override `DrawArgumentField(Custom)`. Make sure to place this script under the `Editor` folder:
4749
```cs
4850
[CustomEditor(typeof(CustomGameEvent))]
4951
public class CustomGameEventEditor : ArgumentGameEventEditor<CustomGameEvent, Custom>
@@ -56,14 +58,14 @@ public class CustomGameEventEditor : ArgumentGameEventEditor<CustomGameEvent, Cu
5658
```
5759

5860
### Game event listeners
59-
Game events can be listened to by listener components. To use pre-made listeners, add an appropriate listener component:
61+
Game events can be listened to by listener components. To use listeners, add an appropriate listener component via:
6062
- `GameEvent` - _Add Component -> Game Events -> Game Event Listener_
6163
- `ArgumentGameEvent` - _Add Component -> Game Events -> "Argument Name" Game Event Listener_
6264

63-
Once the component is added, slot in the appropriate `GameEvent` you want the listener to listen to and add your response methods to `onGameEvent` callback via the inspector.
65+
Once the component is added, slot in the appropriate `GameEvent` asset to the listener and add insert response methods to `onGameEvent` callback via the inspector.
6466

6567
### Custom game event listeners
66-
Custom game event listeners which accept different arguments can also be created. Create a `UnityEvent` which would accept your `Custom` type:
68+
Custom game event listeners which accept different arguments can also be created. Create a `UnityEvent` which would accept the `Custom` type:
6769
```cs
6870
[Serializable]
6971
public class CustomEvent : UnityEvent<Custom>
@@ -81,3 +83,47 @@ public class CustomGameEventListener : ArgumentGameEventListener<CustomGameEvent
8183

8284
### Examples
8385
Import the `GameEvents` samples which show how to use game events in various situations.
86+
87+
## Mutable Objects
88+
Mutable objects allow using `ScriptableObject` assets to store and share data. They are handy when a lot of behaviours need access to specific data. In such cases, mutable objects can be injected in dependant behaviours instead of forming hard dependencies between them or using singletons.
89+
90+
### Mutable object assets
91+
The package provides mutable objects for most commonly used types such as `MutableInt`, `MutableFloat`, etc. Mutable object assets can be created by (where _"Value Name"_ is the type name of the contained value):
92+
- `MutableObject` - _Right Click -> Create -> Mutable Objects -> Mutable "Value Name"_
93+
94+
The core of events is game event `ScriptableObject` assets. They store game event listeners and notify them when an event is raised. To use game events, create an appropriate game event type asset (where _"Argument Name"_ is the type of the argument that will be to passed to events):
95+
- `GameEvent` - _Right Click -> Create -> Game Events -> Game Event_
96+
- `ArgumentGameEvent` - _Right Click -> Create -> Game Events -> "Argument Name" Game Event_
97+
98+
### Custom mutable objects
99+
Using mutable objects for each individual field can be troublesome in cases where a lot of values need to be observed. In such a situation it is better to create a single mutable object to house those values by extending `MutableObject` and overriding `ResetValues()`:
100+
```cs
101+
[CreateAssetMenu(fileName = "MutableCustom", menuName = "Mutable Objects/Mutable Custom")]
102+
public class MutableCustom : MutableObject
103+
{
104+
[SerializeField]
105+
private int health = default;
106+
107+
[SerializeField]
108+
private int armor = default;
109+
110+
[SerializeField]
111+
private int xp = default;
112+
113+
public int Health { get; set; }
114+
115+
public int Armor { get; set; }
116+
117+
public int Xp { get; set; }
118+
119+
// Set property values when mutable object is enabled or if the values change in the inspector.
120+
public override void ResetValues()
121+
{
122+
Health = health;
123+
Armor = armor;
124+
Xp = xp;
125+
}
126+
}
127+
```
128+
129+
After that is set up, make sure to call `MutableObjectExtensions.ResetMutatedObjects()` somewhere in one of the `Awake` methods, each time before a scene loads. Make sure the script which calls this extension executes before other scripts by setting up [Script Execution Order](https://docs.unity3d.com/Manual/class-MonoManager.html).

0 commit comments

Comments
 (0)