在Lua中访问Light用户数据 [英] Accessing Light userdata in Lua

查看:239
本文介绍了在Lua中访问Light用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会误解他们的使用或误读文档,但是如何访问作为light userdata传递给Lua的结构或类的成员?例如,如果一个向量使用以下struct

I may be misunderstanding their use or misread the documentation, but how do I access members of a struct or class passed to Lua as light userdata? For example if a vector using the following struct

typedef struct Foo {
  int x;
  int y;
} Foo;

声明为test,并定义为类似x = 413和y = 612,

was declared as 'test' and defined as something like x = 413 and y = 612, and pushed with a call

lua_pushlightuserdata( L, (void *) &test ) ;

操作或打印x和y的函数的Lua代码是什么样子?

What would the Lua code of a function that manipulates or prints x and y look like?

推荐答案

Lua中的light userdata只是一个指针。 Lua没有理解如何读取由指针引用的对象的内容。这个数据的目的是允许一个C API调用传递一个引用到另一个API调用。

Light userdata in Lua is just a pointer. Lua has no understanding of how to read the contents of the object referenced by the pointer. The purpose of this data is to allow one C API call to pass a reference to another API call.

如果你想在Lua中使用数据,你需要push它到堆栈,在你的情况下,我会使它成一个表。如果你需要这个数据在C和Lua之间同步,那么你需要在两边编写函数来处理赋值并将值复制到其他环境。另一个选项是只有Lua中的数据,并在C中通过Lua API检索,或者在C中检索它,并在Lua中通过C API检索(你会创建)。

If you want to work with data in Lua, you need to push it onto the stack, and in your case I'd make it a table. If you need this data synchronized between C and Lua, then you'll need to write functions in both sides to handle the assignment and copy the value to the other environment. The other option is to only have the data in Lua and retrieve it via a Lua API when in C, or to have it in C and retrieve it via a C API (that you would create) when in Lua.

有关详细信息,请在Lua中编程 Luausers Wiki 各有简要说明。

For more information, Programming in Lua and the Lua-users Wiki each have brief descriptions.

这篇关于在Lua中访问Light用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