Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/Script.h @ 7463

Last change on this file since 7463 was 7463, checked in by dafrick, 14 years ago

Fixing "bug", that caused crash in dedicated mode.
Script object can now specify whether the code that is executed by it needs graphics to work.

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Benjamin Knecht
24 *   Co-authors:
25 *      Damian 'Mozork' Frick
26 *
27 */
28
29#ifndef _Script_H__
30#define _Script_H__
31
32#include "objects/ObjectsPrereqs.h"
33
34#include <string>
35#include "core/BaseObject.h"
36
37namespace orxonox
38{
39
40    namespace ScriptMode
41    {
42        //! Modes of the Script class.
43        enum Value
44        {
45            normal,
46            lua
47        };
48    }
49
50    /**
51    @brief
52        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.
53        There are three parameters:
54        'code': The code that should be executed.
55        'mode': The mode, specifying whether the set code should be executed the normal way ('normal') or in lua ('lua'). Default is 'normal'.
56        'onLoad': Whether the code is executed upon loading (creation) of this object. Default is true.
57        'needsGraphics': Whether the code needs graphics to be executed or not. Default is false.
58
59        Here are two examples illustrating the usage:
60        @code
61        <Script code="showGUI QuestGUI" needsGraphics=true />
62        @endcode
63        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. Also needsGraphics is set to true because showGUI needs graphics to work.
64
65        @code
66        <Script code="hideGUI QuestGUI" mode="normal" onLoad="false" needsGraphics=true >
67            <events>
68                <trigger>
69                    <DistanceTrigger distance=10 target="Pawn" />
70                </trigger>
71            </events>
72        </Script>
73        @endcode
74        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. Also needsGraphics is set to true because showGUI needs graphics to work.
75    @author
76        Benjamin Knecht
77        Damian 'Mozork' Frick
78    */
79    class _ObjectsExport Script : public BaseObject
80    {
81        public:
82            Script(BaseObject* creator);
83            virtual ~Script();
84
85            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML.
86            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
87
88            void trigger(bool triggered); //!< Is called when an event comes in trough the event port.
89            void execute(); //!< Executes the Scripts code, depending on the mode.
90
91            /**
92            @brief Sets the code that is executed by this Script.
93            @param code  The code that is executed by this Script.
94            */
95            inline void setCode(const std::string& code)
96                { code_ = code; }
97            /**
98            @brief Get the code that is executed by this Script.
99            @return Returns the code that is executed by this Script.
100            */
101            inline const std::string& getCode() const
102                { return code_; }
103
104            void setMode(const std::string& mode); //!< Sets the mode of the Script.
105            const std::string& getMode(void); //!< Get the mode of the Script.
106
107            /**
108            @brief Set whether this Script is executed onLoad or not.
109            @param onLoad if true the Script is executed onLoad, if false it's not.
110            */
111            inline void setOnLoad(bool onLoad)
112                { this->onLoad_ = onLoad; }
113            /**
114            @brief Get whether this Script is executed onLoad.
115            @return Returns true if this Script is executed onLoad, false if not.
116            */
117            inline bool isOnLoad(void)
118                { return this->onLoad_; }
119
120            void setTimes(int times); //!< Set the number of times this Script is executed at the most.
121            /**
122            @brief Get the number of times this Script is executed at the most.
123            @return Returns the number of times this Script is executed at the most. -1 denotes infinity.
124            */
125            inline int getTimes(void)
126                { return this->times_; }
127
128            /**
129            @brief Set whether the code to be executed needs graphics to work.
130            @param needsGraphics True if the cde needs graphics to be executed properly.
131            */
132            void setNeedsGraphics(bool needsGraphics)
133                { this->needsGraphics_ = needsGraphics; }
134            /**
135            @brief Get whether the code to be executed needs graphics to work.
136            @return Returns true if the code needs graphic, false if not.
137            */
138            bool getNeedsGraphics(void)
139                { return this->needsGraphics_; }
140
141        private:
142            //! Static variables to avoid magic strings.
143            static const std::string NORMAL;
144            static const std::string LUA;
145            static const int INF;
146
147            std::string code_; //!< The code that is executed by this Script.
148            ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua.
149            bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script.
150            int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
151            bool needsGraphics_; //!< Whether the code to be executed needs graphics.
152
153            LuaState* luaState_; //!< The LuaState to execute the code in lua.
154            int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
155
156            /**
157            @brief Sets the mode of the Script.
158            @param mode The mode of the Script.
159            */
160            inline void setMode(ScriptMode::Value mode)
161                { this->mode_ = mode; }
162    };
163}
164
165#endif /* _Script_H__ */
Note: See TracBrowser for help on using the repository browser.