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: README.md
+23-3
Original file line number
Diff line number
Diff line change
@@ -123,12 +123,15 @@ Your game can be in a number of states. For example, the main character can have
123
123
**How to implement?**
124
124
125
125
* You could use an enum that keeps track of each state and then a switch statement.
126
+
126
127
* The problem with the switch statement is that it tends to become complicated the more states you add. A better way is to define an object for each state and then you switch between the objects as you switch states.
127
128
128
129
**When is it useful?**
129
130
130
131
* When you have too many nested if-statements, such as in a menu system. In the code, you can see an example of a menu system that uses this pattern.
131
-
* Unity is using this pattern in the animation engine.
132
+
133
+
* Unity is using this pattern in the animation engine.
134
+
132
135
* When you make a turn-based combat system: [How to Code a Simple State Machine](https://www.youtube.com/watch?v=G1bd75R10m4).
133
136
134
137
**Related patterns**
@@ -158,15 +161,16 @@ You have an object and now you want to change its type (such as behavior or some
158
161
159
162
**How to implement?**
160
163
161
-
The Type Objects should share the same interface (or parent) to make it easier for the main class to reference the object.
164
+
*The Type Objects should share the same interface (or parent) to make it easier for the main class to reference the object.
162
165
163
166
**When is it useful?**
164
167
165
168
* When you can't (or don't want to) use class inehritance. Let's say you make a game with animals. You have a base class which is parent to all animals, and then as children to that class you add birds, fish, and mammals. In the bird class you define a flying behavior, which is all fine until you add an ostrich, which can't fly. In that case you have to inherit from the bird class and create new children that can fly and can't fly. But what about bats, which is a mammal that can fly? You don't want to add flying behavior in two separate classes! A better way is to define a flying and a non-flying type in a separate class, so both ostriches remain on the ground and bats can fly.
166
169
167
170
**Related patterns**
168
171
169
-
*[State](#6-state). In both cases you have a main object and then you add another object to define something. The difference is that in State you switch the other object, while in Type Object that object remains the same. So if the type can be switched you get the State pattern.
172
+
*[State](#6-state). In both cases you have a main object and then you add another object to define something. The difference is that in State you switch the other object, while in Type Object that object remains the same. So if the type can be switched you get the State pattern.
173
+
170
174
*[Component](#13-component). The difference is that the Component is not always coupled with something else on the game object – it’s living its own life. In Unity you can add colliders, scripts, mesh renderers and they don’t need to know about each other to function. Type Object, however, is about adding a behavior to an existing class, so the type can't live on its own.
171
175
172
176
@@ -182,6 +186,7 @@ When making a big game you should start thinking in components. A component is s
182
186
**When is it useful?**
183
187
184
188
* Because Unity's FPS counter is not to be trusted, you can have a custom FPS counter that you re-use throughout all projects. Just add the script to the project and attach it to some GameObject and it works fine independently of everything else going on in the game.
189
+
185
190
* When making a car game you can put physics in one script, such as drag and rolling resistance, becuse physics will always affect the car and the physics calculations are the same for all cars. This component will not be completely independent because it will need some data from the car, such as current speed, but as said before that's fine.
186
191
187
192
**Related patterns**
@@ -201,6 +206,21 @@ When making a big game you should start thinking in components. A component is s
201
206
202
207
## 17. Dirty Flag
203
208
209
+
This pattern is useful if something has changed in your game, and if so you have to run a costly operation. A Dirty Flag is used to tell that something has changed but the costly operation hasn't been activated.
210
+
211
+
**How to implement?**
212
+
213
+
* The dirty flag is just a simple boolean, which is set to true if something has changed.
214
+
215
+
**When is it useful?**
216
+
217
+
* Saving your game can be a costly operation. If something in your game has changed that also needs to be saved, you set a Dirty Flag in the save game object to true. Now if the player wants to quit the game, you can easily tell the player that there are unsaved changes.
218
+
219
+
* If you ever done some editor scripting in Unity, you know that you can use SetDirty() to mark an object as dirty or you can even mark the entire scene as dirty. Now Unity will understand that you have changed something and those changes should be saved when you save your scene.
220
+
221
+
* Unity is using it in the physics system. A RigidBody doesn't have to be updated unless a force is applied to it. If the RigidBody is sleeping (not moving), a Dirty Flag is used so the physics system can ignore it.
0 commit comments