{"id":8818,"date":"2013-03-15T15:44:55","date_gmt":"2013-03-15T19:44:55","guid":{"rendered":"https:\/\/www.spacesector.com\/blog\/?p=8818"},"modified":"2013-06-28T10:35:24","modified_gmt":"2013-06-28T14:35:24","slug":"making-a-space-4x-game-modding","status":"publish","type":"post","link":"https:\/\/www.spacesector.com\/blog\/2013\/03\/making-a-space-4x-game-modding\/","title":{"rendered":"Making a Space 4X Game: Modding"},"content":{"rendered":"<p>For my next article I&#8217;m going to explain how I implemented modding. Please be aware that this is a more technically focused article.<\/p>\n<p>I wanted to make sure the game was moddable because it can be lots of fun for the players: either by total conversions or just changing a few options and increase the game&#8217;s longevity. The game generally sticks around longer. That can only be a good thing!<\/p>\n<h3>Mod Format<\/h3>\n<p>I decided I would take the common approach of using XML files to hold the majority of the game data. I like XML because I feel it gives the players readable files that can easily be edited. It&#8217;s also generally easy to work with. However, I wanted to make sure these were \u201csimple\u201d XML files. So I don&#8217;t include any of the more complex features you can do with XML (ie: DTD&#8217;s).<\/p>\n<h3>What can you mod?<\/h3>\n<p>I made it possible so the majority of the game data can be modded. This includes, but is not limited to:<\/p>\n<ul>\n<li>orbitals (planets)<\/li>\n<li>stellar objects (stars)<\/li>\n<li>resources<\/li>\n<li>technologies<\/li>\n<li>buildings<\/li>\n<li>trade guilds<\/li>\n<li>technology upgrades<\/li>\n<li>races (which includes all necessary starting values)<\/li>\n<li>ship types (ie: destroyer, battleship values)<\/li>\n<li>ship components<\/li>\n<li>particle effects (there is actually a tool present to make these)<\/li>\n<\/ul>\n<p>You can also mod parts of the UI, namely the look and feel. Since <a  href=\"http:\/\/twl.l33tlabs.org\/\" target=\"_blank\">TWL<\/a> is defined by XML, this fits in nicely. I do not expect anyone but the hardcore modders to really try to mod the UI. TWL can be fairly complex if you are not familiar with UIs and programming in general. But at least the option is there.<\/p>\n<p>How does the XML look? Here is an example for one of the races:<\/p>\n<p><a  href=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceXML.png\" class=\"thickbox no_icon\" title=\"raceXML\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8819\" title=\"raceXML\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceXML.png\" alt=\"\" width=\"600\" height=\"427\" srcset=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceXML.png 750w, https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceXML-300x213.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>This is an image of the modifier section and the starting ship components the empire knows. As you can see, the modifiers are listing what are the valid modifiers for the various weapons (WEAPON_1 can be marked as LONG_RANGE or RAPID_FIRE during ship design).<\/p>\n<p>Here is another example of defining a ship type:<\/p>\n<p style=\"text-align: center;\"><a  href=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/shipTypeXML.png\" class=\"thickbox no_icon\" title=\"shipTypeXML\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8820 aligncenter\" title=\"shipTypeXML\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/shipTypeXML.png\" alt=\"\" width=\"528\" height=\"172\" srcset=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/shipTypeXML.png 528w, https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/shipTypeXML-300x97.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/a><\/p>\n<p>This is one of the rare ship types you can find. You can see how it can hold loads of components (4,000 spacial units) but costs A LOT! (27,000 industry). You can add or change these to your liking.<\/p>\n<p>I also wanted to have modding for some uncommon elements of the game. I wanted to be able to allow the player to specify their own unique maps that can be outside of the game&#8217;s usual settings. So these are essentially pre-made maps. You&#8217;ll be able to specify the exact locations, and every single piece of useful information in these pre-mades. You could make up the Star Wars galaxy (this is pretty popular) if you wanted too, or something else. Yep, I&#8217;m a big Star Wars fan.<\/p>\n<p>I think that having the ability to make pre-made maps is important. But I wanted to take it one step further. Since having to define every single piece of information could be a lot of work, I&#8217;m going to support random generation for various elements. Maybe you only want to have one or two unique planets. Maybe you just want to place certain stars in certain locations. Or maybe you just care about having their names right and everything else about them random. You&#8217;ll be able to do this! Random tags will exist to generate information you don&#8217;t care about it.<\/p>\n<h3>What can&#8217;t you mod?<\/h3>\n<p>Right now the main parts that you cannot mod are: any of the AI \u2013 fleet AI, game AI or anything else. For the time being, this is going to be hard coded. I may change this depending on how much funding I receive in the future.<\/p>\n<p>You also cannot change any of the locations of the UI or the components on them. I don&#8217;t think I&#8217;ll ever support this.<\/p>\n<h3>Some Pit Falls<\/h3>\n<p>I ran into some pitfalls while coding up the game to support modding. The first one is maybe not so obvious to the average person: the time and effort to require to support modding. It took me quite awhile to make up the infrastructure to handle this feature. The reason is because all of the data is not only defined in the code but is now outside, in a file somewhere. This means that the game has to be able to load and read the various data files, then build the in memory objects to represent it. This can be a big project since more code and testing needs to be done.<\/p>\n<p>Another issue was with the XML parsers. I started to write nested XML files in some cases, namely the races XML file. The Race file has everything that defines a race: name, bonuses\/negatives, starting locations(which include the stellar objects, orbitals, resources, built structures, etc), starting technologies and so on. As I&#8217;m sure you are thinking, it gets nested fast! Also I started using the same tags in different files, such as the modifier tags (gives a modifier to something, like +10 Industry) and resource tags (defining what resources are required to build something). The races.xml and technologies.xml both use the modifier tags. I wanted an easy way to be able to reuse the parsing code but make it obvious what&#8217;s going on code-wise. This would help me maintain the game and extend it for the future (which I intend to support for a long time!) easier. We should always strive to have some level of code reuse in our programs.<\/p>\n<p>There are many XML parsers out there, DOM, SAX, and various pulling parsers. I usually use SAX and sometimes I use DOM. I&#8217;m personally not a fan of pulling parsers. All of these parsers were not capable of easily parsing many nested documents without writing lots and lots of code. I haven&#8217;t been able to find a good\/easy way of using SAX to do many nested statements and DOM is just a pain to use sometimes, plus it isn&#8217;t as efficient as SAX. So I decided to write my own parser!<br \/>\n<a name=\"pewpewparser\"><\/p>\n<h3>The Pew Pew XML Parser<\/h3>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/PEWPEW.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8821\" title=\"PEWPEW\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/PEWPEW.png\" alt=\"\" width=\"468\" height=\"60\" srcset=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/PEWPEW.png 468w, https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/PEWPEW-300x38.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">(cheesy banner)<\/p>\n<p>I named this after nukers(casters) in MMORPGS \u2013 they blast (pew pew) mobs with spells. You can think of it as being able to blast away the nestings of the XML file with little effort. This is built on top of SAX.<\/p>\n<p>Now, this is a bit technical, so bear with me. Feel free to skip to the <a  href=\"#mod_support\" target=\"_self\">Mod Support section<\/a> if you aren&#8217;t interested.<\/p>\n<p>Where in SAX, you simply read a file and then implement the code to read the data in and decide what to do when a tag comes with no state data, the Pew Pew Parser works differently. With Pew, you attach a top level filter to a parser and provide a file name. This filter is responsible for looking at the tags and deciding what to do. Each filter has \u201ctags\u201d and then \u201cgroup tags\u201d. The \u201ctags\u201d are straight data, like name, description, or ID. The filter will fill this in on the correct in-memory object. So for example, we can have a Race object with Name as a field. The filter would read in the name tag and then insert that data into the Race&#8217;s Name field.<\/p>\n<p>Here is an example:<\/p>\n<p><a  href=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter1.png\" class=\"thickbox no_icon\" title=\"raceFilter1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8822\" title=\"raceFilter1\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter1.png\" alt=\"\" width=\"600\" height=\"313\" srcset=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter1.png 917w, https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter1-300x156.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>The other part are the group tags. These group tags are for nested tags. You attach a filter to this tag. So when the parser detects a group tag has been read, it&#8217;ll pass control to the next filter in a stack format. That filter will be able to handle everything it&#8217;s supposed to. For example, I could have a ModifierFilter that handles modifiers for something (in this case a Race). So when the XML gets to a point where the tag appears, the parser will pass control from the RaceFilter to the ModifierFilter.<\/p>\n<p>Then the ModifierFilter will be able to handle everything that is supposed to happen during a Modifier. The beauty of this is&#8230;you guessed it&#8230;I can reuse this code for any XML file.<\/p>\n<p>Here are the tags for the RaceFilter:<\/p>\n<p style=\"text-align: center;\"><a  href=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter2.png\" class=\"thickbox no_icon\" title=\"raceFilter2\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8823\" title=\"raceFilter2\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter2.png\" alt=\"\" width=\"572\" height=\"319\" srcset=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter2.png 572w, https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/raceFilter2-300x167.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/a><\/p>\n<p>You can see the normal tags (name, description, etc) and then the group tags (such as mod). When a group tag is seen, the parser will automatically change to that filter (so the ModifierFilter will be called). When the ModifierFilter is completed, it&#8217;ll return control back to the RaceFilter. The same goes with the improvement tag.<\/p>\n<p>Using this parser I have been able to define sets of nested XML tags \u2013 and have successfully hidden it from the modder. I&#8217;ll also be able to use these filters to load saved games.<\/p>\n<p>I do have some code to do proper error reporting so if the parser finds something it does not like, it&#8217;ll display an error message. This helps figure out what went wrong before you even play the game. As such, a modded game will be more stable.<\/p>\n<p>Even though it took me a few weeks to get this written, I can reuse it constantly in this game, as well in future games I make. Overall, I&#8217;m pretty happy with my implementation.<\/p>\n<p>On a side note: This parser cannot be obtained anywhere on the web. I&#8217;m thinking about releasing it for free use. I do not know if anyone is interested in using this thing but if there is enough interest, I may release it. Feel free to contact me outside of this article at: <a  href=\"mailto:dayrinni@dayrinni.com\" target=\"_blank\">dayrinni@dayrinni.com<\/a><br \/>\n<a name=\"mod_support\"><\/a><\/p>\n<h3>Mod Support<\/h3>\n<p>It is my goal to try my best at supporting the mod community. I have a couple of goals here:<\/p>\n<ol>\n<li>Ensure mods are stable. I really do not like it when I&#8217;m playing a modded game and it is prone to crashing. I will try my best to resolve any issues with mods crashing the game. I&#8217;ve tried to write a lot of code that handles error conditions gracefully, as well as having the Pew Pew Parser ensure the data from the XML files is in a more valid format and it has all of the right data.<\/li>\n<li>I will take mod feature requests and attempt to put them into the game. So maybe a player needs a special function for a weapon type or something. I&#8217;ll try to honor this request. Of course, this is no guarantee, and I may get more requests than I can fulfill, but you can be sure I&#8217;ll try my best in regards of adding new features and functionality.<\/li>\n<\/ol>\n<p>I feel this is an important investment of my time as I want the game to be around for many years to come \u2013 either in the base game or modded in some form. I really love playing Hearts of Iron 3 mods, but some of them just do not jell well with the engine and they crash a lot. I would love to be able to have my game be stable with mods.<\/p>\n<p>Finally, I have been documenting the XML files with explanations on how things work. I&#8217;ve also written documentation on the game&#8217;s website. So anyone should be able to easily find out how to mod this game and get to work making their favorite mods faster!<\/p>\n<h3>Intended Side Benefit<\/h3>\n<p>With everything I&#8217;ve done so far for the game in regards to modding, there is one <em>large<\/em> benefit I am getting: I will be able to design the content using my modding tools. Generally, for me, using a tool (like these XML files) to define the game values is faster than writing the content in the code (ie: defining ship components in tables or classes). So in a way, I save time in the end. This is the same for the documentation \u2013 it&#8217;ll help me remember what does what if I haven&#8217;t looked at it for a while.<\/p>\n<p>I can also tweak game play aspects without having to recompile the code \u2013 I can simply tweak values and see how they work. This&#8217;ll help with balancing and ensuring good game play quickly.<\/p>\n<h3>What is left?<\/h3>\n<p>There are a few items left, namely being able to have a separate mod directory and the functionality to turn the mod on\/off without editing the main data files. I&#8217;ll be working on this later, after the game is mostly completed. Then the other items deal with changing many of the \u201chard coded\u201d values in the game moddable, such as size of fleet combat nodes, and so on. These are defined in the game&#8217;s code at the moment so I&#8217;ll need to transfer them to files (I have a class that handles all of this, so it isn&#8217;t too much of an effort).<\/p>\n<h3>Conclusion<\/h3>\n<p>So there you have it \u2013 this is how I&#8217;ve modded the game. I hope to be able to support many mod types such as Star Wars, Star Trek, Mass Effect, or any of the popular Sci-fi IPs out there.<\/p>\n<h3>What am I currently working on?<\/h3>\n<p>I decided to add this section to give you all an idea of what I&#8217;ve been up too. <a  href=\"https:\/\/www.spacesector.com\/blog\/tag\/making-a-space-4x-game\/\" target=\"_blank\">These articles<\/a> usually are about topics I completed a long time ago, sometimes up to 6 months. My policy is to mostly write only about what I&#8217;ve done, not what I will do. I do not want to create any false impressions about the game.<\/p>\n<p>I&#8217;ve been spending time programming the fleet combat system \u2013 which includes ship components, ship design, colonization, of course, the combat. I will be doing a large article on this and it&#8217;s one I&#8217;m personally really excited for! But we&#8217;ll just have to wait until that article appears in the coming months.<\/p>\n<p>Thanks for reading and I look forward to your comments!<\/p>\n<p><em><span style=\"color: #227dc2;\">dayrinni has been a Space Sector contributor since October 2011. This is his first foray into writing articles for any review site. He is an avid gamer in the genres of 4X, Strategy, MMO&#8217;s and RPGs. Finally, he has been the implementor of several MUDs and is currently working on a space 4X game that offers large scope and complexity. See all dayrinni&#8217;s posts <a  style=\"color: #7a1a99;\" title=\"dayrinni's posts\" href=\"https:\/\/www.spacesector.com\/blog\/tag\/dayrinni\/\" target=\"_blank\">here<\/a>. In particular, check his <a  style=\"color: #7a1a99;\" href=\"https:\/\/www.spacesector.com\/blog\/tag\/what-makes-a-good-game\/\" target=\"_blank\">&#8220;what makes a good game&#8221;<\/a> and <a  style=\"color: #7a1a99;\" href=\"https:\/\/www.spacesector.com\/blog\/tag\/making-a-space-4x-game\/\" target=\"_blank\">&#8220;making a space 4X game&#8221;<\/a> series.<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my next article I&#8217;m going to explain how I implemented modding. Please be aware that this is a more technically focused article.<\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"registered_only","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[177,1],"tags":[1502,373,1173,539],"class_list":["post-8818","post","type-post","status-publish","format-standard","hentry","category-game-design","category-space-strategy-games","tag-game-design","tag-game-development","tag-making-a-space-4x-game","tag-mods-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts\/8818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/comments?post=8818"}],"version-history":[{"count":0,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts\/8818\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/media?parent=8818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/categories?post=8818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/tags?post=8818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}