Project 2: Game Programming Essentials

by Andrew Long on ⌜Oct 30, 2022⌟

Tags: #GPE338, #Unity, #Game Programming

Project 2

This project, the first without a given title, was surprisingly easy. It did make me stretch a bit as I haven’t embedded videos in HTML before. Did you know you have to have a video muted just for it to play? It doesn’t even play if the video has no audio!

Loading From JSON:

For whatever reason, I couldn’t get regular JSON loading through Unity’s resource loader. So, I used bog standard C# functions.

To get JSON data from a file, call the jsonLoad.GetJsonFromPaths() function.

I’ve implemented this class like this in my playerSave class:

Here is what the code looks like when running in the game:

Loading From Resource Folder:

Loading assets from the resource folder is relatively easy. All you have to do is pass the path of the asset to the Resources.Load function.

Just remember that the path of the resources folder is relative to Assets/Resources in your Unity project. Also, don’t pass the file extension with the file path.

My implementation uses the function above like so:
Here is what the code looks like when running in the game:

Using Interfaces:

Interfaces are like classes in that they can be inherited. They are different in a few ways, though. Interfaces don’t contain function definitions. Instead, they contain function declarations. The functions inherited have to be defined inside the inheriter.

Using Namespaces:

Namespaces allow you to import all of the classes inside the namespace into another C# script.

I’ve implemented namespaces to separate my loading functions and ordering them under specific classes.

Getters and Setters:

Getters and Setters are functions that get and set private variables.

I’ve used getters and setters to interact with a private variable when the programmer sets or gets a public variable.


Four Principals of Object Oriented Programming

I’ve seperated out two of the principles because because of the way I’ve implemented them.

Encapsulation and Abstraction:


Encapsulation:

Encapsulation is making variables public, private, or protected.

The way it’s implemented here is pretty simplistic. I’ve made the text of the JSON file private, so anything outside the function can’t modify it.

Abstraction:

Abstraction is the process of making private variables accessible through public functions.

The way I’ve implemented abstraction is a bit different than most examples. I’ve used a public variable and its getters and setters to interact with the private variables.

Inheritence and Polymorphism:


Inheritence:

Inheritance is when you make a class derive from another class. It inherits properties and methods from the parent class.

My FlashLight class above inherits from my item class. The item class provides variables like itemName, which stores the item name.

Polymorphism

Polymorphism in OOP is both overloading and overiding functions.

Overloading a function is when you make a function with the same name but give it different parameters. This is to give a single function multiple uses under the same function.

Overriding is used when a virtual function is defined in a parent class, and you need to change the function to conform to what the current class needs.