Index: code/branches/presentation/src/core/BaseObject.cc
===================================================================
--- code/branches/presentation/src/core/BaseObject.cc	(revision 2459)
+++ code/branches/presentation/src/core/BaseObject.cc	(revision 2485)
@@ -36,4 +36,5 @@
 #include "CoreIncludes.h"
 #include "EventIncludes.h"
+#include "Functor.h"
 #include "XMLPort.h"
 #include "XMLFile.h"
@@ -60,4 +61,7 @@
         this->oldGametype_ = 0;
 
+        this->functorSetMainState_ = 0;
+        this->functorGetMainState_ = 0;
+
         this->setCreator(creator);
         if (this->creator_)
@@ -82,9 +86,17 @@
     BaseObject::~BaseObject()
     {
-        for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
-            (*it)->unregisterEventListener(this);
-
-        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
-            it->first->removeEvent(this);
+        if (this->isInitialized())
+        {
+            for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
+                (*it)->unregisterEventListener(this);
+
+            for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
+                it->first->removeEvent(this);
+
+            if (this->functorSetMainState_)
+                delete this->functorSetMainState_;
+            if (this->functorGetMainState_)
+                delete this->functorGetMainState_;
+        }
     }
 
@@ -100,4 +112,5 @@
         XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
         XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
+        XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode);
 
         XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
@@ -109,5 +122,5 @@
             std::list<std::string> eventnames;
 
-            if (mode == XMLPort::LoadObject)
+            if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
             {
                 for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
@@ -279,3 +292,46 @@
         SetEvent(BaseObject, "visibility", setVisible, event);
     }
+
+    void BaseObject::setMainStateName(const std::string& name)
+    {
+        if (this->mainStateName_ != name)
+        {
+            this->mainStateName_ = name;
+            if (this->functorSetMainState_)
+                delete this->functorSetMainState_;
+            if (this->functorGetMainState_)
+                delete this->functorGetMainState_;
+            this->changedMainState();
+            if (!this->functorSetMainState_)
+                COUT(2) << "Warning: \"" << name << "\" is not a valid MainState." << std::endl;
+        }
+    }
+
+    void BaseObject::setMainState(bool state)
+    {
+        if (this->functorSetMainState_)
+            (*this->functorSetMainState_)(state);
+        else
+            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
+    }
+
+    bool BaseObject::getMainState() const
+    {
+        if (this->functorGetMainState_)
+        {
+            (*this->functorGetMainState_)();
+            return this->functorGetMainState_->getReturnvalue();
+        }
+        else
+        {
+            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
+            return false;
+        }
+    }
+
+    void BaseObject::changedMainState()
+    {
+        SetMainState(BaseObject, "activity",   setActive,  isActive);
+        SetMainState(BaseObject, "visibility", setVisible, isVisible);
+    }
 }
Index: code/branches/presentation/src/core/BaseObject.h
===================================================================
--- code/branches/presentation/src/core/BaseObject.h	(revision 2459)
+++ code/branches/presentation/src/core/BaseObject.h	(revision 2485)
@@ -36,4 +36,11 @@
 #ifndef _BaseObject_H__
 #define _BaseObject_H__
+
+#define SetMainState(classname, statename, setfunction, getfunction) \
+    if (this->getMainStateName() == statename) \
+    { \
+        this->functorSetMainState_ = createFunctor(&classname::setfunction)->setObject(this); \
+        this->functorGetMainState_ = createFunctor(&classname::getfunction)->setObject(this); \
+    }
 
 #include <map>
@@ -100,4 +107,11 @@
             virtual void changedVisibility() {}
 
+            void setMainState(bool state);
+            bool getMainState() const;
+
+            void setMainStateName(const std::string& name);
+            inline const std::string& getMainStateName() const { return this->mainStateName_; }
+            virtual void changedMainState();
+
             /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
             inline void setFile(const XMLFile* file) { this->file_ = file; }
@@ -121,8 +135,16 @@
             inline Scene* getScene() const { return this->scene_; }
 
-            inline void setGametype(Gametype* gametype) { this->oldGametype_ = this->gametype_; this->gametype_ = gametype; this->changedGametype(); }
+            inline void setGametype(Gametype* gametype)
+            {
+                if (gametype != this->gametype_)
+                {
+                    this->oldGametype_ = this->gametype_;
+                    this->gametype_ = gametype;
+                    this->changedGametype();
+                }
+            }
             inline Gametype* getGametype() const { return this->gametype_; }
             inline Gametype* getOldGametype() const { return this->oldGametype_; }
-            virtual inline void changedGametype() {}
+            virtual void changedGametype() {}
 
             void fireEvent();
@@ -153,6 +175,9 @@
             std::string name_;                          //!< The name of the object
             std::string oldName_;                       //!< The old name of the object
-            mbool bActive_;                             //!< True = the object is active
-            mbool bVisible_;                            //!< True = the object is visible
+            mbool       bActive_;                       //!< True = the object is active
+            mbool       bVisible_;                      //!< True = the object is visible
+            std::string mainStateName_;
+            Functor*    functorSetMainState_;
+            Functor*    functorGetMainState_;
 
         private:
