Skip to content

Commit 911e92f

Browse files
committed
Updated readme with dirty flag
1 parent 696fc14 commit 911e92f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ Your game can be in a number of states. For example, the main character can have
123123
**How to implement?**
124124

125125
* You could use an enum that keeps track of each state and then a switch statement.
126+
126127
* 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.
127128

128129
**When is it useful?**
129130

130131
* 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+
132135
* When you make a turn-based combat system: [How to Code a Simple State Machine](https://www.youtube.com/watch?v=G1bd75R10m4).
133136

134137
**Related patterns**
@@ -158,15 +161,16 @@ You have an object and now you want to change its type (such as behavior or some
158161

159162
**How to implement?**
160163

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.
162165

163166
**When is it useful?**
164167

165168
* 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.
166169

167170
**Related patterns**
168171

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+
170174
* [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.
171175

172176

@@ -182,6 +186,7 @@ When making a big game you should start thinking in components. A component is s
182186
**When is it useful?**
183187

184188
* 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+
185190
* 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.
186191

187192
**Related patterns**
@@ -201,6 +206,21 @@ When making a big game you should start thinking in components. A component is s
201206

202207
## 17. Dirty Flag
203208

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.
222+
223+
204224

205225
## 18. Object Pool
206226

0 commit comments

Comments
 (0)