Categories
Projects

BattleGrid Update – Screw Draw Calls

Trying to get my draw calls down is extremely frustrating. I’m slowly learning what I’m doing that’s causing the batching to break, but I’m also finding out that some of what I want to see (draw call wise) simply isn’t going to happen.

I just found out that the vertex buffer for draw calls is around 4000 vertices (at least as of 2009). For a 30×30 map, my grid alone is 3600 vertices (900 grid tiles * 4 vertices per tile). For a 40×40 map (my largest maps), that comes out to 6400 vertices – already too many to batch. So, trying to get my terrain and structures to batch together is futile anyway. On the other hand, it seems like I should still be able to get down to just a few draw calls. Even if I max out that vertex buffer each time, I should still only be making about 4-5 draw calls, not the 86 I’m seeing. Those 80 extra draw calls are really causing me grief, and Unity can’t be so stupid about ordering things that it can’t properly organize the batches…

So for the time being, I’m going to forget about draw calls and get back to the fun stuff – AI and bug fixing. I’m going to do some optimization to the AI to get it running smoother, then fix the few things that are still slowing things down (like rendering map previews). Maybe I’ll also take a stab at the map submission website. Anything to get my mind off those damned draw calls.

Categories
Projects

BattleGrid Update – Graphics

Since I’m kind of stuck progress-wise until I figure out what to do about the draw calls in my game, I decided I’d do a post on some of the updated graphics in BattleGrid. Looking over the images I’ve posted in some previous posts, I realized that things have changed a lot since I posted those images.

New Tile Graphics
Tiles have gotten two big updates – first, I reduced the number of planes used for a single tile from three (base, owner highlight, and an extra highlight) to one, which cuts the number of polygons on a single map by two-thirds. Always a good thing.

Second, I found a really cool (and more importantly, free) circuit board drawing that I put on the tiles. I love the way it looks, and it makes the grid a little less barren.

The new map tile, with a circuit board texture.
The new circuit board tile.

The “contested” tiles also got a graphics update. They’re now “cracked” tiles. I like them, but they’re likely to see some updates before the final version:

The new contested tile.
The new contested tile, with a “cracked” look.

They look best on defense maps where it looks like the gateways have “infested” the map. Speaking of which…

New Gateway Texture
The original gateway texture hasn’t been shown before, but it was using a noise-generation function to animate the portal. It looked neat, but killed performance, so I scrapped it. I use a flat texture and a particle system now, which looks pretty good:

Gateway
The gateway, used by waves of enemies to invade the grid.

If the gateway looks a bit like the obsidian portals to the Nether from Minecraft, that’s good because it’s exactly what I intended. If they don’t… Well, that works, too.

New Buttons & Windows/Dialogs
I’ve said before I hate the gradient backgrounds on most of my buttons. The idea I’ve been thinking about for a while now is using scan line backgrounds, sort of like a holographic display. I finally created some, and I’ve updated practically the entire UI to use them.

The new “holographic” buttons come in two styles – the main menu buttons:

New play button
A good button for games to have.

And the window/dialog style:

Close button
Used for… closing things.

The dialogs themselves are supposed to look a bit like a holographic display. They’re lacking in depth, though, but with my new UI framework, I should be able to eventually do something about that.

Pause menu
Kind of boxy.

The new UI uses a package called “NGUI”, which is actually pretty cool. I’ll write a separate post about NGUI soon, since it has totally changed how I think about the UI.

New Factory Model
The last time I mentioned factories, I said the factory was likely to see another model update, and they have. The new model looks a lot like the original:

The newer new factory.
The new(er) factory

However, this one is animated, so when it’s building the “wings” fold down and spin:

The newer new factory, building a tank.
The new factory looks better when it’s moving.

It looks pretty cool in action. I also added a “platform” under the factory to make it a bit easier to tell that the factory covers 9 tiles. The platform isn’t in these images because I took them before I added it. Maybe next time.

Categories
Projects

BattleGrid Update

