Is there anyway to have a huge list of stuff that program will auto choose from without using huge nesting. I'm trying to make a Monster Manager for a combat routine.
something like
int MonsterSelect = 0;
then on the area that triggers the combat have
int MonsterSelect = # of monster stored in a seperate function
so
Game() > MonsterManager() > Combat()
id rather have seperate small functions for each monster type or even a single function if possible that stores all monster information. I don't want tons of if else what about switchs is there a way to do switchs without user input?
How aboutcout << "Monster encounter " << MonsterSelect <<'\n';?
If your monsters only differ in some properties, like power and health you could do
1 2 3 4
struct monster{
int pow, hp;
};
monster my_monsters[50] = {/*you can define your monsters quickly here*/};
then in combat function, do sth like my_monsters[MonsterSelect].hp-=50;.
If your monsters differ drastically and really need different function each, you should go for polymorphism, though I have a feeling this is not the case.
// Monster #1
string MonsterName1 = "Fiddler_Crab";
int MonsterLevel1 = 1;
int MonsterHitPoints1 = 10;
int MonsterAttack1 = 7;
int MonsterDefense1 = 4;
int MonsterHitRate1 = 50;
int MonsterExperience1 = 20;
// Global Monster Variables - Combat System
string MonsterName = "Monster";
int MonsterLevel = 0;
int MonsterHitPoints = 0;
int MonsterAttack = 0;
int MonsterDefense = 0;
int MonsterHitRate = 0;
int MonsterExperience = 0;
int MonsterSelect = 0;
struct Monster
{
string name;
int hp;
//... etc
};
Monster MonsterList[] = {
{"Fiddler Crab",15, /*...*/},
{"Great Moose",96, /*...*/}
};
void somefunction()
{
// select a monster
int monsterID = rand() % number_of_monsters;
Monster mymonster = MonsterList[monsterID]; // no need for any ifs, just use the ID to index
}
Another (arguably better) way to go would be to have the monster details stored in an external file that you load at runtime. But the general idea is the same -- have a big array (or some other container, like a vector/list/deque), and use that as like master list of all the enemies, and just pull from it what you need.
and guess what my way worked 100% perfect and flawless!!! Just i had a homer simpson moment doh!!! I put MonsterCombat() and triggered the combat function with 0 initialized variables instead of MonsterManager() > MonsterCombat()