summaryrefslogtreecommitdiffstats
path: root/source/LuaScript.h
blob: cf480690332d29d8f635cfa2cf01639de2164db7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once

struct lua_State;

struct sLuaUsertype
{
	sLuaUsertype(void* a_pObject, const char* a_pClassName) : Object(a_pObject), ClassName(a_pClassName) {}
	//
	void*       Object;
    const char* ClassName;
};

class cLuaScript
{
public:
	cLuaScript();
	~cLuaScript();

	/// Prepares a Lua state
	void        Initialize();

	/// Load a Lua script on the given path
	bool        LoadFile(const char* a_FilePath);

	/// Execute the loaded Lua script
	bool        Execute();

	/// Call a function on the Lua script. Put all overloads here
	bool        CallFunction(const char* a_Function, AString& ReturnedString);
	bool        CallFunction(const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString);
	bool        CallFunction(const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString);

protected:
	/// Reports an error in the log if a_Status is flagged as an error. Returns true when a_Status is flagged as error, returns false when no error occured.
	bool        ReportErrors(int a_Status);

	/// Helper functions for calling functions in Lua 
	bool        LuaPushFunction(const char * a_FunctionName, bool a_bLogError = true);
	bool        LuaCallFunction(int a_NumArgs, int a_NumResults, const char * a_FunctionName );  // a_FunctionName is only used for error messages, nothing else
private:
	lua_State*  m_LuaState;
};