Global Variables

From IrrWizard

Jump to: navigation, search

Global Variables

The GameManager can be used to declare or hold Global variables used for passing values accross states. For example the amount of enemies killed during a level might be required on a summary or level complete screen, (seperate states).

pManager->setGlobalString("nameString" , "text here");
pManager->setGlobalInt("nameInt", 100);


Local Variables

Variables can be defined and used at a Game Obect level, these are local variables. Everything that is subclasses from IGameEntity, (which should be prety much every game object), will have access to these user deined variables:

pItem->setGlobalString("nameString" , "text here");
pItem->setGlobalInt("nameInt", 100);

Example code

//! Set global variables to be used in Score State
void CGameStateLevel01::setGlobalVariables(CGameManager* pManager)
{
     // Get a list of enemies from the EnemyManager
     irr::core::array<CGameEnemy*> enemies = pManager->getEnemyManager()->GetEntityList();
     for (u32 i=0; i < enemies.size(); i++) 
     {
       // convert int to std::string  
       ostringstream myStream; 
       myStream << i << flush;
       std::string num = myStream.str(); 
       pManager->setGlobalString("enemy_name" + num, enemies[i]->getName());
       pManager->setGlobalInt("enemy_score" + num, enemies[i]->getKills());
     }
}
Personal tools