Page 1 of 1

Weapon data in XML Files?

Posted: Mon Jul 19, 2010 4:28 pm
by TheLividePianoist
For a while, I've wanted to create a weapon system using XML files but I'm not sure how to do so. So instead of that, for now, I've decided to just keep doing things the way I have been by hardcoding all the weapon data. What I'd like to know is which way is better to go:

Have a weapon class and have derived classes like (Glock, M16 and etc) from it and create all the data by making instances like

Code: Select all

class Glock : public Weapon 
{
private:
public: 
}

Glock glock; 
Now, if I do this I'm not sure how I'd connect another class like Loadouts with weapons. In my game I have loadouts which are basically like classes in RPGs. Loadouts have their own specialities, weapons and abilities. Each loadout has 3 types of weapons that can be equipped: Primary, Secondary and Projectile.

Or I could create instances of the weapon class like such:

Code: Select all

Weapon M16(32,4,2,9) //In parameters: Magsize, bulletdmg, X, Y) 
Weapon Glock(17,2,8,9)
Instead of doing this, I wanted all the information to already been stored in XML files rather than me having to do this for ever weapon.

What i was thinking is that I can set each loadout with default weapons for each type like:

Code: Select all

Loadout Assault; 
Assault->Primary.SetDefaultPrimary(&M16); //the assault loadout's primary weapon is the M16
Assault->Primary.SetDefaultSecondary(&Glock);
Assault->Primary.SetDefaultProjectile(&RPG);

cout << "Current Weapon:" << Assault->Primary.Name << endl; 
cout << "Bullet Dmg:" << Assault->Primary.BulletDmg << endl; //for test 
RESULT:
Current Weapon: M16
Bullet Damage: 4

So the in the player class, the player doesn't necessarily have the weapon but instead the Loadout class handles that. So whatever loadout the player has, the weapons he'll have is whatever weapons are in that loadout. Problem is that Loadouts are not gonna always have default weapons. In game, the player can change his weapon by picking up weapons or switching them. Loadouts can also be customized to fit the players wants.

Other notes:
The system runs like this. The player chooses their loadout and based on the pick, the player's primary, secondary and projectile weapons are chosen. In the beginning of the game, each loadout already has the defaults set.

Code: Select all

Player -----> Loadout ----> Weapon 
If someone can help me figure out how to work with XML files or provide some links than it would be much appreciated and I'd scrap this way. Otherwise, any other ways of doing this would also be appreciated.

Re: Weapon data in XML Files?

Posted: Mon Jul 19, 2010 5:31 pm
by dejai
Check out the TinyXML library it allows you to retrieve data from XML. We use it in the NTEngine it has a very straight forward API. To do this create a bool LoadWeapon( std::string filePath ); method within your weapon class and call this in your constructor (or you can just do it in your constructor, however a separate method is more flexible) then use TinyXML to retrieve attributes from xml and pass them into private members and then you create an object with member values corresponding to those in xml! I am not going to teach you how to use the library (maybe later) you can look it up it is quite straight forward.

Code: Select all

<weapon type ="gun.png">
  <attack type="range" damage="10"/>
</weapon>

Re: Weapon data in XML Files?

Posted: Mon Jul 19, 2010 6:05 pm
by XianForce
dejai wrote:Check out the TinyXML library it allows you to retrieve data from XML. We use it in the NTEngine it has a very straight forward API. To do this create a bool LoadWeapon( std::string filePath ); method within your weapon class and call this in your constructor (or you can just do it in your constructor, however a separate method is more flexible) then use TinyXML to retrieve attributes from xml and pass them into private members and then you create an object with member values corresponding to those in xml! I am not going to teach you how to use the library (maybe later) you can look it up it is quite straight forward.

Code: Select all

<weapon type ="gun.png">
  <attack type="range" damage="10"/>
</weapon>
Yeah definitely go with putting it in an Init method rather than a constructor. It's bad practice to create constructors that can fail.

Re: Weapon data in XML Files?

Posted: Mon Jul 19, 2010 9:12 pm
by wearymemory
XianForce wrote: Yeah definitely go with putting it in an Init method rather than a constructor. It's bad practice to create constructors that can fail.
I'd say it's bad practice to allow the creation of an object that serves no purpose, i.e. not failing on initialization (where it counts). This may be an instance where verbosity is crucial because the majority of issues you may be faced with this situation are programming errors. These precondition violations usually signify a failure in the application that is most likely unrecoverable. A number of games tend to store stateless information about an item or weapon in some form (usually not XML, but that's for another thread), and at some point in time, this information (or its location/item number in a file) is loaded into memory, and items can be initialized by passing a specific item type into its constructor. That way, if any programming errors occur while reading in item information, then a user-friendly version of that error can be displayed, and the specific details of that error can be written to a debug log. The user might not be happy with being unable to play their new game, but it lessens the likelihood of a subtle error or bug occurring in the middle of an important part of the game.

Technically speaking, you shouldn't always hard code the specific data or file destinations for each item in your game, as this can decrease readability, reliability, and maintainability if the number of items grows too high. It might be more efficient if you were to keep it all in one, easily maintainable file. You might be able to increase productivity at this stage if you stick with XML and separate files for now, but it's something to think about in the future.

Re: Weapon data in XML Files?

Posted: Tue Jul 20, 2010 2:27 am
by dejai
The obvious answer is to have an xml file that is used to store weapon types so the entire process is based on data outside of the engine. (I won't go into "the design" for weapons but you get the idea). Also you may require it in the constructor but also like to be change an existing object (this again is a design issue) and so a public LoadWeapon(); method may be appropriate. Again all design issues I was focusing on actually answering his question.