This week, I focused on developing Rawrmy’s inventory system.
The typical inventory system is fairly simple: you have the player who owns the item, the item itself, and (perhaps) the quantity of the item.
One of the nicer features of Django that building the inventory system has introduced me to is reverse relations; they make it very easy for me to add new items to the user’s inventory. I am storing their inventory in my UserProfile model, and using a ManyToManyField for keeping track of the user’s items(with an intermediary ‘through’ model for keeping track of the quantity of the item), and that means that I can add items to the player’s inventory with a single line of code:
With item being the item I want to add, and owner being the profile I am adding the item for.
One question that came up while building my inventory system was “Should items be stackable?” – because it makes sense for things like weapons not to stack, but things like usables to stack. In the end, I decided to add a stackable BooleanField to all of my items, and then just check it in my InventoryItem’s save() method, like so:
def save(self,*args,**kwargs): if self.item.stackable and kwargs.get('force_insert',False): self = InventoryItem.objects.get_or_create(item=self.item,owner=self.owner) self.quantity += 1 kwargs['force_insert'] = False super(InventoryItem, self).save(*args,**kwargs)
Is this the best way to do it? Probably not. But it lets me have items that do stack and items that don’t stack fairly easily, in addition to being able to add items to the player’s inventory without having to write a lot of boilerplate code.
Overall, progress is being made – while I’m not sure I will be able to finish as much as I would have liked to by the time that the contest is over (December 11th), I’m fairly confident that I will have a codebase completed that I am happy to work on far into the future.
Completed this week:
- Basic combat system – needs some tweaking but currently workable
- Inventory management – add items, remove items
Still to do:
- Create more content and quests – while there are plans in place for upwards of 5 areas at the start of the game, there have only been quests for 2 of them designed so far.
- Write better storyline content – right now it just says things like “the storyteller tells you to go over there. So you decide to do that.”
- Lots of polishing and re-working – while most of the internals of the game work and are playable right now, they don’t feel “just right” yet.
Wish there was more?
I'm considering writing an ebook - click here.
Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.