lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Archibald wrote:
I want to share my identificators by both program and script. It's easy when I use #define (I simply delete #define part and copy the rest to the script). But how can I do it when I use C++ enum? Is it possible in lua to do it other way than manually giving each identificator it's number?

enum enumItem {  ITEM_SWORD, ITEM_AXE, ITEM_SHIELD };

Try this:

$ lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
>
>
> function enum(s)
>>   local i = 0
>>   for n in string.gfind(s, '[_%w]+') do
>>     _G[n] = i
>>     i = i + 1
>>   end
>> end
>
> enum'ITEM_SWORD, ITEM_AXE, ITEM_SHIELD'
>
> = ITEM_SWORD, ITEM_AXE, ITEM_SHIELD
0       1       2
>