Lua object oriented; comment script.h
[Tsunagari.git] / src / script.h
blob295b0f9595404553a8b60d541a5347c320f7c6fc
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** script.h **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #ifndef SCRIPT_H
8 #define SCRIPT_H
10 #include <string>
12 #include <lua.hpp>
14 class Entity;
16 //! Compile and execute Lua code at runtime.
17 /*!
18 A Script represents a self-contained Lua state. It might have
19 variables, functions, statements, and expressions, and can be executed.
20 Each Script lives in a separate memory space and cannot directly
21 access or call other Scripts.
23 On their own, Scripts cannot change anything in a game. Instead, you
24 must bind C functions that they can call. These C functions can access
25 the C++ objects in the game. You might bind a function that makes a
26 player jump, or that returns the distance to the nearest town. You can
27 also bind variables that a script can access.
29 Scripts use Lua 5.1
31 class Script
33 public:
34 //! Create a new Lua state. The state is destroyed with the death of
35 //this object.
36 Script();
38 //! If we already have a Lua state, wrap around it. The state is not
39 //destroyed with the death of this object.
40 Script(lua_State* L);
42 //! Destroy the Lua state if we own it.
43 ~Script();
46 //! Bind a C function to a global variable in Lua.
47 void bindGlobalFn(const char* name, lua_CFunction fn);
49 //! Bind a C function to a table index. In Lua: "table.index = fn"
50 void bindObjFn(const char* table, const char* index, lua_CFunction fn);
52 //! Set a global integer variable in Lua.
53 void bindInt(const char* name, lua_Integer i);
56 //! Create a global table with specified name which represents an Entity.
57 void bindEntity(const char* name, Entity* entity);
59 //! Get the Entity object from a table in the Lua stack at a specific
60 //position.
61 Entity* getEntity(int pos);
64 //! Compile and run a script, keeping all existing bindings intact.
65 void run(const char* fn);
67 private:
68 //! Did we create our state, or are we borrowing it from another Script
69 //object?
70 bool ownState;
72 //! Everything necessary to compile and run a Lua script.
73 lua_State* L; // TODO: change to shared_ptr ?
76 #endif