开发者

Embedding Lua functions as member variables in Java

开发者 https://www.devze.com 2022-12-29 02:31 出处:网络
Although the program I\'m working on is in Java, answering this from a C perspective is also fine, considering that most of this is either language-agnostic, or happens on the Lua side of things.

Although the program I'm working on is in Java, answering this from a C perspective is also fine, considering that most of this is either language-agnostic, or happens on the Lua side of things.

In the outline I have for the architecture of a game I'm programming, individual types of game objects within a particular class (eg: creatures, items, spells, etc.) are loaded from a data file. Most of their properties are simple data types, but I'd like a few of these members to actually contain simple scripts that define, for example, what an item does when it's used. The scripts will be extremely simple, since all fundamental game actions will be exposed through an API from Java. The Lua is simply responsible for stringing a couple of these basic functions together, and setting arguments.

The question is largely about the best way to store a reference to a specific Lua function as a me开发者_运维问答mber of a Java class.

I understand that if I store the Lua code as a string and call lua_dostring, Lua will compile the code fresh every time it's called. So the function needs to be defined somehow, and a reference to this specific function wrapped in a Java function object.

One possibility that I've considered is, during the data loading process, when the loader encounters a script definition in a data file, it extracts this string, decorates the function name using the associated object's unique ID, calls lua_dostring on the string containing a full function definition, and then wraps the generated function name in a Java function object. A function declared in script run with lua_dostring should still be added to the global function table, correct?

I'm just wondering if there's a better way of going about this. I admit that my knowledge of Lua at this point is rather superficial and theoretical, so it's possible that I'm overlooking something obvious.


just do k = luaL_ref(L, LUA_REGISTRYINDEX);.

call it with the Lua value you want to refer on the top of the stack and it will return an integer k. Store this integer, and when you need the Lua value just do a lua_rawget(L, LUA_REGISTRYINDEX, k); to push the value again to the top of the stack. When you want to release the object (i.e. at object's destruction), call luaL_unref(L, k)

It works with any kind of Lua value, including functions (or closures, in fact) and coroutines.

0

精彩评论

暂无评论...
验证码 换一张
取 消