Your abstractions are a liability
Your abstractions are a liability.
Often we jump to an abstraction in our code because it seems ‘obvious’ or just seems to fit our understanding of our domain. We want to make our code ‘clean’, so we look hard for ways to remove duplication and ‘code smells’.
As our codebase grows, our abstractions often pass their sell-by date, yet they persist as we attempt to work around them. Removing bad or redundant abstractions as our understanding of our codebase grows is essential.
As an example, consider this simplified game loop. This is pretty much how the Sol Trader inner loop worked until about a week ago.
(I’ve placed the methods within the class definition for brevity, but in C++ they would probably be in a different file. This is important.)
The problem is that the scope of _running
is propagated further than it should be. It’s possible to stop the game loop running from both the update()
and draw()
methods: in fact, anywhere in the Game
class at all. It’s also not clear what the designer of this class intended the behaviour to be. Where exactly should _running
be updated?
With a large and complex set of methods (initializing and tearing down a modern
game is no small feat) then the _running
could potentially be set from a
large number of places - and perhaps set and set again during the same frame!
Just because a variable is private doesn’t save you from a large class giving access to that variable to many code paths.
Inlining variables
Let’s remove the _running
member variable and use a local variable instead:
By moving _running
into a local variable within the run()
method, we’ve stopped the state propagating throughout the class instance, and encapsulated the running logic where it belongs. It’s also clear now that the update()
method should decide when the game stops running, and that the we should draw the final frame before we quit.
Inlining methods
You can take this further and inline the other objects:
The real problem has now become evident. It is an insufficient understanding of Object Oriented programming - namely putting everything into noun-based objects such as Game
. It turns out there’s not actually useful game abstraction here - we are better off with a simple run()
method. Functional programming, anyone?
It seems simple, but the amount of code I’ve been able to simplify with this two techniques is astonishing. I do end up with longer methods, but by collapsing unnecessary abstractions the code becomes more straightforward and clearer.
Summary
Removing useless abstractions by inlining methods and variables where appropriate helps us to see where the real seams of our code are. It helps us to pick apart unhelpful or mistakenly classified objects and leads us to a better understanding of OO.
The wrong abstraction is (far) worse than no abstraction at all.
Share this article
Read more
Your Code Is A Liability
Every chunk of code you commit is more for someone else to read, digest and understand.
Every complex “clever” expression requires another few minutes of effort for each of your team. They must now interpret what you wrote and why you wrote it.
Every line you add limits your project’s responsiveness to change.
Your code is a liability. Never forget this.
Read moreThe Sol Trader Christmas Eve update: moddable missions
The relative radio silence from Sol Trader Towers is for a reason: I’ve been working hard on a flexible and moddable mission structure, that allows players to take a variety of interesting quests in-game.
This build is now available on the forums should you have access (there’s still time if you don’t.)
I’ve built a few missions to start with, including delivering parcels for business or personal reasons, taking characters on business trips and making other characters disappear. It’s great fun to have a variety of things to do for characters now and adds yet more colour to the game. Because it’s completely moddable, I’m also excited to see what storylines other people come up with!
Under the hood
The full details of how to create your own missions are available as a lengthy forum post, which will be kept up to date with changes and clarifications. Here’s an overview:
The missions are organised into packs, which exists under the data/missions
subfolder. If you have access to the beta builds, you’ll see there’s one pack there already: these are the missions that are built in to the game.
There are several csv files in each mission folder:
requirements.csv
: This file details the cases in which this mission might be triggered. Each character in the game has a chance of picking this mission (and becoming the ‘giver’ of the mission), based on the conditions imposed by this file.conversation_player.csv
: The extra conversation options available to the player because of this mission.conversation_ai_response.csv
: The extra options the AI can choose from as conversation responses.opinions.csv
: The extra opinion triggers, used for reactions to the generation and completion of these missions.strings.csv
: The new strings needed for the previous CSV files.
The possibilities for you to build your own missions are expanding all the time, as I add new missions triggers and possible goals for the AI.
What’s next?
At the moment it’s possible to take on any mission from any person, which isn’t very realistic. I need to allow players to gain other character’s trust, so that they will only give you sensitive missions in certain cases. Additionally it will soon be possible to start a career with an organisation, which will give you a rank, a certain amount of built in trust, and access to more senior characters.
I’m also going to be working on the in-space AI very soon. At the moment only freelance traders fly around between planets: it’s time we had passenger ships, military guards and pirates thrown into the mix.
Have a fantastic Christmas and I’ll see you all in the new year with some more updates.
Read moreNew Sol Trader beta: the science of blame and unforgiveness
Previously I wrote about how I’m modelling opinions and prejudice in Sol Trader. It’s time to put some of that information to use.
The opinions a character has of other people, based on the partial events that they know about them, will now directly affect the things that happen in the history generation. This creates new events, which will in turn feed more character opinions.
There’s a new beta available on the forums if you have insider access.
Dudley and Meredith
In the example on the left, we can see that an acrimonious divorce of Meredith’s parents has left an indelible mark on her childhood. She now has a very low opinion of her father, Dudley.
When characters are adults, they can then generate a series of ‘favours’ (or ‘missions’) that they want completed. This is a source of work for the players, although completing certain missions does have real consequences on your relationships with the target of the mission. If they find out you’ve taken a mission against them, then they won’t be happy with you.
To continue our example, Meredith, whom we are now married to, wants us to find out some potentially incriminating information about our own father-in-law, Dudley. It’s up to us whether we take it or not. If he finds out, we’ll make an enemy of him.
As the game goes on, the player will get embroiled in these relationships between the various characters and be able to directly affect their stories. Choosing what to take on and who to ally yourself with forms a major part of Sol Trader’s gameplay.
Sarina’s spiral of doom
Another example: the sad tale of Sarina, our older half sister. I picked Dagny and Warren in history generation to be my character’s parents, knowing that Dagny was cheating on her husband Hayden, mostly to see what happened. Little did I know how much it would affect Sarina, Dagny and Hayden’s eight year old daughter. When she found out about my birth, she got very upset.
She didn’t blame me, thankfully, although she never thought much of me. However, she never really spoke to our mother again, especially since her beloved father Hayden died soon after we were born.
She left home at a young age, and became a political assistant, but she didn’t make too many friends. She was doing ok for a time, only to find out that the love of her life, Richard Ruhr, had been having an affair behind her back all along.
She divorced him, got depressed, quit her job and by the time I grew to adulthood at the start of the game, she was living in a hippie commune somewhere on Mercury, trying desperately to get some gossip on her ex-husband.
New beta out now
This new beta is now available from the forum if you have purchased insider access (if you haven’t there’s still time.) Let me know if you find any other interesting stories such as these!
Read moreModelling opinions and prejudices in Sol Trader
I’ve been working hard on the Sol Trader core gameplay mechanics in the last two weeks. High up on my list was a way of generating more interesting missions for the characters to complete.
In order to have a reason to gather dirt, find locations or desire an early end for an enemy, our characters need to feel strongly about other people they know. This is where their opinions and prejudices come in.
Characters already keep track of the events they know about for each other character in the game. Now they can form an opinion of a character based on the partial set of info they know about someone else’s past.
The plan is to use these thoughts about each other to make decisions about who they’re friends with, deal with relationship breakdown, blame and prejudice.
Here’s an example of how we configure this under the hood for an occasion where a character is caught and reported for taking bribes:
Anyone knowing about this event will think the character is less deserving of sympathy and assume the character is less moral. If we’re the one catching them take the bribes, then the briber becomes much less influential over us. If we’re the one being caught, then the one catching us is definitely no longer our friend. Depending on our profession, we will brief against them or possibly try to take them out.
Now characters have opinions about others, we can use these to guide their conversation choices, who they’re likely to target, give us gossip on, etc. It’s all game design fuel for other behaviours in the game, and will combine to form interesting unexpected effects and tell original stories each time.
Next time I’ll discuss about the new events that get created in the history generation because of these new opinions. Our stylised formulaic view of history is about to become, well, a lot more messed up. Rather like real history…
Read moreSol Trader combat preview
Since the Kickstarter was successfully funded last month, I’ve been working hard the next major feature: combat!
Here’s a short video showing progress so far. If you’re on the beta of the game, head over to the forums - you can grab a new copy of the game today and start shooting things for yourself! (There’s still time to jump on the beta if you aren’t already…)
Since the Kickstarter finished, I’ve added sound effects and explosions, and I’ve done a lot of work under the hood on the entity component system to allow the to removal of components from entities.
This was fiddly as I had to change the implementation of some of my fundamental low level data structures in order to support fast removal. It’s worth it, though. Now when a ship blows up, I simply remove the components that made it a physical thing (Spatial, PhysicalObject, Renderable, Enterable, etc) and leave the components for its logical existence (Ownable, Nameable, etc) so that characters don’t forget about it.
I’ve also added the basics of ‘bad’ events when combat takes place. When a ship is destroyed, the game kills everyone who happened to be on the ship, and adds ‘killed’ events for them, blaming the pilot of the attacking ship. These events will come back to haunt the attackers in future, as word gets around about who is responsible…
Lastly, I’ve added the first draft of the inevitable “game over” screen, with a Rogue-like throwback style :) This one is a work in progress and will get more interesting later on:
If you’re the kind of person who likes to get the up-to-the-minute news on development, and doesn’t mind lots of detail, you can see the latest development notes on our Trello board. You can even comment and vote on cards - I welcome any feedback!
Read more