0

lua から c++ にアクセスするために luabridge に関するいくつかのチュートリアルを行ってきましたが、Google を検索しても答えが見つからないように見える問題に遭遇しました。

スクリプトを実行し、c++ から lua にアクセスするサンプル プログラムをセットアップしました。これは正常に動作します。しかし、関数をグローバル名前空間に登録しようとすると、実行時にエラーが発生します-コンパイルは問題ありません。

#include <iostream>
#include <string>

#include "../LuaBridge/LuaBridge.h"

using namespace luabridge;

void printMessage(const std::string& s) {
    std::cout << s << std::endl;
}

int main() {
    lua_State* L = luaL_newstate();

    getGlobalNamespace(L).addFunction("printMessage", printMessage);

    luaL_dofile(L, "script.lua");
    luaL_openlibs(L);
    lua_pcall(L, 0, 0, 0);
    LuaRef s = getGlobal(L, "testString");
    LuaRef n = getGlobal(L, "number");
    std::string luaString = s.cast<std::string>();
    int answer = n.cast<int>();
    std::cout << luaString << std::endl;
    std::cout << "And here's our number:" << answer << std::endl;
}

したがって、addFunction 呼び出しを含むこのコードでは、このエラーが発生します

Lua: /home/linuxxon/Programming/kingdoms-online/Script/Lua/../LuaBridge/detail/Namespace.h:1080: luabridge::Namespace& luabridge::Namespace::addFunction(const char*, FP) [with FP = void (*)(const std::basic_string<char>&)]: Assertion `(lua_type(L, (-1)) == 5)' failed.

addFunction 呼び出しがなければ、スクリプトに期待されるものを取得できます。

そのようなものを見つけられなかったので、私が見逃した明らかなものがあるのでしょうか?

すべての助けに感謝します!

4

1 に答える 1

1

問題は、

luaL_openlibs(L);

スクリプトが実行された後でした。問題は、私がフォローしていたチュートリアルにありました.luaでの試行の最初に同じことに遭遇しました.

新しい lua 状態を作成した後に呼び出すことで、すべてが完全に機能します。

于 2014-08-05T15:56:31.063 に答える