-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="LuaFunctionRef" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> | ||
<brief_description> | ||
Lua Function Reference. | ||
</brief_description> | ||
<description> | ||
Reference to a Lua function. This class is used to pass Lua functions to Godot. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="inoke"> | ||
<return type="Variant" /> | ||
<param index="0" name="Arguments" type="Array" /> | ||
<description> | ||
Invoke the Lua function being referenced. | ||
</description> | ||
</method> | ||
</methods> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "luaFunctionRef.h" | ||
|
||
#include <luaState.h> | ||
|
||
void LuaFunctionRef::_bind_methods() { | ||
ClassDB::bind_method(D_METHOD("invoke", "args"), &LuaFunctionRef::invoke); | ||
} | ||
|
||
LuaFunctionRef::LuaFunctionRef() { | ||
L = nullptr; | ||
ref = LUA_NOREF; | ||
} | ||
|
||
LuaFunctionRef::~LuaFunctionRef() { | ||
luaL_unref(L, LUA_REGISTRYINDEX, ref); | ||
} | ||
|
||
void LuaFunctionRef::setLuaState(lua_State *state) { | ||
L = state; | ||
} | ||
|
||
void LuaFunctionRef::setRef(int ref) { | ||
this->ref = ref; | ||
} | ||
|
||
Variant LuaFunctionRef::invoke(Array args) { | ||
lua_pushcfunction(L, LuaState::luaErrorHandler); | ||
lua_rawgeti(L, LUA_REGISTRYINDEX, ref); | ||
|
||
for (int i = 0; i < args.size(); i++) { | ||
Variant arg = args[i]; | ||
LuaState::pushVariant(L, arg); | ||
} | ||
|
||
int err = lua_pcall(L, args.size(), 1, -2 - args.size()); | ||
Variant ret; | ||
if (err) { | ||
ret = LuaState::handleError(L, ret); | ||
} else { | ||
ret = LuaState::getVariant(L, -1); | ||
} | ||
|
||
lua_pop(L, 1); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef LUAFUNCTIONREF_H | ||
#define LUAFUNCTIONREF_H | ||
|
||
#ifndef LAPI_GDEXTENSION | ||
#include "core/core_bind.h" | ||
#include "core/object/ref_counted.h" | ||
#else | ||
#include <godot_cpp/classes/ref.hpp> | ||
#endif | ||
|
||
#include <lua/lua.hpp> | ||
|
||
#ifdef LAPI_GDEXTENSION | ||
using namespace godot; | ||
#endif | ||
|
||
class LuaFunctionRef : public RefCounted { | ||
GDCLASS(LuaFunctionRef, RefCounted); | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
LuaFunctionRef(); | ||
~LuaFunctionRef(); | ||
|
||
void setLuaState(lua_State *state); | ||
void setRef(int ref); | ||
|
||
Variant invoke(Array args); | ||
|
||
inline int getRef() const { return ref; } | ||
inline lua_State *getLuaState() const { return L; } | ||
|
||
private: | ||
lua_State *L; | ||
int ref; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters