An Item is born!
Published: 2014-09-22
Author: Killermon
(I probably should do some fancy intro for this, but sod it!)
[embed]https://dl.dropboxusercontent.com/u/11013411/ShareX/2014/06/2014-06-24\_22-30-20.mp4\[/embed\]
The video above shows the very basic process of an item being added to a player backpack (Yes, there’s checkerboard and it looks a bit ass). In this instance, coal, from mining a local rock nearby! Now, on the surface, this process looks simple as it’s just ‘adding an item to a backpack’. To understand the complexities that go into what the video above shows, let’s have a look at the backpack in RP08.
Now, when you look at your backpack, you’ll see that each item is stored by category, this means if you have 10 oranges, and then you find 10 more, you have 20 oranges in your backpack! This makes sense as 10 + 10 is 20 after all.
[caption id="" align=“alignnone” width=“600”] RP08 backpack[/caption]
These items exist in a table that is built against you, the player object. When you leave the server, we just take that table, convert it into a JSON string and whack it into the DB (DISCLAIMER: We do not endorse nor encourage the storing of JSON strings in DB’s as it really defeats the ).
[caption id="" align=“alignnone” width=“775”] RP08 DB[/caption]
So, this storage of data is really simple. You get an item on the server, we just add a number to that item entry in your player table! We just need to dump it to the database when you leave, it’s simple!
Now, this approach has a serious number of issues, primarily its scalability and flexibility; you can only ever really use it to implement an inventory system where the only limiting factor is a ‘weight’ which is really just an arbitrary number.
As you may have worked out from the original video, we have a ‘diablo style’ inventory system, so taking the same approach as RP08 is simply impossible. For those unfamiliar with what a ‘diablo style’inventory is, it’s basically where you have a flat grid, say 10x10, and items can take up multiple grids slots - coal in our video above is a 2x2 item - this picture will show it a bit better, each item takes up a different amount of space.
Whereas RP08 took the approach of only using the DB as a storage mechanism, Life uses the DB as a control mechanism. This means that when you find an ‘item’, it is kept as an item on the server (Following Object Orientated Programming practises) but it is also stored in the DB as an item. To get this done, when you first find an item, the server does it little bit (Finding a slot and somewhere to store the item); this is then sent to the database which adds what it needs to into the item, which is then passed to the server and eventually, to your client!
Excuse the rather naff flowchart, but it should give you a rough idea of what I’m blabbering about! This is only a very high level snapshot of what the server does, so lets go into more details:
- So, you’ve mined a rock, now what? Well, first the server has to find where to put the item, following these two rules:
- Can we add it to an existing stack in any of your bags?
- If not, can we find spaces for it?Depend on how much you find, depends on how we distribute it. For example, coal has a maximum stack of 20, this means if you find 30 of it, we need space for two stacks.
- So, lets say the server finds you can’t add the items to existing stacks, but instead must create a new one. So, now we have to package all the items variables up (Of which, there are quite a few that the player never sees) and sends this to the database.
- The database gets this info, allocates the item a Unique ID, saves it in the Database and relays this Unique ID back to the server.It also has a very verbose audit trail so we can follow every variable about an item, when it changed and, most importantly, who changed it.
- Server gets the Unique ID, creates the items, add it to the servers global items table and then inserts the item into the relevant backpack.
- Now, the server has to synchronise all the clients. This bit is a little more complex, and I’ll cover the ins and outs of the networking side in another blog post - needless to say, the items data eventually gets down to you.
- You then open your backpack screen, and your item is there! Or, if it’s already open, it will pop up in front of you.
All that, required to make a single item in RP - whenever you get a new item in RP, that same process has to be followed each and every time. Not to mention you also have other things to worry about, such as players splitting stacks, merging stacks and moving items across a 2D inventory grid - all of which is synchronised to a MySQL database and then synced across the relevant clients in real time! It was a bastard to code, but at least we’ve made progress.
For those actually curious as to our progress, the backpack (Officially, the ‘Container’ system.) and the item system are complete, with completed database synchronisation, networking and client side code.
At the moment, I’m working on the world object system (Which is any item in the world that you interact with ). All the items in the below screenshot work - the thumpers automatically mine ore ( with or without people on the server), the jalopy has a chest on the back of it with working item / container system, the chests also all works, and the landmines go BOOOOOM!.. when you stand on them anyway. These items may or may not be in the final release before certain people get too excited about landmines, it was a good way to test the passing of entity interactions back up to the world object code.
The ins and outs of the world object system will be covered in a later blog post.