switch (uniqueID)
{
case 1:
DrinkLifePotion();
break;
case2:
EquipWeapon();
break;
//etc
}
public void DrinkLifePotion()
{
//parse lifeRestored from XML file
lifeHealed; //would equal the value from the XML file
player.HP += lifeHealed;
}
switch (uniqueID)
{
case 1:
DrinkLifePotion();
break;
case2:
EquipWeapon();
break;
//etc
}
public void DrinkLifePotion()
{
//parse lifeRestored from XML file
lifeHealed; //would equal the value from the XML file
player.HP += lifeHealed;
}
class Item{ virtual void apply( Object target );}class HealingItem : public Item{ int healingStrength; virtual void apply( Object target ) { if ( target is CreatureObject ) { // cannot use on monsters } if ( target is PlayerObject ) { if (target.isFriendly() ) { // heal target } } }}
class Command{ virtual void execute();};class UseItemCommand : public Command{ UseItemCommand( Object item, Object target ); virtual void execute() { item->apply(target); }};....// on double click on item{ CommandQueue.push( new UseItemCommand( Inventory.getSelectedItem(), UI.getCurrentTarget() ) );}
switch (uniqueID)
{
case 1:
DrinkLifePotion();
break;
case2:
EquipWeapon();
break;
//etc
}
[\code]
What if you need to add a new item to the inventory? You will have to hard code a new case everytime you create a new item, also you will have to update the xml items list and load the file?
I think a better aproach would be read the xml file and store you items "properties" to class or struct. Then you would have some kind of collection for storing these objects "items".
Hope this helps
Quote:Original post by Antheus
You use XML to persist your in-game objects. It's not intended for life transformation (although it can be done, EE-grade software does it all the time, X-Path and similar).
Unfortunately, you then immediately run into a whole lot of issues regarding identities, dependencies and other deal-breaking things.
There's an article in Game Programming Gems 6 on Component oriented architecture, which natively supports run-time configuration and XML persistence.
You really don't want to deal with IDs as such in your code. Too prone to abuse or mistake. Your inventory contains objects. One useful approach here is inversion of control, the other is command pattern.
The idea is something like this:
*** Source Snippet Removed ***
Quote:Original post by violentcrayon
Thanks so much! That clears things up quite a bit. So, using your code, then if the user says he wants to use the healing potion, I would use
Item.Apply( HealingItem )
right?
class Program { public static int HP; static void Main(string[] args) { Program program = new Program(); program.InvetoryTest(); } public void InvetoryTest() { HP = 5; HealingItem heal = new HealingItem(); HarmfulItem harm = new HarmfulItem(); Item[] inventory; inventory = new Item[] { heal, harm }; Console.WriteLine(HP); inventory[0].ApplyItem(); Console.WriteLine(HP); inventory[1].ApplyItem(); Console.WriteLine(HP); Console.ReadLine(); } } public class Item { public virtual void ApplyItem() { //nothing happens here...it's always overridden } } public class HealingItem : Item { public override void ApplyItem() { Program.HP += 5; } } public class HarmfulItem : Item { public override void ApplyItem() { Program.HP -= 3; } }}