Hi! Here is the situation:
The player has two spell slots.
In each slot, the player can have one element activated (such as Fire, Water, Rock etc.).
The problem that I’m trying to solve, is that I want to compare the elements of both slots, and compare them in a way that disregard their order (i.e. whether if they are in slot_1 or slot_2. This way, Fire-Rock combination should produce the same effect as Rock-Fire combination, allowing me to kill two birds with one block of code.
Right now, I’ve stored the two active spells in two slots:
string Spell1 = "Fire";
string Spell2 = "Rock";
I’m thinking I could solve this by simply concatenating the two strings, and just go:
if (“FireRock” || “RockFire”) {
//do the Fireball attack
}
But is there any other ways of solving this??
Much appreciated!
Although boolean Logic here does work for a few spells, one thing to note if you have lots of different spells you will soon run out of bits to play with. eg. If your game ends up having 100’s of spells this will no longer work.
Another option here is then to use HashSets.
//configure some recipes..
HashSet<string> fireAndWind = new HashSet<string>();
fireAndWind.Add("Fire");
fireAndWind.Add("Wind");
HashSet<string> fire = new HashSet<string>();
fire.Add("Fire");
HashSet<string> water = new HashSet<string>();
water.Add("Water");
//put what we have in inventory
HashSet<string> inhand = new HashSet<string>();
inhand.Add("Wind");
inhand.Add("Fire");
//some tests..
Debug.Log ( inhand.Overlaps(fireAndWind) ); //true
Debug.Log ( inhand.Overlaps(fire) ); //true
Debug.Log ( inhand.Overlaps(water) ); //false
I solved this question in Convert int to bit (but not as string)? - Unity Answers. This may be closed/marked as duplicate.
Like @Landern said, bit masks and bit wise operations are a good way to solve combination problems like this.
You can assign the individual spells int
identifiers.
// if you use ints as bitmasks, this is a good way to assign them imo
int Fire = 1 << 0; // means: take 1 (0001) and move the bits left by 0 => 0001 == 1
int Water = 1 << 1; // means: take 1 (0001) and move the bits left by 1 => 0010 == 2
int Earth = 1 << 2; // means: take 1 (0001) and move the bits left by 2 => 0100 == 4
int Wind = 1 << 3; //...8
Then using bit wise operations, you can combine the identifiers to make spell-combos and then make all kinds of checks on them (that are performance wise one of the fastest things you can do in any program
)
int allCombo = Fire | Water | Earth | Wind
// ORing makes an int that has all the bits turned on:
// 0001
// 0010
// 0100
// 1000
// ---- result:
// 1111
// does the combo have Wind and Fire ?
int mask = Wind | Fire; // 1001
// AND (&) of 2 ints leaves up only the bits that occur in both source ints
// 1111
// &
// 1001
// =
// 1001
bool hasWindFire = (allCombo & mask) == mask; // true if has both
bool hasAtLeastWindOrFire = (allCombo & mask) != 0; // true if has both or either
The possibilities are endless when you learn to play around with these operations