@@ -160,14 +185,14 @@
             Template* getTemplate(unsigned int index) const;
 
-            bool                  bInitialized_;         //!< True if the object was initialized (passed the object registration)
-            const XMLFile*        file_;                 //!< The XMLFile that loaded this object
-            std::string           loaderIndentation_;    //!< Indentation of the debug output in the Loader
-            Namespace*            namespace_;
-            BaseObject*           creator_;
-            Scene*                scene_;
-            Gametype*             gametype_;
-            Gametype*             oldGametype_;
-            std::set<Template*>   templates_;
-            std::map<BaseObject*, std::string> eventListeners_;
+            bool                   bInitialized_;         //!< True if the object was initialized (passed the object registration)
+            const XMLFile*         file_;                 //!< The XMLFile that loaded this object
+            std::string            loaderIndentation_;    //!< Indentation of the debug output in the Loader
+            Namespace*             namespace_;
+            BaseObject*            creator_;
+            Scene*                 scene_;
+            Gametype*              gametype_;
+            Gametype*              oldGametype_;
+            std::set<Template*>    templates_;
+            std::map<BaseObject*,  std::string> eventListeners_;
             std::list<BaseObject*> events_;
             std::map<std::string, EventContainer*> eventContainers_;
@@ -178,4 +203,7 @@
     SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
     SUPER_FUNCTION(4, BaseObject, processEvent, false);
+    SUPER_FUNCTION(6, BaseObject, changedMainState, false);
+    SUPER_FUNCTION(9, BaseObject, changedName, false);
+    SUPER_FUNCTION(10, BaseObject, changedGametype, false);
 }
 
Index: code/branches/presentation/src/core/CommandExecutor.cc
===================================================================
--- code/branches/presentation/src/core/CommandExecutor.cc	(revision 2459)
+++ code/branches/presentation/src/core/CommandExecutor.cc	(revision 2485)
@@ -53,5 +53,5 @@
     }
 
-    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command)
+    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit)
     {
         std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName());
@@ -61,4 +61,9 @@
         }
 
+        // Make sure we can also delete the external ConsoleCommands that don't belong to an Identifier
+        if (command && bDeleteAtExit)
+        {
+            CommandExecutor::getInstance().consoleCommandExternals_.insert(command);
+        }
 
         CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;
@@ -647,3 +652,10 @@
         }
     }
+
+    void CommandExecutor::destroyExternalCommands()
+    {
+        for (std::set<ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandExternals_.begin();
+            it != CommandExecutor::getInstance().consoleCommandExternals_.end(); ++it)
+            delete *it;
+    }
 }
Index: code/branches/presentation/src/core/CommandExecutor.h
===================================================================
--- code/branches/presentation/src/core/CommandExecutor.h	(revision 2459)
+++ code/branches/presentation/src/core/CommandExecutor.h	(revision 2485)
@@ -51,5 +51,5 @@
             static const CommandEvaluation& getLastEvaluation();
 
-            static ConsoleCommand& addConsoleCommandShortcut(ConsoleCommand* command);
+            static ConsoleCommand& addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit = false);
             static ConsoleCommand* getConsoleCommandShortcut(const std::string& name);
             static ConsoleCommand* getLowercaseConsoleCommandShortcut(const std::string& name);
@@ -68,4 +68,6 @@
             /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */
             static inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
+
+            static void destroyExternalCommands();
 
         private:
@@ -101,4 +103,5 @@
             std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_;
             std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_LC_;
+            std::set<ConsoleCommand*>              consoleCommandExternals_;
     }; // tolua_export
 } // tolua_export
Index: code/branches/presentation/src/core/CommandLine.cc
===================================================================
--- code/branches/presentation/src/core/CommandLine.cc	(revision 2459)
+++ code/branches/presentation/src/core/CommandLine.cc	(revision 2485)
@@ -83,9 +83,5 @@
     CommandLine::~CommandLine()
     {
-        for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
-            it != cmdLineArgs_.end(); ++it)
-        {
-            delete it->second;
-        }
+        CommandLine::destroyAllArguments();
     }
 
@@ -98,4 +94,17 @@
         static CommandLine instance;
         return instance;
+    }
+
+    /**
+    @brief
+        Destroys all command line arguments. This should be called at the end
+        of main. Do not use before that.
+    */
+    void CommandLine::destroyAllArguments()
+    {
+        for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin();
+            it != _getInstance().cmdLineArgs_.end(); ++it)
+            delete it->second;
+        _getInstance().cmdLineArgs_.clear();
     }
 
Index: code/branches/presentation/src/core/CommandLine.h
===================================================================
--- code/branches/presentation/src/core/CommandLine.h	(revision 2459)
+++ code/branches/presentation/src/core/CommandLine.h	(revision 2485)
@@ -155,4 +155,5 @@
         }
 
+        static void destroyAllArguments();
 
     private:
@@ -179,5 +180,5 @@
         //! Holds all pointers to the arguments and serves as a search map by name.
         std::map<std::string, CommandLineArgument*> cmdLineArgs_;
-        //! Search map by chortcut for the arguments.
+        //! Search map by shortcut for the arguments.
         std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
     };
