尝试使用表作为从外部传递的用户生成数据的查找。 tab = { ['on'] = function(x) x=x+1 return x end,
['off'] = function(x) x=x+2 return x end,
['high']= function(x) x=x+3 return x end,
['low'] = function(x) x=x+4 return x end
}
do
local var=0
local userDat
如果我创建一个userdata对象并将其保存在一个表中,那么在C/C++中获得对它的引用,该引用有效的时间是多久?只要用户数据保存在Lua中的表中,C/C++中的引用是否保证有效?或者,Lua运行时是否会移动userdata对象,从而使C/C++引用无效?
我正在做的事情是:
// Initially, the stack contains a table
class Foo { ... };
lua_pushstring(L, "my_userdata");
void* mem = lua_newuserdata(L, sizeof(Foo));
new (mem) Foo(
如果我有一个暴露在Lua上的C函数,我将一个userdata对象作为参数传递给lua,那么C函数可以在lua端将这个变量设置为'nil‘吗?
// From C++
static void Delete(lua_State* L){
// the first param will be userdata and I want to set it so after this call in lua happens whatever was passed in will be nil
}
-- from lua
local obj = CreateUserDataObject()
我使用lua_pcall调用一些函数,并希望捕获错误。在某些情况下,错误似乎消失了。怎么会发生这种事?这既适用于使用错误处理程序的情况,也适用于不使用错误处理程序的情况。在这两种情况下,堆栈的顶部都不是字符串。
C代码:
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_replace(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, my_func_index);
// now push n_in number of value
我正在尝试将Lua类对象推送到堆栈上。指向该对象的指针可以由多个函数返回。
换句话说:我需要推送用户数据值,同时保持对它们使用'==','~=‘等的能力,所以如果用户数据指针是相同的C++对象,那么它必须是相同的。
-- this should push the object onto the stack
local firstObject = GetClassObject();
firstObject:doSomething();
firstObject将由lua脚本存储,稍后在代码中我将需要再次执行此操作:
-- the c++ class pointer has
因为我的userdata对象引用了自己,所以我需要删除和0,这是垃圾收集器工作的一个变量。
Lua代码:
obj = object:new()
--
-- Some time later
obj:delete() -- Removes the self reference
obj = nil -- Ready for collection
C代码:
typedef struct {
int self; // Reference to the object
int callback; // Reference to a Lua function
// Othe
我在C++中有以下类
class B;
class A {
B* GetB();
void SetB(B*& b) { _b = b;};
private:
B* _b;
}
以及lua绑定代码的一部分:
int A::setB(lua_State* L) {
A* a = checkA(L,1) // Macro for luaL_checkudata
B* b = checkB(L,2) // again similar macro
a->SetB(b);
return 0;
}
int A::g
我想向来自C++的Lua userdata对象添加一些静态字段。有问题的对象是向量,它们是在C++中创建的,它们的工作方式是这样的,但我在我的用户数据上尝试了lua_setfield,但我得到了一个attempt to index a Vector value错误,我真的不想使用.x(), .y(), .z(),因为必须调用一个函数,推送到堆栈,然后在lua端读取。
有没有办法在userdata上注册字段以供Lua访问?
在Lua5.1中,可以将环境表与用户数据关联起来。这允许我们将“字段”添加到单个用户数据中。
显而易见的方法是在创建userdata时创建这个环境表,可能是空表。但是,在我的应用程序中,会有很多用户数据不一定需要这个环境表,而且它们很快就会出现。我不希望创建这么多不会被使用的临时空表的开销。
因此,相反,我只想在检测到userdata还没有创建一个环境表时才创建环境表。
问题是,在Lua5.1中,默认环境表不是nil。它被认为是全球的桌子,_G (我想知道这到底有什么用)。因此,据推测,我会通过以下操作来测试一个未初始化的用户数据:
/* 'index' is where my
如何将C++对象返回给lua?
我的C++代码如下:
class MyClass
{
public:
void say()
{
print("Hello\r\n");
}
};
int test(lua_State* l)
{
MyClass* obj = new MyClass();
lua_pushlightuserdata(l, obj);
return 1;
}
Lua测试如下:
local a = MyClass:new()
a:say() <--- OK, beacause I set metatable!!
local b
问题:如何使用C#对象(最好是匿名类型)将表从C#插入到“LuaInterface”脚本范围?
/// I want to do this, but it does not work
/// (complains that 'test' is userdata and not table
/// when I pass it to pairs() in the script)
//lua["test"] = new { A = 1, B = 2 };
/// another option
/// but building this string is a
有没有一种方法可以将数据附加到协程,或者至少以某种方式识别不同的协程? 我正在尝试实现一个计时器API,其中计时器由主机控制,在Lua端看起来像下面这样: function callback()
local timer = ElapsedTimer()
...
end
function main()
local timer = CreateTimer(...)
StartTimer(timer, callback, ...)
end StartTimer()调用将计时器和回调发送到C端,C端最终将在新的协程中调用回调。 对ElapsedTimer()的调用需要返回特定于该
我试图向Lua脚本提供一个整数列表,并对它们进行迭代。
我得到的错误是:
test.lua:12: bad argument #1 to 'pairs' (table expected, got userdata) stack traceback: [C]: in function 'pairs' test.lua:12: in main chunk
正在使用的C++代码:
#include <sol.hpp>
struct Test {
std::vector<int> a;
};
int m