scripts load from world
[Tsunagari.git] / src / script.h
blob2054e16357bdcc8472069ad4abb3d120e1247776
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;
15 class Resourcer;
17 //! Compile and execute Lua code at runtime.
18 /*!
19 A Script represents a self-contained Lua state. It might have
20 variables, functions, statements, and expressions, and can be executed.
21 Each Script lives in a separate memory space and cannot directly
22 access or call other Scripts.
24 On their own, Scripts cannot change anything in a game. Instead, you
25 must bind C functions that they can call. These C functions can access
26 the C++ objects in the game. You might bind a function that makes a
27 player jump, or that returns the distance to the nearest town. You can
28 also bind variables that a script can access.
30 Scripts use Lua 5.1
32 class Script
34 public:
35 //! Create a new Lua state. The state is destroyed with the death of
36 //this object.
37 Script();
39 //! If we already have a Lua state, wrap around it. The state is not
40 //destroyed with the death of this object.
41 Script(lua_State* L);
43 //! Destroy the Lua state if we own it.
44 ~Script();
47 //! Bind a C function to a global variable in Lua.
48 void bindGlobalFn(const char* name, lua_CFunction fn);
50 //! Bind a C function to a table index. In Lua: "table.index = fn"
51 void bindObjFn(const char* table, const char* index, lua_CFunction fn);
53 //! Set a global integer variable in Lua.
54 void bindInt(const char* name, lua_Integer i);
57 //! Create a global table with specified name which represents an Entity.
58 void bindEntity(const char* name, Entity* entity);
60 //! Get the Entity object from a table in the Lua stack at a specific
61 //position.
62 Entity* getEntity(int pos);
65 //! Compile and run a script, keeping all existing bindings intact.
66 void run(Resourcer* rc, const char* fn);
68 private:
69 //! Did we create our state, or are we borrowing it from another Script
70 //object?
71 bool ownState;
73 //! Everything necessary to compile and run a Lua script.
74 lua_State* L; // TODO: change to shared_ptr ?
77 #endif