Index: code/branches/presentation/src/core/ConsoleCommand.h
===================================================================
--- code/branches/presentation/src/core/ConsoleCommand.h	(revision 2459)
+++ code/branches/presentation/src/core/ConsoleCommand.h	(revision 2485)
@@ -64,5 +64,5 @@
 
 #define SetConsoleCommandShortcutGeneric(fakevariable, command) \
-    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command)
+    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true)
 
 
Index: code/branches/presentation/src/core/Core.cc
===================================================================
--- code/branches/presentation/src/core/Core.cc	(revision 2459)
+++ code/branches/presentation/src/core/Core.cc	(revision 2485)
@@ -46,4 +46,6 @@
     bool Core::bIsMaster_s      = false;
 
+    Core* Core::singletonRef_s = 0;
+
     /**
         @brief Constructor: Registers the object and sets the config-values.
@@ -53,6 +55,10 @@
     {
         RegisterRootObject(Core);
+
+        assert(Core::singletonRef_s == 0);
+        Core::singletonRef_s = this;
+        this->bInitializeRandomNumberGenerator_ = false;
+
         this->setConfigValues();
-        isCreatingCoreSettings() = false;
     }
 
@@ -62,35 +68,6 @@
     Core::~Core()
     {
-        isCreatingCoreSettings() = true;
-    }
-
-    /**
-        @brief Returns true if the Core instance is not yet ready and the static functions have to return a default value.
-    */
-    bool& Core::isCreatingCoreSettings()
-    {
-        static bool bCreatingCoreSettings = true;
-        return bCreatingCoreSettings;
-    }
-
-    /**
-        @brief Returns a unique instance of Core.
-        @return The instance
-    */
-    Core& Core::getInstance()
-    {
-        // If bCreatingSoftDebugLevelObject is true, we're just about to create an instance of the DebugLevel class
-        //if (Core::isCreatingCoreSettings())
-        //{
-        //    isCreatingCoreSettings() = false;
-        //    //instance.setConfigValues();
-        //}
-
-        static bool firstTime = true;
-        if (firstTime)
-            isCreatingCoreSettings() = true;
-
-        static Core instance;
-        return instance;
+        assert(Core::singletonRef_s);
+        Core::singletonRef_s = 0;
     }
 
@@ -104,4 +81,5 @@
         SetConfigValue(softDebugLevelShell_, 1).description("The maximal level of debug output shown in the ingame shell").callback(this, &Core::debugLevelChanged);
         SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text").callback(this, &Core::languageChanged);
+        SetConfigValue(bInitializeRandomNumberGenerator_, true).description("If true, all random actions are different each time you start the game").callback(this, &Core::initializeRandomNumberGenerator);
     }
 
@@ -140,23 +118,18 @@
     int Core::getSoftDebugLevel(OutputHandler::OutputDevice device)
     {
-        if (!Core::isCreatingCoreSettings())
+        switch (device)
         {
-            switch (device)
-            {
-            case OutputHandler::LD_All:
-                return Core::getInstance().softDebugLevel_;
-            case OutputHandler::LD_Console:
-                return Core::getInstance().softDebugLevelConsole_;
-            case OutputHandler::LD_Logfile:
-                return Core::getInstance().softDebugLevelLogfile_;
-            case OutputHandler::LD_Shell:
-                return Core::getInstance().softDebugLevelShell_;
-            default:
-                assert(0);
-            }
+        case OutputHandler::LD_All:
+            return Core::getInstance().softDebugLevel_;
+        case OutputHandler::LD_Console:
+            return Core::getInstance().softDebugLevelConsole_;
+        case OutputHandler::LD_Logfile:
+            return Core::getInstance().softDebugLevelLogfile_;
+        case OutputHandler::LD_Shell:
+            return Core::getInstance().softDebugLevelShell_;
+        default:
+            assert(0);
+            return 2;
         }
-
-        // Return a constant value while we're creating the object
-        return 2;
     }
 
@@ -168,17 +141,14 @@
      void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
      {
-        if (!Core::isCreatingCoreSettings())
-        {
-            if (device == OutputHandler::LD_All)
-                Core::getInstance().softDebugLevel_ = level;
-            else if (device == OutputHandler::LD_Console)
-                Core::getInstance().softDebugLevelConsole_ = level;
-            else if (device == OutputHandler::LD_Logfile)
-                Core::getInstance().softDebugLevelLogfile_ = level;
-            else if (device == OutputHandler::LD_Shell)
-                Core::getInstance().softDebugLevelShell_ = level;
+        if (device == OutputHandler::LD_All)
+            Core::getInstance().softDebugLevel_ = level;
+        else if (device == OutputHandler::LD_Console)
+            Core::getInstance().softDebugLevelConsole_ = level;
+        else if (device == OutputHandler::LD_Logfile)
+            Core::getInstance().softDebugLevelLogfile_ = level;
+        else if (device == OutputHandler::LD_Shell)
+            Core::getInstance().softDebugLevelShell_ = level;
 
-            OutputHandler::setSoftDebugLevel(device, level);
-        }
+        OutputHandler::setSoftDebugLevel(device, level);
      }
 
