Low-Level Programming: Post 10 26/11/2015
November 26th 2015 is the date of the tenth lesson on Low-Level Programming.
Keeping with tradition, we were shown an overview of what we were going to learn today. From the bullet points, we were only going to be learning about three (more) types of patterns. Below are the bullet points in more detail...
- Observer Pattern: What is it and how does it work?
- Component Pattern: What is it and how does it work?
- Type Pattern: What is it and how does it work?
Before we began looking at the patterns, we were given a practical task. The task was to create two Pseudocodes, one for the 'Factory' that creates the GameObjects, which should have consisted of the Player, Three enemies and a Boss, and the other for an Object Pool that creates the enemies. It was a simple task, simply creating a class for, except the player, the enemies. We decided to make a separate pool for the boss as well.
After the 15 minute practical session, we moved onto the lesson slides again, resuming with learning about Observer Patterns.
After the 15 minute practical session, we moved onto the lesson slides again, resuming with learning about Observer Patterns.
Observer: Define a one-to-many dependency between objects so that when one object updates, others are aware
- Java & C#: Model-View-Controller architecture utilizes the observer pattern and is built into Java and C# even supports it via the event keyword.
- Announcement: In this pattern, one part of the code-base will announce an event, this event can be processed elsewhere.
- Coupling: An event based system allows us to reduce the level of complexity.
- Achievements: The achievement system used in games is event triggered, rather than pollute the code where the event happens, we notify observers.
- Register: It's now possible to register observers with our observable object.
- Events: The observable objects can then send events to all it's observers without any understanding of it's intention. We've maintained low coupling.
- Performance: We're simply iterating through a list of observers and calling a virtual function to handle the event.
- Dereferencing: There's an obvious overhead associated with virtual function calls, but it's fairly minimal, especially as we don't need to "poll" or couple.
- Observers: Care needs to be taken as the thread is paused whilst processing the observers update functions.
- Allocations: Best practices dictate we reduce the number of allocations during game time, in this case add observers at load time.
- Linked List: Instead of using a vector of pointers, it's possible to create a linked list using observer nodes, with each one pointing to the next.
- Multiple Observer: Using a linked list, we can only have the observer itself observing a single object at any given time.
- Nodes: To circumvent this problem, we can use the concept of a node, which has a link to the next node and a pointer to the observer.
- Pool: The nodes can then be pooled together, saving on allocations and deallocations. Remember to give them a bool flag for reusage.
Component: Allows a single entity to span multiple domains without coupling the domains to each other
- Game Objects: In games, it's not uncommon that objects need to interact with the rendering, update and input systems i.e. multiple domains.
- Coupling: In a traditional update method, it soon becomes obvious that code between the domains is coupled, with the coder needing an understanding of each component.
- Divide and Conquer: Let's resolve the issue by dividing the domains in to their own respective managed components.
- Candidates:
- An Input Component.
- An Audio Component.
- A Rendering Component.
- Result: The three components are decoupled and can be managed independently. Though it's likely they will need some awareness of each other.
- Reusable: Components are now reusable and we can prevent objects from having complex inheritance structures.
- Decoration: Decorations i.e. static objects with no dynamic updates are now composed of graphic components.
- Composition: Prefer composition over inheritance.
- Domains: This illustrates the definition in which it's stated that a single entity i.e. zombie spans multiple domains and remains decoupled.
- Large Classes: When the functionality in a class is becoming large and complex, it may be a good time to implement components to reduce it.
- Decoupling: If there's a desire to keep a class loosely coupled, then the component design pattern allows this.
- Common Functionality: If a variety of objects are utilizing similar functionality but inheritance doesn't fit correctly, again components can be used.
- Complexity: Utilising this design pattern makes the object more complex. It has a number of components, all of which need initialising.
- Communication: Another consideration is cross-communication between the domains, how do you tie them together if they need each other.
Type: Allows the flexible creation of new 'classes' by creating a single class, each instance represents a different type
- Inheritance: When designing classes of objects, there is the desire to create subclasses for each kind of similar object.
- Breeds: Using the Dungeon Death game as an example; last week I mentioned three enemies, most likely only different in base attributes.
- Subclass: Each subclass has more health than the other, due to the enemies' increased perception of strength.
- Additional Enemies: For every additional enemy, we end up writing short and annoying classes that only consist of a few lines.
- Inheritance: Inheritance works in this example, but it means we end up with all these small classes, but there's another option.
- Composition: Each monster class or enemy has a breed object. The breed object determines how a monster attacks or it's health.
- Challenges: The ability to override functions to determine behaviour are now not possible, instead the type data is attached to the class.
- Behaviour: Type patterns are good for defining the data relevant to an instance but are more difficult to encompass different behaviours at different times.
- States: We could store a state in the type data and then act differently depending on the value stored in the state.
- Data Files: With more complex solutions, behaviours can be constructed from data files. This way, the behaviour is completely removed from the code base.
- Type: The breed object molds or defines a monster's type, hence the name of the pattern i.e. type.
- Instances: Instances of the monsters which use the same breed object are conceptually identical and will act in the same way.
- Data: The instances store the data required per breed; this is what get's altered when the monster takes damage.
- Usages:
- You don't know what types you will need up front, just that you require multiple types.
- You want to be able to modify and add new types without having to change code.
After the end of the rather short lectural side of the lesson, we were shown some questions that other people on the course asked about the assignment. Two of the three questions were about some of the rules in the BlackJack game, in which the tutor said we didn't NEED to implement it, but implementing it would get us higher marks. The other question was about the documentation needed for the assignment. It says that it should cover our design decisions and utilise UML, Pseudocode and diagrams where necessary, basically saying why we design the game(s) like we did and how to implement it.
After that, we then moved onto the second practical of the lesson. Carrying on from the first practical earlier on, we had to implement the Pseudocode from earlier, as in, creating the classes for the enemies. Sadly, we only managed to create the classes and ran out of time to do any more work on it.
No comments:
Post a Comment