Generating Code with Rails
In my day job I get a lot of experience with different technologies. Mostly related to mobile or website, but there is a lot of bleed over in the game industry. I've done a few Ruby on Rails applications, it's not my primary toolbox, but it has some benefits. Mostly the ease and speed of development.
I switched to using Ruby on Rails for the data management layer of my game. At the moment there isn't much data, only about 300 rows in a database for everything in the game. This is expected to more than triple within the next few months as I add achievements, more items, 7 new quests, and shops!
Eventually this will go extremely large and hard coding all that data will become very error prone and next to impossible to manage.
Ruby on Rails has the ability to have command line tasks which can be used to generate data.
Sample task:
namespace :codegen do task :achievement_types => :environment do erb = ERB.new(File.read('lib/tasks/codegen/achievement_types.erb')) params = binding params.local_variable_set(:achievementTypes, AchievementType.all) data = erb.result(params) f = File.new('codegen/AchievementType.h', 'w') f.write(data) f.close() end end
What this code does is it loads an ERB file which is a templating file for Ruby.
Queries the database for all AchievementType objects,
Then creates a local binding scope for the template,
Renders the template to a string
Presto, Generated C++.
Erb file in question:
#ifndef ACHIEVEMENT_TYPE_H #define ACHIEVEMENT_TYPE_H <% achievementTypes.each do |at| %> #define <%= at.macroname %> <%= at.id %> <% end %> #endif
Code generated:
#ifndef ACHIEVEMENT_TYPE_H #define ACHIEVEMENT_TYPE_H #define ACHIEVEMENT_TYPE_WOODCUTTING 1 #define ACHIEVEMENT_TYPE_FISHING 2 #define ACHIEVEMENT_TYPE_MINING 3 #define ACHIEVEMENT_TYPE_FIREMAKING 4 #endif
The use case above is fairly simple, but more complex situations can occur, such as Npc Conversations. Currently my NpcConversation_Gen.h file is 500 lines long. with lists of data similar to this:
new NpcConversation(34, 0, "69488b5b-cfd1-4255-bd74-a6b7eeb0e939", { new NpcConversationAction(27, "AddInventoryItem", 39, 1), new NpcConversationAction(28, "CompleteMilestone", 3, 15), }, { new NpcConversationConditional(12, "StartedQuest", 3, 0), new NpcConversationConditional(13, "HasItem", 57, 10), } ),
Maintaining that code by hand would triple the amount of time to create quests.
So if your game uses a large amount of data, I really recommend using a web framework (Ruby on Rails, Codeigniter, Cakephp, Revel, Django, Spring, ect) to manage all your game data!
7 Comments
Recommended Comments