将大量常量导出到Lua状态(Exporting a large

编程入门 行业动态 更新时间:2024-10-13 22:18:55
将大量常量导出到Lua状态(Exporting a large-ish list of constants to Lua state)

使用C(99),我有一个从Lua脚本加载Lua表的游戏。

技术方面没有问题,一切按预期工作。 但是,C内核使用许多常量来定义项类型等。 该脚本实际上提供了要加载的武器数据,并且为了清楚起见应该使用相同的常量名称/值。 在C中,常量是诸如#define ITEM_TYPE_EDIBLE 2之类的东西。

所以,我的问题是:如何从C中轻松地将长整数(~300定义)的整数列表导出到Lua? 不止一个脚本应该使用该列表,虽然内存中的大小并不是真正的问题,但我主要是为了方便。

我已经想到了这些可能的解决方案,但不确定从长远来看哪个会更好:

A)在处理所需脚本的Lua状态下手动创建嵌套表。 B)手动创建包含常量的lua脚本,并在每个使用它们的脚本中导入它。

那么,您认为最方便的方法是什么? 因为,方便更新和在Lua方面使用,所以mods可以很容易地编写。 项目加载都是在启动时完成的,所以如果它不是超快的方法并不重要(例如,它不会滞后于动作,只是启动时间)

哦,我正在使用Lua 5.1,但如果它让我的任务变得更容易,那就不要反对使用5.2了。 如果可能的话,也不反对以编程方式更新列表。

Using C(99), I have a game that loads a Lua table from a Lua script.

No problems in the technical aspect, all works as intended. However, the C core uses a number of constants to define item types and such. The script actually provides weapon data to be loaded, and should use the same constants names/values for clarity. The constants are, in C, something such as #define ITEM_TYPE_EDIBLE 2 .

So, my question is: How can I easily export a long-ish (~300 defines) list of integers to Lua from C? More than one script should be using that list, and while the size in memory is not really a concern, I am mostly looking for convenience.

I've thought of these possible solutions, but not sure which one would be better on the long run:

A) Manually create a nested table in the Lua state processing the desired script. B) Manually create a lua script containing the constants, and import it in every script making use of them.

So, what do you think is the most convenient way to do this? As in, convenient to update and to use in the Lua side of things, so mods can be written easily. Item loading is all to be done at startup, so it doesn't matter if it's not a super-fast approach (as in, it won't lag the action, just the boot time)

Oh, I am using Lua 5.1, but not against using 5.2 if it makes things any easier on my task. Also not against updating the list programmatically if possible.

最满意答案

A)在处理所需脚本的Lua状态下手动创建嵌套表。 B)手动创建包含常量的lua脚本,并在每个使用它们的脚本中导入它。

你忘了:

C)手动创建一个包含常量的Lua脚本,然后当我创建我的lua_State时,立即导入它。 这样,使用它们的每个脚本都可以假定它已被导入。

实际上,我通常会将此脚本放在可执行文件本身或可执行文件加载的特殊文件中。

A) Manually create a nested table in the Lua state processing the desired script. B) Manually create a lua script containing the constants, and import it in every script making use of them.

You forgot:

C) Manually create a Lua script containing the constants, then when I create my lua_State, import it right then. That way, every script that uses them can assume it has been imported.

Indeed, I would generally put this script either in the executable itself or in a special file loaded by the executable.

更多推荐

本文发布于:2023-07-30 18:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   状态   导出到   Lua   Exporting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!