使用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.
更多推荐
发布评论