I’ve scaled back my work on BattleGrid a bit. Through February and March, I was working on BattleGrid like it was a second job, and I could definitely feel some pressure building. I was making a big push to get something ready for PAX, and even though I missed that target, I’m happy with the work I got done. It’s just not a sustainable pace.

That said, I’ve been working on the problem I mentioned in my last BattleGrid post – draw calls. I’ve been trying to get batching working properly, and to that end I’ve got my texture atlases set up and all my meshes updated to use the atlas. In theory, a scene in my game should be using less than 10 draw calls: 1 neutral material (for the neutral player and projectiles), 4 colored materials (1 for each player), 2 for the UI (foreground and background), and a few for particle effects. Instead, I get 300 – 400 draw calls. For those of you keeping count, that’s 3x more than before creating the texture atlas and updating all my meshes. It’s the exact opposite of what I expected.

I know the atlas is supposed to work, so there’s obviously something else I’m doing wrong, I just don’t know what yet. Maybe I’ll have an epiphany and realize I’m doing something stupid, or maybe I’ll spin my wheels for a month or two and get very little done. I’m hoping for the epiphany.

Categories
Projects

BattleGrid – PAX

As stated before, I was aiming to have BattleGrid playable on my iPad by PAX so I could get feedback from strangers. While the game is certainly playable on my iPad, it still suffers from framerate problems when things get hectic, so I’m probably not going to be showing it to strangers – it’s simply not ready for that.

I think I’ve figured out how to fix the framerate problem, I just don’t know exactly how to do it, and the only tools I’ve found to help either don’t work well or are a bit complex.

The problem is that iOS simply can’t handle drawing a lot at once. Unity uses “dynamic batching”, which means that each time it needs to redraw, it batches everything together that’s using the same material (textures + shader) and sends it as a single draw call. My game uses about 100 – 140 draw calls in busy scenes. Everything I’ve read says I should keep it below 30 draw calls for good performance. So I’m using over 3 times as many draw calls as I should.

I think I know what needs to be done to fix it – I need to use a texture atlas. This means taking all the individual textures and cramming them into a single big texture. This is the easy part.

The hard part is that the UV mapping on all the models also needs to change. The UV mapping defines what part of the texture is displayed on each face of the model. When all the textures are combined, I need to scale and offset the UV coordinates for each model, and I can’t find a tool to make this easy. In the end, I’ll likely just write a tool to do it myself, since working with meshes in code isn’t too difficult. Writing a tool to do the work for me will take some time, though, so there’s no time to do that before PAX.

I’ll install BattleGrid on my iPad 3 and see if it runs better, but I’m not getting my hopes up.

Categories
Projects

BattleGrid Update

Wow, two months since the last update. Well, the last update I actually published to the blog – I’ve had about a half dozen posts I’ve started but never finished because the things I was writing about changed before I was finished. And because I wanted to add pictures I never got around to grabbing. So this post is going to sum up everything from those half-finished posts.

I spent a lot of time last month tweaking things, figuring stuff out, and updating graphics. It was a good month of work, and resulted in most of the frustrating bugs being fixed.

Toward the beginning of this month, I sent links to a few friends to start getting some feedback. As expected, the feedback has been extremely valuable. The in-game UI has changed to something that works much better – something between the new and old UIs. I created a series of help screens based on feedback about what confuses people. I also got the idea to create a map for the sole purpose of testing the frame rate when things are hectic, and that one map is the map I play all the time now as I improve things.

A week or two ago I installed BattleGrid to my iPad. It runs, but as of the last version I installed, the controls weren’t working, and the game ran terribly. On my test map, the FPS would drop to 3. Yikes. It’s good that it’s running, though.

Unity also decided to give away their “Mobile Basic” kit, which includes iPhone and Android publishing, so BattleGrid will probably end up on Android tablets at some point. I’d need an Android tablet to test with, so the iOS version will still come first and long before the Android version.

