What Is Code Optimization? Why Do I Need It?

Being a good games programmer doesn’t just mean you understand how to make something happen in a game, it means making something happen in the most efficient way possible. Even more so for mobile development where resources are limited! As many of you know, this is often a lot harder than it sounds. I’ve decided to share some of the insights I’ve learned about code optimization that aren’t always so obvious, in the hope that it helps you to build a game that’s smoother and cooler!

What Is Code Optimization And Why Do I Need It?

GearsThe tech answer from Wikipedia, says:

In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power.

So what does this mean? In it’s most basic form, it means don’t try and figure something out the hard way if you don’t have to. In terms of how a computer works, everything you tell it to do requires Resources ( for the most part this will be Memory / RAM ) and  processing power ( The computer’s processor and usually the graphics card’s processor too ). Usually, the more complicated / long-winded your code is, the more resources and processing power it will take for the computer to complete.

An Overly Basic, Human Example

People say the human mind is like a super-computer. Let’s say someone asked you how often you went to visit your family every week. And for the sake of argument let’s say you went every Monday and Wednesday…

Most people will probably figure it out like this inside their head:

“I know there are 7 days in the week, Monday and Wednesday are 2 of those days .. So therefore I go twice a week. The answer is 2.”

If this was programmed, it could be expressed like this:   ( 7 / 7 ) * 2 = 2;

A longwinded (and very silly) way of figuring this out would be like this:

“I know there are 365 days in a year, and that is equal to around 52 weeks, of those 52 weeks I go every Monday and Wednesday, so that makes it 104 when I multiply those together .. So if I divide the number of visits I made every year by the number of weeks in a year, that makes it 2.”

In programming, that thought process would look like this:

numberOfWeeksInYear = Round( 365 / 7 );      (equals 52)

numberOfVisits = numberOfWeeksInYear * 2;    (equals 104 )

numberOfVisits / numberOfWeeksInYear = 2;

Conclusion

Technically, both methods are correct because they arrived at the same answer. But It’s pretty clear the first method was going to take far less time for you to figure out, which allowed you to move on with your life and go get a milkshake or something!

In Code Optimization the same idea applies, the faster the computer can figure something out, the faster it can move onto something else. In games, this will directly impact your FPS ( Frames Per Second), which is how many times the screen gets updated every second. The more times it updates, the smoother your game will look! =)

I hope this article explains the importance of code optimisation!

Cheers,

– Mel

 

Post Brutal – Attack And Adrenaline Ability Engine Test

[youtube=http://youtu.be/DwhZRMieoTI]

This is the second public development video of my own personal project made with Unity, “POST BRUTAL”.

Quick Background

It’s a 3D Action-RPG based in a Post Apocalyptic and Brutal environment ( .. The title makes sense now, right? ). It finds you struggling to survive in a city cut off from the rest of the world, plagued by a virus that turned the majority of the population into a zombie-like state. Unlike the typical zombie genre, the zombies aren’t the real threat, the rival factions in the city aspire to take control while external governmental influences complicate your survival even further..

In This Video

In this development video, we get a glimpse of the real time combat engine, allowing you to switch between a single melee and ranged weapon from your inventory ( the inventory will be shown in a later clip ) and literally cutting down wave after wave of zombie with gorgeously crazy gore! Limbs get torn off, heads are decapitated and blood rains down! We also activate the “Adrenaline Rush” ability, which slows down time in a matrix style effect that allows us to withstand damage for a limited time and get up close and personal with our melee weapon of choice! More abilities will be available depending on the type of character you have, such as a stealth ability allowing you to become invisible for a short time ( also coming up in a future video! ).

Notes

This video is in fact already out-of-date, we’re about 2 months ahead of this video, so much of the AI is vastly improved!

There’ll be way more videos to follow! If you like the vid, please let us know and show your support! =)

Cheers,

– Mel

Post Brutal – Unity3D Mobile Game Inspired By Zombies, Fallout and Dues Ex!

[youtube=http://youtu.be/F2kCanwGRiw]

This is the first public showcase of my own personal project made with Unity, “POST BRUTAL”.

It’s a 3D Action-RPG based in a Post Apocalyptic and Brutal environment ( .. The title makes sense now, right? ). It finds you struggling to survive in a city cut off from the rest of the world, plagued by a virus that turned the majority of the population into a zombie-like state. Unlike the typical zombie genre, the zombies aren’t the real threat, the rival factions in the city aspire to take control while external governmental influences complicate your survival even further.. The storyline get’s a lot deeper but we’re holding that back for now! =)

The greatest thing about Post Brutal is a feature set that we can’t yet share. We’re keeping it TOP SECRET because we think it’s going to be ground breaking! Not just for a mobile game, but for video games in general!

The great thing about Unity3D is that it enables independent small teams and even single developers to create commercial quality titles. There are 2 more people on the team helping me with the non-technical areas of Post Brutal. Development is coming along nicely. What I can show you is some early engine videos, starting with this one that showcases some of the early zombie AI (which has since been vastly improved!), the cool stealth / detection system, and a glimpse of the attack systems. There’ll be way more videos to follow! If you like the vid, please let us know and show your support! =)

Cheers,

– Mel

Save 50% Memory From 3D Meshes in Unity

So this is a pretty awesome optimisation I discovered while I was working on Flight Unlimited Las Vegas.

mzl.slfdyteq.320x480-75

The Problem

The game has a massive part of the Las Vegas area satellite-imaged as part of the flying area. In fact, it is over 130 square miles! This really causes problems on mobile devices as RAM is limited.

I did the usual things when trying to optimise memory usage. Reduced texture sizes for smaller objects, turned off mip-maps (more on that in a different post!), etc. Then I checked out the Profiler to discover something pretty interesting ..

The actual 3D meshes ( NOT the textures ) were taking up the vast majority of the RAM. I found this pretty shocking at first as most of the buildings in the game were pretty low poly. I guess the sheer amount of them stretching over 130 virtual square miles was enough! The meshes alone were taking around 40MB of RAM.

The Solution

After thinking about what might be causing this, I did something insanely effective that I’m sure many of us would skip or not think twice about.. Disable the NORMALS and TANGENTS from the Mesh.

NormalsAndTangents

This has certain side-effects, but luckily, none of them applied to our project.

Normals are a type of data stored in the mesh to help out real-time lights, and Tangents are used to help Shaders perform things like “bump-mapping”.

As the scene was already baked with BEAST light-mapping, the scene wasn’t going to need the normals. And the shaders we used for the environment weren’t using bump mapping so the tangents weren’t a problem either.

The result …?

Mesh Memory was brought down from 40MB to about 23MB. Almost a 50% reduction. This was the difference between iPod4 / iPad1 compatibility, which we were able to offer upon discovering this optimisation.

I’m sure some of you should find this helpful! =)

All the best,

– Mel.