|
Item & DAT Files
So, you've read all the newbie stuff and are ready for some real VERGEing, eh? Good place to start. ~_^
This article covers one of the more fundamental concepts behind making an RPG system -- items.
Ookay, first off, I assume you know some basic VC coding, like putting entities on the map and stuff like that. It also assumes that you can read VC already, and know what commands do, or at the least, know how to look them up. ^_^
Okay, if you just try to blitzkrieg into this, you'll most likely end up making a big mess, and if you're lucky, it might work. So, do a little planning out first. How do you want it to work in the end? In this article, I'll assume a conventional "Final Fantasy" style of item system. The party will have a number of "slots", each of which can hold up to 100 of any one kind of item. I chose this method mainly because it's simple, and it works well despite VERGE's lack of structs and multi-dimension arrays. (don't sweat it if that last sentence didn't make sense to you, it doesn't have to)
To that end, we need to set up a couple of databases. First off, since it's the backbone of the whole thing, let's make the item database. This will hold all the information for the different kinds of items. This is not what's in the player's inventory. This is a list of all the items that exist in your game, and what they're called, etc.... To that end, let's #define a few constants first:
// this is the size of our database.
#define MAXITEMS 200
hmm... we don't really need a lot of constants for this one, do we?
By the way, it's considered good practice to define your constants in all caps. It makes it easier to tell what's a constant, and what's a variable, so do it. ^_^
Oh well, let's get on with it. Here's some data:
string i_name[MAXITEMS]; // the item's name
int i_useable[MAXITEMS]; // set to 1 if this item is useable, 0 otherwise
So, every item in our game has a name, and a flag that says whether it's useable or not. You'll probably want to add other aspects to your items, but this is just a tutorial, so we'll keep it simple. ;)
Now, we've got our database all set up, but we need a way to stuff things in it. We could just set each string and integer manually, but that would mean lots and lots of redundant code. We'll get a little fancy and implement a DAT file instead. Here's how we'll make it work:
4
Potion 1
Ether 1
Excalibur 0
Buckler 0
Okay, that number on the first line will be the total number of items in the file, so we know how far to go. After that, we've got the name, then a space, (or many spaces, if you like) then the value that goes into our use flag. Now... how do we load it? Simple. VC has a few handy-dandy built in functions that make this sort of thing much easier. Mostly, we'll be using fgettoken. Here's the code:
string tempstring; // fgettoken doesn't like local strings, so we'll make a global one for it to read to
void LoadItemDAT()
{
int f; // our file handler
int count; // a loop counter
int numitems; // this will store the number of items stored in our item data file
f=fopen("items.dat"); // open our data file
if (f=0) // Did something go wrong?
Exit("Error loading items.dat"); // tell the user what happened
fgettoken(tempstring,f); // read the first line
numitems=val(tempstring); // and store it away
if (numitems>MAXITEMS) // do we have enough room?
numitems=MAXITEMS; // Nope, we'll just read in as many as we can, then.
for (count=0; count<numitems; count++)
{
fgettoken(tempstring,f); // get a string
i_name[count]=tempstring; // and stuff it in the database
fgettoken(tempstring,f); // one more
i_useable[count]=val(tempstring); // and store it away
}
fclose(f); // Close up the file we opened.
}
That's it! It might look a little tricky, but if you have to, take a peek at vergec.txt, and look up the functions it uses, and it should start to make sense.
Now, we've got a database, and a function to load it up for us. Peachy. Next up, we need another database for the party's inventory. This one is pretty simple, just an array. If the first item in items.dat was a "Potion", then p_inventory[0] would hold the number of potions the party has.
int p_inventory[MAXITEMS];
Woo. Tuff stuff. If you wanted to make the items sortable, you could pair it up with another array, which would hold the type of item in a given "slot". We'll just assume that it's always the same as the slot number, for simplicity.
Now, we could just let you run around with that, but let's make things a bit simpler. Time for some helper functions! Here they be.
void GiveItem(int whatkind,int howmany)
// gives <howmany> of item <whatkind> to the party
{
// first, let's make sure the arguments are valid.
if (whatkind<0 || whatkind>MAXITEMS) return;
if (howmany<0) return;
// next, DO IT!
p_inventory[whatkind]+=howmany;
// one last thing
if (p_inventory[whatkind]>100)
p_inventory[whatkind]=100; // We'll only let the party hold up to 100 of any one kind of item.
}
void TakeItem(int whatkind,int howmany)
// takes <howmany> of item <whatkind> from the party
{
// range checking
if (whatkind<0 || whatkind>MAXITEMS) return;
if (howmany<0) return;
if (howmany<p_inventory[whatkind]) // are we taking more than the party has?
howmany=p_inventory[whatkind]; // if so, take them all then away
// TAKE IT!
p_inventory[whatkind]-=howmany;
}
int ItemCount(int whatkind)
// this will return the number of items of type <whatkind> the party has
{
if (whatkind<0 || whatkind>MAXITEMS) return 0; // invalid item reference, just return zero
return p_inventory[whatkind];
}
So, now you can give the party items with GiveItem, take them away with TakeItem, and find out how many a character has with ItemCount. Like so:
event // some guy
{
Text("Gimme that!"); // I'll assume you've already got one of these. ^_~
TakeItem(2,1); // oh no! He stole Excalibur!
if (ItemCount(0)<10) // does the party have at least 10 potions?
Text("Hey! Take some stuff!");
GiveItem(0,10); // give 10 potions (item index #0) to the party
}
Well, that was easy, no? The beauty of this system is that your other VC systems don't have to know how this one works internally, it just has to know that it can find out how many of an item the party has with ItemCount, that GiveItem gives items, and TakeItem takes them. So, you could totally restructure it, and as long as those 3 functions do the same thing, the rest of your VC wouldn't need to be touched! ^_^
Next time, (assuming there is a next time) I'll get to coding an item menu. Until then... uhm... good luck! ^_^
- tSB
|