Some of the developers in the Unity Asset Store decided to put their stuff on sale with Unity giving away the mobile kit, so I’ve spent about $200 on development tools to make my life easier. I’ve been pretty pleased with most of what I’ve bought, but there’s a downside… I bought a new UI framework, and I’ve been moving my entire UI over to use the new framework. The framework is fantastic – it’s much more efficient than what I had and much more flexible, but it has taken time to figure out its quirks and how to use it properly. I also got a tool that handles gestures for me, so I’m replacing a lot of my control code with updated code that uses the gesture framework.

My productivity has also suffered because Mass Effect 3 was released and I’ve spent a ton of time playing it.

So, I might not hit my PAX deadline for having something playable… I’ll see what I can get done this weekend and next and cross my fingers.

I’ll also try to post some more updates soon about the changes I’ve made.

Categories
Projects

BattleGrid Update

It’s been a while since I’ve posted any updates about my game, and that’s mainly because I’ve been spending a lot of time working on it and things have been changing pretty quickly. There’s about a half-dozen posts I’ve started in the past month about things that have already changed.

Anyway, toward the beginning of this month, I sent links to the game to a few people to start getting feedback. As expected, the feedback has been extremely valuable. The in-game UI has changed to something that works much better, and I’ve started work on help screens based on feedback about what confuses people. I also got the idea to create a map for the sole purpose of testing the frame rate when things are hectic, and this one map has been very useful as I figure things out.

What people don’t know yet (including my few testers) is that the game is running on my iPad now. Granted, it runs terribly, but it runs. I’ve only got about three weeks before PAX, so having it running on the iPad is a huge step toward my goal.

The other big news is that Unity made their Android basic kit free earlier this month, so there’s a good chance BattleGrid ends up on Android tablets at some point. Of course, I need an Android tablet to test with, so the iOS version will still come first, and long before the Android version, since I need to see the iOS version make some money first…

Oh god, I hope it makes some money. Enough to pay off the development tools and licenses, at least.

Categories
Development

There’s always more to learn…

One of the reasons I love programming is that there is always something new to learn – a new language, a new technology, a new technique. The bright side is that it’s always fresh and fun. The downside is that sometimes it makes you feel like an idiot.

I’m going through this “new and exciting” experience with my game. Unity is a fantastic development platform – it’s flexible, it’s easy, and it’s powerful. I’ve been working with it for over 2 years now, so I thought I had figured a good bit out about how to do things. But I’ve been struggling with getting a decent frame rate even though my game is really pretty basic. My game has tiny worlds (minuscule, really), mostly static meshes, low-poly models, etc. However, the frame rate would frequently dip below 30 FPS… Totally unacceptable on my fairly beefy computer.

Now, a lot of my problems are just the result of a misunderstanding about how Unity optimizes some things. Unity uses an “Update” method on objects that need to be updated every frame. I was using this method all over the place to manage things that needed to happen over time – but not necessarily every frame. Well, as it turns out, having that Update method means Unity needs to call that method, which takes time, even if the method doesn’t do anything. So, I went back and stripped out most of my “Update” methods and replaced them with coroutines. And instantly I got a 20-30 FPS increase.

But my frame rate was still sitting in the mid-50s for a simple game. I’ve spent hours tweaking some of my most commonly used code to eke every last drop of performance out of it. I’ve done a pretty good job, and created a lot of code that’s useful for time-slicing and scheduling operations to run over multiple frames. Still not enough, though.

So today I’m doing some thinking and out of nowhere, an idea pops into my head: “What if I try triggers for targeting?” A trigger in Unity is a collider that doesn’t affect physics. So, it can tell when another object enters, moves around within, or leaves its collider. You could use them to trigger an enemy spawn when the player gets close, or fire a trap, or whatever. Stupidly, I had not been using them for targeting. Instead, I was periodically scanning every target within range and finding the best target. With triggers, I can instead handle this as new targets come into range – when a new target comes into range, check to see if it’s “better” than the current target. This has two side effects: it’s doesn’t take as long because you’re only checking a single target, and it happens as soon as the target enters range. This means that turrets will now immediately notice a new target and react to it instead of having to wait for the next target scan.

