This actually took me a lot faster than I thought it would, like all things apparently.

I was actually very surprised how little code clean code requires. Allowing for dynamic runtime creation of resource and building lists was pretty simple in C#.

Eventually I want to allow the player to edit the list from the main menu, have different resource lists to however they want, and I’ve taken the first steps to making that a reality.

Anyways, we made lots of prefabs, we made a bunch of draggable code, time to show it off:

First step was rigging up draggable objects. Each colony is currently set to a random number of buildings between 8 and 25 I believe. Each building is generated in the panel on the side, and the draggable is generated. When I drag, it creates a clone of it in case we want to do more than one. The slots trigger an event and have the building data inside of it. If the colony doesn’t have the required resources, the object is just despawned into an object pool and nothing happens. Otherwise we build the building.

So now here we have the left panel dynamically create whatever resources the colony has, and creates a text prefab. This is robots and matter, and it only updates when there’s a change to the resources, not every frame. This is triggered through call backs, which have to unregister and reregister when switching colonies, which is my ~fave~ to code.

Here I have the buildings now wired up to create building. I had a bug where I forgot to reset the resource timer on the building, so it incremented every frame.

I decided to use the entire name of each resource, and to give some feedback on how much of each resource you’re making. Red for a deficit, Green for surplus. Easy visual aids to help you make decisions.

Here we added a tool tip to display building costs, so you know which resources you need.

So then there was the matter of state. None of my classes really know about each other, besides the parent classes that pass state to them. The bottom slot panel doesn’t talk to any of the other panels, it just grabs the event when something drops on it.

Ok, but what if we change colonies? How does the slots remember what building is in them? How does it remember in which slot it used to be? I realized I didn’t have any of this information available, and who knows which slot I’m pulling out of the spawning pool.

So a second refactor. Instead of an int to keep track of max buildings, I used a dictionary of <int, Building> and slotted the building into the dictionary, null if it’s not there. Then passed this to the script in charge of spawning the slots to grab the correct slots.

This actually introduced a clever bug. You see, the Buildings are reference, so the ones inside the dictionary aren’t the same object as the one inside the prefab. So even thought I’m passing a Matter Fab Building, it’s not the same as the one in the dictionary, so it says it can’t find the key. I solved it by just storing the string of name of the building instead of the object itself. I thought it was an interesting bug.

So now the colony works, we can switch colonies, I can have multiple colonies, we use spawn pooling to get all the UI prefabs from destroying constantly, reusing them, and it’s all dynamic and if I wanted to change any resources and buildings, I just do it in one file and it works. Good stuff.

Next up, we do some early play testing to balance the resources so that one colony is fun, and then hook up the end game to it so that I have a vertical slice of my game. Start cleaning out all these old files from my project of all the hard coded sloppy code. The old code had about 5k lines of code, and now we’re at 1k, so it was a good refactor and I’m happy with my clean code ability for the first time.