@@ -188,8 +158,5 @@
     const std::string& Core::getLanguage()
     {
-        if (!Core::isCreatingCoreSettings())
-            return Core::getInstance().language_;
-
-        return Language::getLanguage().defaultLanguage_;
+        return Core::getInstance().language_;
     }
 
@@ -209,3 +176,14 @@
         ResetConfigValue(language_);
     }
+
+    void Core::initializeRandomNumberGenerator()
+    {
+        static bool bInitialized = false;
+        if (!bInitialized && this->bInitializeRandomNumberGenerator_)
+        {
+            srand(time(0));
+            rand();
+            bInitialized = true;
+        }
+    }
 }
Index: code/branches/presentation/src/core/Core.h
===================================================================
--- code/branches/presentation/src/core/Core.h	(revision 2459)
+++ code/branches/presentation/src/core/Core.h	(revision 2485)
@@ -40,4 +40,5 @@
 #include "CorePrereqs.h"
 
+#include <cassert>
 #include "OrxonoxClass.h"
 #include "util/OutputHandler.h"
@@ -49,14 +50,16 @@
     {
         public:
-            static Core& getInstance();
-            static bool& isCreatingCoreSettings();
+            Core();
+            ~Core();
             void setConfigValues();
             void debugLevelChanged();
             void languageChanged();
 
-            static int getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
-            static void setSoftDebugLevel(OutputHandler::OutputDevice device, int level);
+            static Core& getInstance() { assert(Core::singletonRef_s); return *Core::singletonRef_s; }
+
+            static int   getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
+            static void  setSoftDebugLevel(OutputHandler::OutputDevice device, int level);
             static const std::string& getLanguage();
-            static void resetLanguage();
+            static void  resetLanguage();
 
             // fast access global variables.
@@ -73,9 +76,7 @@
 
         private:
+            Core(const Core&);
             void resetLanguageIntern();
-
-            Core();
-            Core(const Core& other);
-            virtual ~Core();
+            void initializeRandomNumberGenerator();
 
             int softDebugLevel_;                            //!< The debug level
@@ -84,4 +85,5 @@
             int softDebugLevelShell_;                       //!< The debug level for the ingame shell
             std::string language_;                          //!< The language
+            bool bInitializeRandomNumberGenerator_;          //!< If true, srand(time(0)) is called
 
             static bool bShowsGraphics_s;                   //!< global variable that tells whether to show graphics
@@ -90,4 +92,6 @@
             static bool bIsStandalone_s;
             static bool bIsMaster_s;
+
+            static Core* singletonRef_s;
     };
 }
Index: code/branches/presentation/src/core/CorePrereqs.h
===================================================================
--- code/branches/presentation/src/core/CorePrereqs.h	(revision 2459)
+++ code/branches/presentation/src/core/CorePrereqs.h	(revision 2485)
@@ -69,5 +69,6 @@
     {
       LoadObject,
-      SaveObject
+      SaveObject,
+      ExpandObject
     };
   }
@@ -132,4 +133,5 @@
   class LanguageEntry;
   class Loader;
+  class LuaBind;
   class MetaObjectList;
   class MetaObjectListElement;
Index: code/branches/presentation/src/core/Functor.h
===================================================================
--- code/branches/presentation/src/core/Functor.h	(revision 2459)
+++ code/branches/presentation/src/core/Functor.h	(revision 2485)
@@ -167,5 +167,5 @@
             }
 
-            FunctorMember* setObject(T* object)
+            FunctorMember<T>* setObject(T* object)
             {
                 this->bConstObject_ = false;
@@ -174,5 +174,5 @@
             }
 
-            FunctorMember* setObject(const T* object)
+            FunctorMember<T>* setObject(const T* object)
             {
                 this->bConstObject_ = true;
Index: code/branches/presentation/src/core/Identifier.cc
===================================================================
--- code/branches/presentation/src/core/Identifier.cc	(revision 2459)
+++ code/branches/presentation/src/core/Identifier.cc	(revision 2485)
@@ -93,4 +93,15 @@
         for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
             delete (it->second);
+        for (std::vector<Functor*>::iterator it = this->constructionCallbacks_.begin(); it != this->constructionCallbacks_.end(); ++it)
+            delete *it;
+    }
+
+    /**
+        @brief Returns the identifier map with the names as received by typeid(). This is only used internally.
+    */
+    std::map<std::string, Identifier*>& Identifier::getTypeIDIdentifierMap()
+    {
+        static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
+        return identifiers;
     }
 