Luckily, my AI design was pretty good, so making the switch was easy and I got it tested within an hour. My frame rate is now sitting in the 100+ range, though I still want to try it out on a really busy battle. If things work out, I won’t have to go to some of the “drastic” measures I was thinking about (like enforcing a unit limit).

Categories
Development

Game Development

So over the course of developing BattleGrid, I’ve learned a lot about game development – nitty gritty stuff that it’s hard to learn without actually doing any of the work. I find a lot of this fascinating. I develop applications for a living, and while the basics are all the same, the details about how to do certain things are totally different. Case in point:

I don’t care how quick it is, split it up over multiple frames
There are a lot of things that I could do with a single line of code. I’m a bit of a code snob – I love clean, elegant code that’s easy to read, understand, and maintain. (I have a coworker that doesn’t feel the same way. We don’t get along.) In game development, though, you sometimes have to take a single unit of work and split it up over multiple frames. This means doing a bit of extra work to keep track of your progress each frame, so you can continue on the next frame. There are elegant ways to do this (I think I have a decent method), but it will never be as nice as the one-line beauty that I had before.

An example would probably help. Take targeting. Each turret and unit in BattleGrid has a targeting component that picks out the best target on the map. I’ve simply defined “best” as “closest”, so we just need to do a range check to find the closest. When I started, I just did the work, something like:

public void FindTarget()
{
    var target = this.Targeting.FindClosestTarget();
    this.SetTarget(target);
}

This worked great, except when an enemy was destroyed and the 5 turrets targeting it all decided to find new targets. Finding a target for an individual turret was easy and quick enough to do in a single frame, but if too many turrets all tried to find a target at the same time, the game starts to chug. So, I developed a simple queue:

TaskQueue.Enqueue(this.FindTarget);

Without going into any detail about what that means, that just puts the “FindTarget” action onto the task queue. The task queue executes one operation every tick (about 1/60th of a second), so if 5 turrets need targets, they just queue up their “FindTarget” actions, and targets are found over the next 5 ticks. This is fast enough that a player never notices, but slow enough that it doesn’t hurt framerate.

All this talk about targeting loosely relates to my next point:

AI is hard, but rewarding
I’ve never had kids, but I imagine teaching a kid to do something and then seeing them do it gives about the same warm fuzzy feeling that seeing an AI do what you taught it gives me. I’ve done a lot of work on the AI in BattleGrid, and I know there’s still plenty to be done. One of the hardest things to figure out was how to “teach” the AI what to build in given situations. I had a few ideas that were really complex, but ended up with a pretty simple idea – give the AI “effectiveness” values to work with.

This is what players do without thinking too much about it. “Oh, that guy has a blaster turret protecting his base. Missile turrets are longer range and can destroy that turret easily. I’ll build a missile turret.” I stored “effectiveness” values for every structure in the game. For instance, I’ve set it up so blaster turrets are highly effective against all mobile units, and artillery is effective against all stationary structures.

Now, I knew I had set these values and taught the AI how to use them. Even knowing that, the first time I saw an AI build a missile turret just out of range of one of my blaster turrets, I was amazed. That’s something a player would do – build a longer-range turret out of range of the shorter-range turret.

I’m still amazed when I see the AI counter my structures with the same things I would. I build artillery, they build shields and tanks. I build tanks, they build blasters. It’s the standard rock-paper-scissors scenario, but it’s fun to see it in action.

Continuing on AI…

Sometimes the AI needs to cheat
One of the things I noticed while playing is that the AI is much more difficult when it can afford more. When I dropped the price of all the structures (for testing), the AI could more effectively counter anything I did because they had the cash to do it. So, for more difficult AI settings, the AI cheats a bit… They get twice as much income as the player. This gives them significantly more cash, which means they’re not only thinking faster, they’re also building more that the player has to fight through. Fortunately, the AI isn’t particularly devious or clever, so the player can still beat them, but it takes significantly more work, which is exactly what I wanted for more difficult AI.

