Showing posts with label Dark Arts. Show all posts
Showing posts with label Dark Arts. Show all posts

Wednesday, 25 November 2015

Dark Arts 4: Statics, XML and Shaders

Static classes cannot be allocated, therefore there can be only one of them in which every method and variable must also be static. They can be used to load in files and store variables from them, such as settings, as well as wrap up methods you know will be needed in multiple places. Static classes are never deallocated which makes them really useful for keeping data around between scenes in Unity, like textures which would save on loading times.
Static class to add and remove textures at will
XML was designed to be easily read by people as well as computers. All XML files must begin with the header of    <?xml version="1.0" encoding="utf-8" ?>    .  From there you have to define an element which will be the name to call when needing the data inside (similar to a class). Inside that other elements can be defined which contain the precise data you require, in the form of a 'Key Value' pair e.g <player lives="3"/>. Every value is recorded as a string.
To use an XML file, you need to first parse it into Unity and then select the base element you want. Then you can walk through any children and use the key/value pairs as needed.

XML parsing class

Wednesday, 18 November 2015

Dark Arts 3: Delegates and Co-routines

Delegates are used as a reference to a method with specific parameters. We can use them if we have multiple versions of a method that we need to choose from and how to handle each one. To define the delegate all you need is:  public delegate <Type>   MethodName(<Type> variable);
Another good use of delegates is a messaging system between classes. A list of delegates can be created as a way to keep track of the receivers, that way we always know where the message will be going. The 'listeners' can then chose whether or not they act on the message depending on what it is.
Example of using delegates for message system

A co-routine is a method that can be paused and continued later. This is very useful to script events and create situations where something is waited for or triggered at a certain time. Using a co-routine that contains yield is what causes it to continue in the next frame from where it left off in the last one, rather than starting from scratch. This is what I've done in order to fade out the bullet holes in previous projects I've done so it's good to find out I did it to a proper standard. A co-routine can also be stopped at any time but only by calling it's name, giving you even greater control on how it carries out.
Co-routine that fades and resets colour

Wednesday, 11 November 2015

Dark Arts 2: Singletons and Entities

In the second of our dark arts sessions we were taught about the uses of singletons and entities in our coding. As I've already covered the singleton pattern in Java, it was simple recap when Richard talked about how to use it, however it was very useful to see the examples he gave of where in a game it could be used such as for UI managers. The singleton pattern is where you set something up that is only created once and called anytime it's ever needed. This stops multiple copies of the same object from being instantiated, lowering the amount of errors you could get and also keeping memory management tidier. The downsides are that it isn't thread safe and everything can access it, which is just as useful as it is a liability.
How to instantiate and keep an singleton

Virtual methods were the second subject Richard covered, which are methods a class can essentially inherit from, unless the method is overwritten. Essentially it allows you to write one class with a lot of methods in for a variety of objects that will use them, but be able to change it for each object if needs be. The base class is essentially the game entity with all other classes using it if they require the methods.
Example of two classes using a base class but one overriding the method

Wednesday, 4 November 2015

Dark arts 1: Generics

This week we had the first of our ‘Dark Arts’ sessions with Richard Weeks. Richard is head of a company called Total Monkery that operates from Plymouth and we’ve been lucky enough for him to come in and teach us some industry standard programming methods. Having somebody who runs his own company and has experienced the games industry first hand come in to teach us is an excellent opportunity. While what I’ve learnt so far at University has been essential, Richard is going through ways to make Unity work in ways it normally wouldn’t, hence why these are called ‘Dark Arts’ sessions.


The first session covered generics. This allows us to make custom types for classes, methods and variables which has lots of uses. When used for a class, it allows the user to get the variables and constructors, meaning you can create objects in the generic class from another. When using a generic method, the type of object you want to pass in must be specified before the parameter, this way a single generic method can perform an operation on many varying data types. One example (shown below) is a method to create entities where a list of different entities can all be passed in. Dictionaries can use generics to form lists with a variety of different types by having a generic key with a corresponding value.

In an example we were shown, one method assigned class types and names, while the other passed in a large variety of different types of entities.This is something I could use in my game to make it less prone to errors and easier to work with as a whole.
Entity inheritance example