Wednesday, 2 December 2015

Session 11

Operations!
One example of an operation is x == y. This is the equality operation
It compares one thing to another (x is equal to y), if it was a single = it'd be assigning the value instead of comparing

Function pointers
A function identifier is a constant pointer to a function, eg:
void run(); //the brackets are the dereference bit

The address of a function can be stored, eg:
int(*anyName)(Health&);
anyName(gremlin.getHealth());

The brackets specify that you want the function's address and not just a generic pointer address
Storing pointer functions - implicit means you don't specify that the pointer is a function pointer, it is only implied.
Explicit function pointer storing requires that you Do specify (using (*x), where x is the name of the function)
It is possible to pass function pointers to different functions
But why? You can compare, by adding the function pointer to the function signature

Decltype evaluates type of an expression to cheat our declaration of function pointers as a time
Decltype is like the auto keyword; saves time and effort (looks a bit neater too for longer declarations)
Auto saves having to write: bool (*functionPointer)(int x, int y)
Can use function pointers when making a factory

Static GameObject* createObject() {return new PlayingCard;}
is the same as:
std::map<std::string, GameObject*(*)() map;

State Machine - if all action-handling functions have the same signature, you can use function pointers for the input
Functor - a function 'wrapped up' as a class. Can use, for example, a multiplication class as if it were a function

Session 10

Observer
An observer defines a one-to-many dependency between objects so that when one object updates, anything attached to that object updates (and reacts to it) too automatically
Allows a reduce in the level of complexity - easier to look at, saves time etc.
Can set an event that tells everything that happens (eg a player dies), this saves manual checking as anything that needs to know about the player dying will automatically know
The observed class is also known as the subject

example syntax:
observable subject;
subject.addObserver(observer); //to add an observer

Performance - be aware there is some overheard for iterating through with observers
However, instead of checking continuously, something can be made to run Only when it sees an event to counter that
When an event plays, the thread pauses and observers update functions
Add observers during compile to reduce lag during runtime

Component Pattern
Allows a single entity to span multiple domains (aka systems) without having to couple those domains
('Multiple domains' can refer to rendering, update, input etc)
Each domain can become a component
For example, a gameobject knows it has multiple components, but it doesn't have to know what they actually do
These components don't have to be coupled together
There is no need for class hierarchy when using composition
(for reference, class hierarchy is this kind of thing):

Type Patterns
These allow for flexible creation of new classes
Usually enemy classes for example are very similar so instead of multiple sub-classes, type can be used
Each enemy has a breed which holds its health, damage, etc
Behaviour can be read from a text file if the breed is complex, this would save coding space but isn't really necessary