@@ -103,8 +114,7 @@
     Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal)
     {
-        static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
-        std::map<std::string, Identifier*>::const_iterator it = identifiers.find(name);
-
-        if (it != identifiers.end())
+        std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
+
+        if (it != getTypeIDIdentifierMap().end())
         {
             // There is already an entry: return it and delete the proposal
@@ -115,5 +125,5 @@
         {
             // There is no entry: put the proposal into the map and return it
-            identifiers[name] = proposal;
+            getTypeIDIdentifierMap()[name] = proposal;
             return proposal;
         }
@@ -192,5 +202,5 @@
     void Identifier::destroyAllIdentifiers()
     {
-        for (std::map<std::string, Identifier*>::iterator it = Identifier::getIdentifierMapIntern().begin(); it != Identifier::getIdentifierMapIntern().end(); ++it)
+        for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it)
             delete (it->second);
     }
Index: code/branches/presentation/src/core/Identifier.h
===================================================================
--- code/branches/presentation/src/core/Identifier.h	(revision 2459)
+++ code/branches/presentation/src/core/Identifier.h	(revision 2485)
@@ -258,4 +258,6 @@
             void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
 
+            static void destroyAllIdentifiers();
+
         protected:
             Identifier();
@@ -300,7 +302,7 @@
             }
 
+            static std::map<std::string, Identifier*>& getTypeIDIdentifierMap();
+
             void initialize(std::set<const Identifier*>* parents);
-
-            static void destroyAllIdentifiers();
 
             std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
Index: code/branches/presentation/src/core/Language.cc
===================================================================
--- code/branches/presentation/src/core/Language.cc	(revision 2459)
+++ code/branches/presentation/src/core/Language.cc	(revision 2485)
@@ -87,4 +87,7 @@
     // ###        Language         ###
     // ###############################
+
+    Language* Language::singletonRef_s = 0;
+
     /**
         @brief Constructor: Reads the default language file and sets some values.
@@ -92,4 +95,7 @@
     Language::Language()
     {
+        assert(singletonRef_s == 0);
+        singletonRef_s = this;
+
         this->defaultLanguage_ = "default";
         this->defaultLocalisation_ = "ERROR: LANGUAGE ENTRY DOESN'T EXIST!";
@@ -106,14 +112,7 @@
         for (std::map<std::string, LanguageEntry*>::iterator it = this->languageEntries_.begin(); it != this->languageEntries_.end(); ++it)
             delete (it->second);
-    }
-
-    /**
-        @brief Returns a reference to the only existing instance of the Language class and calls the setConfigValues() function.
-        @return The reference to the only existing instance
-    */
-    Language& Language::getLanguage()
-    {
-        static Language instance = Language();
-        return instance;
+
+        assert(singletonRef_s);
+        singletonRef_s = 0;
     }
 
Index: code/branches/presentation/src/core/Language.h
===================================================================
--- code/branches/presentation/src/core/Language.h	(revision 2459)
+++ code/branches/presentation/src/core/Language.h	(revision 2485)
@@ -50,4 +50,5 @@
 #include <map>
 #include <string>
+#include <cassert>
 
 #define AddLanguageEntry(label, fallbackstring) \
@@ -116,12 +117,13 @@
 
         public:
-            static Language& getLanguage();
+            Language();
+            ~Language();
+
+            static Language& getLanguage() { assert(singletonRef_s); return *singletonRef_s; }
             void addEntry(const LanguageEntryLabel& label, const std::string& entry);
             const std::string& getLocalisation(const LanguageEntryLabel& label) const;
 
         private:
-            Language();
-            Language(const Language& language);     // don't copy
-            virtual ~Language();
+            Language(const Language&);
 
             void readDefaultLanguageFile();
@@ -134,4 +136,6 @@
             std::string defaultLocalisation_;                       //!< The returned string, if an entry unavailable entry is requested
             std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
+
+            static Language* singletonRef_s;
     };
 }
Index: code/branches/presentation/src/core/Loader.cc
===================================================================
--- code/branches/presentation/src/core/Loader.cc	(revision 2459)
+++ code/branches/presentation/src/core/Loader.cc	(revision 2485)
@@ -120,8 +120,8 @@
 
         // let Lua work this out:
-        LuaBind* lua = LuaBind::getInstance();
-        lua->clearLuaOutput();
-        lua->loadFile(file->getFilename(), true);
-        lua->run();
+        LuaBind& lua = LuaBind::getInstance();
+        lua.clearLuaOutput();
+        lua.loadFile(file->getFilename(), true);
+        lua.run();
 
         try
@@ -135,5 +135,5 @@
             ticpp::Document xmlfile;
             //xmlfile.ToDocument();
-            xmlfile.Parse(lua->getLuaOutput(), true);
+            xmlfile.Parse(lua.getLuaOutput(), true);
 
             ticpp::Element rootElement;
