# C# Potions usage and how to implement it

## Recommended Posts

Alright, I'm not even sure if I write my first post in the right sub category.

Basically I'm creating some browser game based on .net and you got there an inventory with a bunch of potions. I'm just not really sure how to handle different potions and its usage on the backend to not make it into a spagetti. So, whenever user clicks on the potion, it sends an id of the potion to the api endpoint, I'm checking there the type and just getting the info about potion. I got an enum which describes the type, like mana, hp, energy, exp or you name it.

So what now. Do I make like a huge switch statment and just pass it to a different methods or something and do there what has to be done with the potion? Sorry for not showcasing any code, but I do not have any for this, just trying to brainstorm how to implement some potion usage mechanics. Am I going the right direction or I just missed the path and heading to the dead end?

Edited by harugawa

##### Share on other sites

It depends on the design of your game.

First of all, you say a switch statement would be "huge": does it mean that there are going to be many potion "SKUs", many essentially different potion effects requiring separate specific code, or both? In general, switch statements are appropriate for a small and stable set of alternatives: probably not potion types.

If potion effects are shared between many potions (e.g. one potion gives a lot of mana and heals a few hit points, one gives a little mana and heals many hit points, one heals hit points only) or with other sources (e.g. a bath in an enchanted fountain restores mana or heals hit points like drinking a potion) you can reduce duplication by focusing on the effects (e.g. a routines to heal N hit points , add N mana, etc.) and describing potions with a data structure listing their effects and other properties (e.g. "this potion is called potion of invigorating healing, it is pale blue, it heals 45 hit points, it gives 3 mana, 3 energy, 0 experience"), even without an ID. Then when a potion is consumed you would go through its description, processing general drinking rules (e.g. choking because of bad taste) and effects one by one in a standard order, without low value potion-specific code,

This data-driven approach would facilitate potion variations by allowing them to be anonymous, whereas potions with the same ID would have to be identical. Potions could be generated randomly (e.g. average strength of healing potions increases proportionally to character hit points, and they are all a "potion of healing"), altered (e.g. by mixing them), deduced from other data (e.g. distilling a corpse produces a potion with effects that depend on the creature).

##### Share on other sites

What I would do is create some form of consumable function, create a switch statement around the adjustable stat

ConsumePotion(StatType.Health, 25); = SmallHealthPotion

ConsumePotion(StatType.Armor, 200); = Greater Armor Potion

If you do your switch based on a specific stat, you have an unlimited potion arsenal, with minor changes instead of tons of changes.

##### Share on other sites

Decouple the data from the implementation details so that you don't need a massive switch statement. Imagine you had 500 different types of potions. Do you really want to make a switch statement with 500 case-statements? I certainly wouldn't.

Here is an example:

class Potion
{
public int mana;
public int health;
public int energy;
public int experience;
}

Creating a potion is simple:

Potion superAwesomePotion = new Potion();
superAwesomePotion.mana			= 10;		// Indicates that the potion should add 10 mana
superAwesomePotion.health		= 6;		// Indicates that the potion should add 6 health
superAwesomePotion.energy		= 2;		// Indicates that the potion should add 2 energy
superAwesomePotion.experience	= 25;		// Indicates that the potion should add 25 XP

You can store this data in XML files, JSON, or binary data that can be loaded at runtime or stored in the game in some way. You could create an array of potions that could be indexed or named (dictionary).

To apply the potion to a character (or anything really), you could have a UsePotion method:

private void UsePotion(Potion potion)
{
this.mana		+= potion.mana;
this.health		+= potion.health;
this.energy		+= potion.energy;
this.experience	+= potion.experience;
}

 Fixed potion creation sample as mana was duplicated

Edited by Jamison

## Create an account

Register a new account

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011780
• ### Who's Online (See full list)

There are no registered users currently online

×