Changeset 10624 for code/trunk/src/orxonox/Level.cc
- Timestamp:
- Oct 4, 2015, 9:12:21 PM (9 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
code/trunk/src/orxonox/Level.cc
r9667 r10624 35 35 #include "core/XMLFile.h" 36 36 #include "core/XMLPort.h" 37 #include "core/module/PluginReference.h" 37 38 38 39 #include "infos/PlayerInfo.h" … … 49 50 RegisterObject(Level); 50 51 52 this->setLevel(WeakPtr<Level>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency) 51 53 52 54 this->registerVariables(); … … 63 65 64 66 if (this->xmlfile_) 65 Loader::unload(this->xmlfile_); 67 Loader::getInstance().unload(this->xmlfile_); 68 69 this->unloadPlugins(); 66 70 } 67 71 } … … 71 75 SUPER(Level, XMLPort, xmlelement, mode); 72 76 77 XMLPortParam(Level, "plugins", setPluginsString, getPluginsString, xmlelement, mode); 73 78 XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); 74 79 … … 95 100 this->xmlfile_ = new XMLFile(mask, this->xmlfilename_); 96 101 97 Loader:: open(this->xmlfile_);102 Loader::getInstance().load(this->xmlfile_); 98 103 } 99 104 … … 105 110 Template::getTemplate(*it)->applyOn(this); 106 111 } 112 } 113 114 void Level::setPluginsString(const std::string& pluginsString) 115 { 116 // unload old plugins 117 this->unloadPlugins(); 118 119 // load new plugins 120 this->pluginsString_ = pluginsString; 121 SubString tokens(pluginsString, ","); 122 for (size_t i = 0; i < tokens.size(); ++i) 123 this->plugins_.push_back(new PluginReference(tokens[i])); 124 } 125 126 void Level::unloadPlugins() 127 { 128 // use destroyLater() - this ensures that plugins are not unloaded too early. 129 // Note: When a level gets unloaded, the Level object is usually the last object that gets destroyed. This is because all other 130 // objects inside a level have a StrongPtr (in BaseObject) that references the Level object. This means that the Level 131 // object is only destroyed, when all StrongPtrs that pointed to it were destroyed. But at the time when the last StrongPtr 132 // is destroyed, the other object is not yet fully destroyed because the StrongPtr is destroyed in ~BaseObject (and this 133 // means that e.g. ~Identifiable was not yet called for this object). This means that technically there are still other 134 // objects alive when ~Level is called. This is the reason why we cannot directly destroy() the Plugins - instead we need 135 // to call destroyLater() to ensure that no instances from this plugin exist anymore. 136 for (std::list<PluginReference*>::iterator it = this->plugins_.begin(); it != this->plugins_.end(); ++it) 137 (*it)->destroyLater(); 138 this->plugins_.clear(); 107 139 } 108 140 … … 121 153 122 154 Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this)); 123 this->setGametype(rootgametype); 124 125 for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) 126 (*it)->setGametype(rootgametype); 155 156 // store a weak-pointer to the gametype to avoid a circular dependency between this level and the gametype (which has a strong-reference on this level) 157 this->setGametype(WeakPtr<Gametype>(rootgametype)); 158 159 rootgametype->init(); // call init() AFTER the gametype was set 127 160 128 161 if (LevelManager::exists()) … … 134 167 { 135 168 this->objects_.push_back(object); 136 object->setGametype(this->getGametype());137 object->setLevel(this);138 169 } 139 170 … … 170 201 { 171 202 orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; 172 player->s etGametype(this->getGametype());203 player->switchGametype(this->getGametype()); 173 204 } 174 205 … … 176 207 { 177 208 orxout(internal_info) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; 178 player->s etGametype(0);209 player->switchGametype(0); 179 210 } 180 211 }
Note: See TracChangeset
for help on using the changeset viewer.