Balance is hard
I’ve struggled to get everything to feel balanced. The more options you give a player, the more difficult it is to make sure those options are balanced. I want BattleGrid to support multiple styles of play and make them all viable (playing offensively or defensively), but that makes it incredibly difficult to make sure each play style gets a fair shake. I don’t really have a solution here; it just takes a lot of testing and tweaking.

There are many more examples, but this is enough for now. Hopefully over time I’ll start to find elegant ways to solve all these problems; ways that I can carry forward into other projects.

Categories
Projects

BattleGrid – Factories

I’ve been searching for a clever solution to an issue I’ve had with factories since I introduced the “5 active units per factory” limit: how do I show the player how many units are active?

The straightforward solution is to display some sort of bar above the factory, like a 5-segment progress bar that fills up depending on how many units are active. I didn’t really want to do this, though. Mostly because my experiments in getting UI elements to line up with 3D objects have been unsuccessful.

One of my other ideas was some sort of antenna on the factory that sent out little pulses to the units periodically. I liked the idea, but it doesn’t solve the problem – there’s still no indication of how many units are active unless a player waits for the next pulse and counts the pulses. I liked the idea, though, so it’s been sitting in the back of my head a while.

Then today, a neat idea struck me that I thought I’d try out. Enter the new factory:

The new, more compact factory
The new, more compact factory

The new factory is a bit more compact than the old, but for good reason. It needs some room for the new drones that hover around it:

The new factory with a drone hovering around it
The new factory drone, circling the factory

A new drone is spawned for each unit the factory builds, so you can easily see how many units are active by counting the number of drones. And every few seconds, the drones send out one of those “pulses” I mentioned:

A pulse sent from one of the new factory drones, hunting down its tank.
A pulse sent from one of the new factory drones, hunting down its tank.

The pulses seek out the unit the drone is attached to. These pulses look fantastic (a screenshot doesn’t do them justice) and add a little new animation to the game.

The factories are likely to get another change later since I’m not totally satisfied with the new look, and the pulses, while looking pretty cool in-game, are still a little drab, so I’ll have to dress them up a bit.

Categories
Projects

BattleGrid – UI (Continued)

The switch to the “select a cell, then a structure” UI when building structures looks and works great. There’s only one problem: not all my structures fit in a 1×1 cell. Some structures are 2×2, some are 3×3. The new UI still worked for these sizes of structures, but didn’t feel right. For 3×3 structures, the tile you picked is in the center of the structure, which is OK. For 2×2 structures, though, the tile you picked is the upper-right of the 4 tiles. I lack a screenshot, so this will have to do:

New build menu, showing the problem with 2x2 structures
The problem with the new build menu

I liked the new UI too much to switch back, though. It felt right, even if it wasn’t working great yet. So I did some more thinking, added a bunch of code, and a few iterations later I have things working pretty well. The only downside is that building requires one extra tap/click. However, requiring that extra tap works out pretty well.

When you tap a cell, the build menu appears. When you select a structure, the game finds the appropriate position closest to the tile you selected and places the structure there. Then the camera re-centers on the structure. For 1×1 (or 3×3) structures, there’s no change (though I might add a slight zoom-out if you’re too close for 3×3 structures), but for 2×2 structures, the camera will line up on the intersection of the 4 cells:

New build UI for 2x2 structures
Building an outpost.

If the location is valid, the cells with light up green. If not, the invalid cells will light up red:

The new build UI for 2x2 structures when the location is invalid
Invalid outpost placement

If all the cells are green, you can tap the structure, and it will start building. The part that works out well with the switch is that you can select a structure, and if the location is invalid, you can use the standard camera control gestures (swiping and pinching) to pan and zoom the camera. The structure you’re working on will stay centered the whole time, and when you’ve moved to a good position, you can tap it to build it. This is an improvement over the old UI, where you’d tap a structure and you could drag it around, but you lost control of the camera, so the structure had to go somewhere within the camera’s view when you selected the structure.