Index: code/branches/presentation/src/core/LuaBind.cc
===================================================================
--- code/branches/presentation/src/core/LuaBind.cc	(revision 2459)
+++ code/branches/presentation/src/core/LuaBind.cc	(revision 2485)
@@ -40,8 +40,11 @@
 namespace orxonox
 {
-  LuaBind* LuaBind::singletonRef = NULL;
+  LuaBind* LuaBind::singletonRef_s = NULL;
 
   LuaBind::LuaBind()
   {
+    assert(LuaBind::singletonRef_s == 0);
+    LuaBind::singletonRef_s = this;
+
     luaState_ = lua_open();
     luaSource_ = "";
Index: code/branches/presentation/src/core/LuaBind.h
===================================================================
--- code/branches/presentation/src/core/LuaBind.h	(revision 2459)
+++ code/branches/presentation/src/core/LuaBind.h	(revision 2485)
@@ -42,4 +42,5 @@
 }
 
+#include <cassert>
 #include <list>
 #include <string>
@@ -58,6 +59,8 @@
 
     public:
-      inline static LuaBind* getInstance() { if (!LuaBind::singletonRef) LuaBind::singletonRef = new LuaBind(); return LuaBind::singletonRef; } // tolua_export
-      inline ~LuaBind() { LuaBind::singletonRef = NULL; };
+      LuaBind();
+      inline ~LuaBind() { assert(singletonRef_s); LuaBind::singletonRef_s = NULL; };
+
+      inline static LuaBind& getInstance() { assert(singletonRef_s); return *LuaBind::singletonRef_s; } // tolua_export
 
     void loadFile(std::string filename, bool luaTags);
@@ -83,6 +86,5 @@
 
     private:
-      LuaBind();
-      static LuaBind* singletonRef;
+      static LuaBind* singletonRef_s;
 
       std::string luaSource_;
Index: code/branches/presentation/src/core/RootGameState.cc
===================================================================
--- code/branches/presentation/src/core/RootGameState.cc	(revision 2459)
+++ code/branches/presentation/src/core/RootGameState.cc	(revision 2485)
@@ -31,5 +31,4 @@
 #include "util/Debug.h"
 #include "util/Exception.h"
-#include "Core.h"
 #include "Clock.h"
 #include "CommandLine.h"
@@ -138,7 +137,4 @@
             Clock clock;
 
-            // create the Core settings to configure the output level
-            Core::getInstance();
-
             this->activate();
 
Index: code/branches/presentation/src/core/RootGameState.h
===================================================================
--- code/branches/presentation/src/core/RootGameState.h	(revision 2459)
+++ code/branches/presentation/src/core/RootGameState.h	(revision 2485)
@@ -48,5 +48,5 @@
         void gotoState(const std::string& name);
 
-        std::string           stateRequest_;
+        std::string stateRequest_;
     };
 }
Index: code/branches/presentation/src/core/Shell.cc
===================================================================
--- code/branches/presentation/src/core/Shell.cc	(revision 2459)
+++ code/branches/presentation/src/core/Shell.cc	(revision 2485)
@@ -75,5 +75,5 @@
 
         this->outputBuffer_.registerListener(this);
-        OutputHandler::getOutStream().setOutputBuffer(this->outputBuffer_);
+        OutputHandler::getOutStream().setOutputBuffer(&this->outputBuffer_);
 
         this->setConfigValues();
@@ -84,4 +84,5 @@
     Shell::~Shell()
     {
+        OutputHandler::getOutStream().setOutputBuffer(0);
         if (this->inputBuffer_)
             delete this->inputBuffer_;
Index: code/branches/presentation/src/core/Super.h
===================================================================
--- code/branches/presentation/src/core/Super.h	(revision 2459)
+++ code/branches/presentation/src/core/Super.h	(revision 2485)
@@ -99,4 +99,5 @@
             \
             static void apply(void* temp) {} \
+            \
             static void apply(baseclass* temp) \
             { \
@@ -104,4 +105,11 @@
                 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) \
                 { \
+                    if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \
+                    { \
+                        delete ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_; \
+                        ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = 0; \
+                        ((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ = false; \
+                    } \
+                    \
                     if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \
                     { \
@@ -163,4 +171,13 @@
                 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it)
                 {
+                    // Check if the caller is a fallback-caller
+                    if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_)
+                    {
+                        // Delete the fallback caller an prepare to get a real caller
+                        delete ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_;
+                        ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = 0;
+                        ((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ = false;
+                    }
+
                     // Check if there's not already a caller
                     if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_)
@@ -183,5 +200,5 @@
         struct SuperFunctionCondition<functionnumber, baseclass, 0, templatehack2> \
         { \
-            // The check function just behaves like the fallback - it advances to the check for the next super-function (functionnumber + 1)
+            // The check function acts like the fallback - it advances to the check for the next super-function (functionnumber + 1)
             static void check() \
             { \
@@ -233,4 +250,22 @@
     #define SUPER_processEvent(classname, functionname, ...) \
         SUPER_ARGS(classname, functionname, __VA_ARGS__)
+
+    #define SUPER_changedScale(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
+
+    #define SUPER_changedMainState(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
+
+    #define SUPER_changedOwner(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
+
+    #define SUPER_changedOverlayGroup(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
+
+    #define SUPER_changedName(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
+
+    #define SUPER_changedGametype(classname, functionname, ...) \
+        SUPER_NOARGS(classname, functionname)
     // (1/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
 
@@ -292,4 +327,20 @@
             }; \
             \
+            class _CoreExport SuperFunctionCaller_##functionname \
+            { \
+                public: \
+                    virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0; \
+                    virtual ~SuperFunctionCaller_##functionname () {} \
+            }; \
+            \
+            template <class T> \
+            class SuperFunctionClassCaller_purevirtualfallback_##functionname : public SuperFunctionCaller_##functionname \
+            { \
+                public: \
+                    inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) \
+                    { \
+                    } \
+            }; \
+            \
             template <class T> \
             struct SuperFunctionInitialization<functionnumber, T> \
@@ -297,5 +348,6 @@
                 static void initialize(ClassIdentifier<T>* identifier) \
                 { \
-                    identifier->superFunctionCaller_##functionname##_ = 0; \
+                    identifier->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_purevirtualfallback_##functionname <T>; \
+                    identifier->bSuperFunctionCaller_##functionname##_isFallback_ = true; \
                     SuperFunctionInitialization<functionnumber + 1, T>::initialize(identifier); \
                 } \
@@ -313,11 +365,4 @@
             }; \
             \
-            class _CoreExport SuperFunctionCaller_##functionname \
-            { \
-                public: \
-                    virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0; \
-                    virtual ~SuperFunctionCaller_##functionname () {} \
-            }; \
-            \
             template <class T> \
             class SuperFunctionClassCaller_##functionname : public SuperFunctionCaller_##functionname \
@@ -366,5 +411,25 @@
         };
 
-        // Initializes the SuperFunctionCaller-pointer with zero.
+        // Baseclass of the super-function caller. The real call will be done by a
+        // templatized subclass through the virtual () operator.
+        class _CoreExport SuperFunctionCaller_##functionname
+        {
+            public:
+                virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0;
+                virtual ~SuperFunctionCaller_##functionname () {}
+        };
+
+        // Fallback if the base is pure virtual
+        template <class T>
+        class SuperFunctionClassCaller_purevirtualfallback_##functionname : public SuperFunctionCaller_##functionname
+        {
+            public:
+                // Fallback does nothing
+                inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) )
+                {
+                }
+        };
+
+        // Initializes the SuperFunctionCaller-pointer with a fallback caller in case the base function is pure virtual
         template <class T>
         struct SuperFunctionInitialization<functionnumber, T>
@@ -372,5 +437,6 @@
             static void initialize(ClassIdentifier<T>* identifier)
             {
-                identifier->superFunctionCaller_##functionname##_ = 0;
+                identifier->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_purevirtualfallback_##functionname <T>;
+                identifier->bSuperFunctionCaller_##functionname##_isFallback_ = true;
 
                 // Calls the initialization of the next super-function (functionnumber + 1)
@@ -391,13 +457,4 @@
                 SuperFunctionDestruction<functionnumber + 1, T>::destroy(identifier);
             }
-        };
-
-        // Baseclass of the super-function caller. The real call will be done by a
-        // templatized subclass through the virtual () operator.
-        class _CoreExport SuperFunctionCaller_##functionname
-        {
-            public:
-                virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0;
-                virtual ~SuperFunctionCaller_##functionname () {}
         };
 
