Sunday, 8 June 2014

Scripting in Iron Heart

Due to extraordinary circumstances, it has been a while since my last post about developing the Iron Heart engine, however, this post will tackle my methods for fixing problems 1-3 from the previous post. As a brief recap, the three major problems were memory leaks, every frame calling of new, and a complete lack of scripting. I'll first cover how I implement new scripting, and how that fixes the other two mentioned problems.

Scripting

In my previous post, I suggested that Iron Heart should load all tank stats from a script. Upon planning out how it would work to solve problems like memory leaks and long compiling times, I wanted the loading and all file paths to also be scripted. Because of this, it needs to be as dynamic and modular as possible. How do I go about doing this, first and foremost?

In order to make it as easy as possible for my designers, I have made it so that items, tanks, and levels all follow a similar pattern for loading and setting of any stats. Each section (tank, level, or item) have a hard coded location: a 'locate' folder. A locate script will hold any file paths necessary, including the stat script location, for that particular entity, and a stat script will fill out any necessary stats (health, speed, mass, etc).

When starting a game

Upon starting the game, we create and init a new singleton called Manager_Script. When we init this singleton, it will dynamically search every hard coded location and grab the file name of any .txt files in that location. This is a huge benefit because it is dynamic. Say we are searching in the folder Tanks\Locate folder. If my designer makes a bran new script, they can simply plop it in and the program will automatically grab the name of that file and store it in a vector. So upon grabbing the name of all locate and scripts, we can continue.

When the game starts, we count how many locate scripts we have. Each locate script will represent a unique tank. We can then create x amount of Tank objects based on the number of locate scripts. We can then parse those locate scripts, set the given file paths to any variables, and then load the stat script and fill out all the variables that the tanks will have.



What is great about this is that we are segmenting the loading between different points in the game. At the start, we just do the locate script and stat script loading. We do not load in the actual model or textures. We clocked the loading, setting, and parsing of both a single locate script and its associated stat script at less then a tenth of a second. I personally consider this a success.

The only catch is that the locate text file has a name in it for the entity being loaded. That name must be the exact same name as the model we are loading or else it could cause problems. (For example, in the Tesla Tank script, if the model in Maya is called 'TeslaTank' the text file should have the name listed as 'TeslaTank'

How does this solve any problems

This is a huge benefit because everything is dynamic. This means the designer never has to dive into any code. It reduces compiling times because we don't have to compile thousands of extra lines of codes. Every tank is treated as an abstract object, which sounds like a disadvantage. However, it is a great advantage because we can then use Data Oriented Design methods and send the tanks to update as big chunks of data. The parser is not without problems (my first script parser for a game), but it works well for what it is. We are calling the operator 'new' so much less now. The stats are able to be changed dynamically on the fly. Less .cpp and .h files. In the end, the new scripting system is already a massive improvement to the old way we loaded tanks into Iron Heart.

Recap & Conclusion

  • Program begins, init's Ogre and Havok.
  • Init Manager_Script singleton
  • Search hard coded locations for 'locate' scripts. Store those names into memory.
  • Open those scripts one by one, parse and store variable names.
  • Load the stat functions. It's location is given to us by the locate script. (Faster then searching and storing the name)
  • Load and parse stat script.
  • Load object, set textures.
  • Continue with game.

In the future, we'll be using this system for levels, items, and other parts. This is one step of rewriting the Iron Heart engine into something epic. It has so far been a great learning experience. Another update coming next week! Leave a comment if you have any questions!


No comments:

Post a Comment