Index: code/trunk/src/modules/objects/Script.cc
===================================================================
--- code/trunk/src/modules/objects/Script.cc	(revision 7403)
+++ code/trunk/src/modules/objects/Script.cc	(revision 7404)
@@ -23,5 +23,5 @@
  *      Benjamin Knecht
  *   Co-authors:
- *      ...
+ *      Damian 'Mozork' Frick
  *
  */
@@ -29,5 +29,7 @@
 #include "Script.h"
 
+#include "core/command/CommandExecutor.h"
 #include "core/CoreIncludes.h"
+#include "core/EventIncludes.h"
 #include "core/LuaState.h"
 #include "core/XMLPort.h"
@@ -37,28 +39,166 @@
     CreateFactory(Script);
 
+    // Initializing constants.
+    /*static*/ const std::string Script::NORMAL = "normal";
+    /*static*/ const std::string Script::LUA = "lua";
+
+    /**
+    @brief
+        Constructor. Registers and initializes the object.
+    @param creator
+        The creator of this object.
+    */
     Script::Script(BaseObject* creator) : BaseObject(creator)
     {
         RegisterObject(Script);
 
-        // Get a new LuaState
-        luaState_ = new LuaState();
-    }
-
+        // Initialize variables.
+        this->luaState_ = NULL;
+        this->remainingExecutions_ = Script::INF;
+
+    }
+
+    /**
+    @brief
+        Destructor. Cleans up.
+    */
     Script::~Script()
     {
-        if (this->isInitialized())
-            delete luaState_;
-    }
-
-    void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
-    {
-        BaseObject::XMLPort(xmlelement, mode);
-
-        XMLPortParam(Script, "code", setCode, getCode, xmlelement, mode);
-    }
-
+        if(this->isInitialized() && this->luaState_ != NULL)
+            delete this->luaState_;
+    }
+
+    /**
+    @brief
+        Method for creating a Script object through XML.
+    @param xmlElement
+        The element.
+    @param mode
+        The mode.
+    */
+    void Script::XMLPort(Element& xmlElement, XMLPort::Mode mode)
+    {
+        SUPER(Script, XMLPort, xmlElement, mode);
+
+        XMLPortParam(Script, "code", setCode, getCode, xmlElement, mode);
+        XMLPortParamTemplate(Script, "mode", setMode, getMode, xmlElement, mode, const std::string&).defaultValues(Script::NORMAL);
+        XMLPortParam(Script, "onLoad", setOnLoad, isOnLoad, xmlElement, mode).defaultValues(true);
+        XMLPortParam(Script, "times", setTimes, getTimes, xmlElement, mode).defaultValues(Script::INF);
+
+        XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlElement, mode);
+
+        if(this->isOnLoad()) // If the object is onLoad the code is executed at once.
+            this->execute();
+    }
+
+    /**
+    @brief
+        Creates a port that can be used to channel events and react to them.
+    @param xmlElement
+        The element.
+    @param mode
+        The mode.
+    */
+    void Script::XMLEventPort(Element& xmlElement, XMLPort::Mode mode)
+    {
+        SUPER(Script, XMLEventPort, xmlElement, mode);
+
+        XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlElement, mode);
+    }
+
+    /**
+    @brief
+        Is called when an event comes in trough the event port.
+    @param triggered
+        Whether the event is triggering or un-triggering.
+    */
+    void Script::trigger(bool triggered)
+    {
+        if(triggered) // If the event is triggering (instead of un-triggering) the code of this Script  is executed.
+            this->execute();
+    }
+
+    /**
+    @brief
+        Executes the Scripts code, depending on the mode.
+    */
     void Script::execute()
     {
-        luaState_->doString(code_);
-    }
+        if(this->times_ != Script::INF && this->remainingExecutions_ == 0)
+            return;
+
+        if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'.
+            CommandExecutor::execute(this->code_);
+        else if(this->mode_ == ScriptMode::lua) // If it's 'lua'.
+        {
+            assert(this->luaState_);
+            this->luaState_->doString(this->code_);
+        }
+
+        if(this->times_ != Script::INF)
+            this->remainingExecutions_--;
+    }
+
+    /**
+    @brief
+        Sets the mode of the Script.
+    @param mode
+        The mode as a string.
+    */
+    void Script::setMode(const std::string& mode)
+    {
+        if(mode == Script::NORMAL)
+            this->setMode(ScriptMode::normal);
+        else if(mode == Script::LUA)
+        {
+            this->setMode(ScriptMode::lua);
+            // Creates a new LuaState.
+            if(this->luaState_ == NULL)
+                this->luaState_ = new LuaState();
+        }
+        else
+        {
+            COUT(2) << "Invalid mode '" << mode << "' in Script object." << std::endl;
+            this->setMode(ScriptMode::normal);
+        }
+    }
+
+    /**
+    @brief
+        Get the mode of the Script.
+    @return
+        Returns the mode as a string.
+    */
+    const std::string& Script::getMode(void)
+    {
+        switch(this->mode_)
+        {
+            case ScriptMode::normal:
+                return Script::NORMAL;
+            case ScriptMode::lua:
+                return Script::LUA;
+        }
+    }
+
+    /**
+    @brief
+        Set the number of times this Script is executed at the most.
+        -1 denotes infinity.
+    @param times
+        The number of times to be set.
+    */
+    void Script::setTimes(int times)
+    {
+        if(times >= -1)
+        {
+            this->times_ = times;
+            this->remainingExecutions_ = times;
+        }
+        else
+        {
+            COUT(2) << "Invalid times '" << times << "' in Script." << std::endl;
+            this->times_ = Script::INF;
+        }
+    }
+
 }