@@ -441,4 +498,28 @@
             (event)
         SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(5, changedScale, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(6, changedMainState, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(7, changedOwner, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(8, changedOverlayGroup, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(9, changedName, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
+
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(10, changedGametype, false)
+            ()
+        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
         // (2/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
 
@@ -476,5 +557,6 @@
         #ifndef SUPER_INTRUSIVE_DECLARATION
           #define SUPER_INTRUSIVE_DECLARATION(functionname) \
-            SuperFunctionCaller_##functionname * superFunctionCaller_##functionname##_
+            SuperFunctionCaller_##functionname * superFunctionCaller_##functionname##_; \
+            bool bSuperFunctionCaller_##functionname##_isFallback_
         #endif
 
@@ -488,4 +570,10 @@
     SUPER_INTRUSIVE_DECLARATION(changedVisibility);
     SUPER_INTRUSIVE_DECLARATION(processEvent);
+    SUPER_INTRUSIVE_DECLARATION(changedScale);
+    SUPER_INTRUSIVE_DECLARATION(changedMainState);
+    SUPER_INTRUSIVE_DECLARATION(changedOwner);
+    SUPER_INTRUSIVE_DECLARATION(changedOverlayGroup);
+    SUPER_INTRUSIVE_DECLARATION(changedName);
+    SUPER_INTRUSIVE_DECLARATION(changedGametype);
     // (3/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
 
Index: code/branches/presentation/src/core/Template.cc
===================================================================
--- code/branches/presentation/src/core/Template.cc	(revision 2459)
+++ code/branches/presentation/src/core/Template.cc	(revision 2485)
@@ -43,4 +43,5 @@
 
         this->bIsLink_ = false;
+        this->bLoadDefaults_ = true;
         this->bIsReturningXMLElement_ = false;
         this->baseclassIdentifier_ = 0;
@@ -56,6 +57,7 @@
         SUPER(Template, XMLPort, xmlelement, mode);
 
-        XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode);
-        XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode);
+        XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode);
+        XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode);
+        XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true);
 
         Element* element = xmlelement.FirstChildElement(false);
