Inheritance STL Entity Component Systems Scene Graphs Event Systems
Event Systems Motivation: Decoupling events from where they are sent and where they are processed. It facilitates communication between game systems. Observer or Command which do not decouple senders and receivers in time. Event or Message: Something that has happened or something needs to be done. ex) Keypress, Entity Death, etc... Event Queue You game will receive events from external code sporadically. You ll want to process input events at your own pace so each new event will be put into a FIFO queue. Game systems can subscribe to and receive events from the queue. But can also be done using an event channel object. Synchronous vs Asynchronous Event Handling Synchronous: Events are processed immediately. This is sometimes good for user input which controls the character. Because slight delays in input are very noticeable and detract from the game play experience. Behavior can get out of step with the reset of the game. Leading to the game waiting while processing the events and causing stuttering. Asynchronous: Important Problem your event handling must not assume the game is in the same state as when the event was posted as the FIFO nature of the queue means that many things can happen before the event is actually processed. Your events will probably include a lot more data than events in synchronous systems as they need to track this extra state information. Be careful about cycles in event handling. As a rule: avoid sending events from code that is handling events. Singlecast vs Broadcast
Inheritance C++ What is a derived class? Use class derivation list to specify the base class Derived classes can have different levels of Access Control. By Default it is private. Public Public members of the base class become public members of the derived class. Protected members of the base class become protected members of the derived class. Private members of the base class are not accessible Protected Public and protected members of the base class become protected members of the derived class. Private members of the base class are not accessible Private Public and protected members of the base class become private members of the derived class. Virtual Functions (Polymorphic Functions) A function that declares or inherits a virtual function Virtual keyword allows for references to a derived types base class to still use derived types method definitions. To use dynamic_cast, base and derived classes must be polymorphic. Virtual allows the function used to be determined at runtime based on the type of the object "Object Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away. Pure Virtual Functions (Abstract Class) Pure Virtual Functions defer providing a definition to base classes. These cannot be instantiated. Guarantees base classes have to provide a definition.
Virtual Destructors Virtual destructors are useful when you can delete an instance of a derived class through a pointer to base class. Avoiding memory leaks! Rule of Three The rule of three is a rule of thumb in C++ that claims that if a class defines one (or more) of the following it should probably explicitly define all three: Destructor Must be called explicitly if using variables declared on the heap. Called automatically when out of scope if variable is declared on the stack. copy constructor constructor that takes a reference to another instance of it s type copy assignment operator operator= function that takes exactly one parameter of type T. Assigning your own avoid shallow copies and invalidating pointers.
Entity Component Systems What is an Entity? Something tied to a concept or noun, has related data and logic? Player, Bullet, Car Generally you should prefer composition over inheritance OOP Inheritance Simple to implement Least Flexible Becomes a Blob anti pattern: huge single class with a large amount of complex interwoven functionality. OOP Composition Entity is an aggregate of components. Components store data and have logic. Easy and Flexible Cache Unfriendly Create entities through factory methods DOD composition Entity is an aggregate of components. Components are strictly data. Cache Friendly Hard to implement Most Flexible Systems handle logic Mimics relational database
Scene Graphs It is a data structure that contains the logical and spatial aspects of your scene s graphics. It s usually a tree or graph structure composed of nodes. Effects (colour changes, etc) on a parent are applied to the children and propagate further. Commonly useful for transformations, they allow for grouping of many objects into a compound one. Each node usually represents entities or objects in the scene. In this course you can probably get away with a simple linked-list structure. Just use PhysX