Index: code/trunk/src/modules/objects/Script.h
===================================================================
--- code/trunk/src/modules/objects/Script.h	(revision 7403)
+++ code/trunk/src/modules/objects/Script.h	(revision 7404)
@@ -23,5 +23,5 @@
  *      Benjamin Knecht
  *   Co-authors:
- *      ...
+ *      Damian 'Mozork' Frick
  *
  */
@@ -37,18 +37,112 @@
 namespace orxonox
 {
+
+    namespace ScriptMode
+    {
+        //! Modes of the Script class.
+        enum Value
+        {
+            normal,
+            lua
+        };
+    }
+
+    /**
+    @brief
+        The Script class lets you execute a piece of code, either the normal way or in lua, through XML. It can be specified whether the code is executed upon loading (creation) of the object. Additionally the code is executed each time a trigger event comes in.
+        There are three parameters:
+        'code': The code that should be executed.
+        'mode': The mode, specifying whether the set code should be executed the normal way ('normal') or in lua ('lua'). Default is 'normal'.
+        'onLoad': Whether the code is executed upon loading (creation) of this object. Default is true.
+
+        Here are two examples illustrating the usage:
+        @code
+        <Script code="showGUI QuestGUI" />
+        @endcode
+        This would show the QuestGUI opon creation of the object. The mode is 'normal', not specified here since that is the default, also onLoad is true, also not specified, since it is the default as well.
+
+        @code
+        <Script code="hideGUI QuestGUI" mode="normal" onLoad="false">
+            <events>
+                <trigger>
+                    <DistanceTrigger distance=10 target="Pawn" />
+                </trigger>
+            </events>
+        </Script>
+        @endcode
+        This would hide the QuestGUI as soon as a Pawn got in range of the DistanceTrigger. The mode is 'normal', it is specified here, but could be ommitted as well, since it is the default. OnLoad is false, that is why it can't be ommitted.
+    @author
+        Benjamin Knecht
+        Damian 'Mozork' Frick
+    */
     class _ObjectsExport Script : public BaseObject
     {
-    public:
-        Script(BaseObject* creator);
-        ~Script();
-        void XMLPort(Element& xmlelement, XMLPort::Mode mode);
-        void execute();
+        public:
+            Script(BaseObject* creator);
+            virtual ~Script();
 
-        void setCode(const std::string& code) { code_ = code; }
-        const std::string& getCode() const { return code_; }
+            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML.
+            virtual void XMLEventPort(Element& xmlElement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
 
-    private:
-        std::string code_;
-        LuaState* luaState_;
+            void trigger(bool triggered); //!< Is called when an event comes in trough the event port.
+            void execute(); //!< Executes the Scripts code, depending on the mode.
+
+            /**
+            @brief Sets the code that is executed by this Script.
+            @param code  The code that is executed by this Script.
+            */
+            inline void setCode(const std::string& code)
+                { code_ = code; }
+            /**
+            @brief Get the code that is executed by this Script.
+            @return Returns the code that is executed by this Script.
+            */
+            inline const std::string& getCode() const
+                { return code_; }
+
+            void setMode(const std::string& mode); //!< Sets the mode of the Script.
+            const std::string& getMode(void); //!< Get the mode of the Script.
+
+            /**
+            @brief Set whether this Script is executed onLoad or not.
+            @param onLoad if true the Script is executed onLoad, if false it's not.
+            */
+            inline void setOnLoad(bool onLoad)
+                { this->onLoad_ = onLoad; }
+            /**
+            @brief Get whether this Script is executed onLoad.
+            @return Returns true if this Script is executed onLoad, false if not.
+            */
+            inline bool isOnLoad(void)
+                { return this->onLoad_; }
+
+            void setTimes(int times); //!< Set the number of times this Script is executed at the most.
+            /**
+            @brief Get the number of times this Script is executed at the most.
+            @return Returns the number of times this Script is executed at the most. -1 denotes infinity.
+            */
+            inline int getTimes(void)
+                { return this->times_; }
+
+        private:
+            //! Static variables to avoid magic strings.
+            static const std::string NORMAL;
+            static const std::string LUA;
+            static const int INF = -1;
+
+            std::string code_; //!< The code that is executed by this Script.
+            ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua.
+            bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script.
+            int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
+
+            LuaState* luaState_; //!< The LuaState to execute the code in lua.
+            int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
+
+            /**
+            @brief Sets the mode of the Script.
+            @param mode The mode of the Script.
+            */
+            inline void setMode(ScriptMode::Value mode)
+                { this->mode_ = mode; }
     };
 }