@@ -70,4 +72,6 @@
     void Template::changedName()
     {
+        SUPER(Template, changedName);
+
         if (this->getName() != "")
         {
@@ -134,5 +138,9 @@
 
         Element temp = ((TiXmlElement*)&this->getXMLElement());
-        object->XMLPort(temp, XMLPort::LoadObject);
+
+        if (this->bLoadDefaults_)
+            object->XMLPort(temp, XMLPort::LoadObject);
+        else
+            object->XMLPort(temp, XMLPort::ExpandObject);
     }
 
Index: code/branches/presentation/src/core/Template.h
===================================================================
--- code/branches/presentation/src/core/Template.h	(revision 2459)
+++ code/branches/presentation/src/core/Template.h	(revision 2485)
@@ -53,4 +53,9 @@
                 { return this->link_; }
 
+            inline void setLoadDefaults(bool bLoadDefaults)
+                { this->bLoadDefaults_ = bLoadDefaults; }
+            inline bool getLoadDefaults() const
+                { return this->bLoadDefaults_; }
+
             inline void setXMLElement(const TiXmlElement& xmlelement)
                 { this->xmlelement_ = xmlelement; }
@@ -75,4 +80,5 @@
             Identifier* baseclassIdentifier_;
             bool bIsLink_;
+            bool bLoadDefaults_;
             mutable bool bIsReturningXMLElement_;
     };
Index: code/branches/presentation/src/core/XMLPort.h
===================================================================
--- code/branches/presentation/src/core/XMLPort.h	(revision 2459)
+++ code/branches/presentation/src/core/XMLPort.h	(revision 2485)
@@ -43,4 +43,5 @@
 #include "CorePrereqs.h"
 
+#include <cassert>
 #include "util/Debug.h"
 #include "util/Exception.h"
@@ -369,4 +370,12 @@
             }
 
+            ~XMLPortClassParamContainer()
+            {
+                assert(this->loadexecutor_);
+                delete this->loadexecutor_;
+                if (this->saveexecutor_)
+                    delete this->saveexecutor_;
+            }
+
             XMLPortParamContainer& port(BaseObject* owner, T* object, Element& xmlelement, XMLPort::Mode mode)
             {
@@ -376,17 +385,19 @@
                 this->parseParams_.mode = mode;
 
-                if (mode == XMLPort::LoadObject)
+                if ((mode == XMLPort::LoadObject) || (mode == XMLPort::ExpandObject))
                 {
                     try
                     {
                         std::string attribute = xmlelement.GetAttribute(this->paramname_);
-                        if ((attribute.size() > 0) || (this->loadexecutor_->allDefaultValuesSet()))
+                        if ((attribute.size() > 0) || ((mode != XMLPort::ExpandObject) && this->loadexecutor_->allDefaultValuesSet()))
                         {
                             COUT(5) << this->owner_->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << this->identifier_->getName() << " (objectname " << this->owner_->getName() << ")." << std::endl << this->owner_->getLoaderIndentation();
-                            if (this->loadexecutor_->parse(object, attribute, ","))
+                            if (this->loadexecutor_->parse(object, attribute, ",") || (mode  == XMLPort::ExpandObject))
                                 this->parseResult_ = PR_finished;
                             else
                                 this->parseResult_ = PR_waiting_for_default_values;
                         }
+                        else if (mode == XMLPort::ExpandObject)
+                            this->parseResult_ = PR_finished;
                         else
                             this->parseResult_ = PR_waiting_for_default_values;
@@ -511,7 +522,15 @@
             }
 
+            ~XMLPortClassObjectContainer()
+            {
+                assert(this->loadexecutor_);
+                delete this->loadexecutor_;
+                if (this->saveexecutor_)
+                    delete this->saveexecutor_;
+            }
+
             XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
             {
-                if (mode == XMLPort::LoadObject)
+                if ((mode == XMLPort::LoadObject) || (mode == XMLPort::ExpandObject))
                 {
                     try
Index: code/branches/presentation/src/core/input/Button.cc
===================================================================
--- code/branches/presentation/src/core/input/Button.cc	(revision 2459)
+++ code/branches/presentation/src/core/input/Button.cc	(revision 2485)
@@ -59,4 +59,5 @@
         nCommands_[1]=0;
         nCommands_[2]=0;
+        this->configContainer_ = 0;
         clear();
     }
@@ -80,4 +81,8 @@
             }
         }
+
+        if (this->configContainer_)
+            delete this->configContainer_;
+        this->configContainer_ = 0;
     }
 
Index: code/branches/presentation/src/core/input/InputBuffer.cc
===================================================================
--- code/branches/presentation/src/core/input/InputBuffer.cc	(revision 2459)
+++ code/branches/presentation/src/core/input/InputBuffer.cc	(revision 2485)
@@ -73,4 +73,11 @@
     }
 
+    InputBuffer::~InputBuffer()
+    {
+        for (std::list<BaseInputBufferListenerTuple*>::const_iterator it = this->listeners_.begin();
+            it != this->listeners_.end(); ++it)
+            delete *it;
+    }
+
     void InputBuffer::setConfigValues()
     {
Index: code/branches/presentation/src/core/input/InputBuffer.h
===================================================================
--- code/branches/presentation/src/core/input/InputBuffer.h	(revision 2459)
+++ code/branches/presentation/src/core/input/InputBuffer.h	(revision 2485)
@@ -79,4 +79,5 @@
         public:
             InputBuffer();
+            ~InputBuffer();
             InputBuffer(const std::string allowedChars);
 
