Page 1 of 1

Any way to get the variable name of a definition value?

Posted: Fri Dec 23, 2016 11:22 am
by Plorntus
So I am currently working on a project to allow for input and output from factorio to nodejs/web services to run code.

I've been making a serializer to allow me to convert lua userdata objects into JSONable tables, Im auto generating the code from the documentation, so far I have it working however I need to be able to serialize the defines values.

Although it makes sense how factorio works to have a sort of ENUM with the defines object is there any way of converting that data back into the definition variable name without having to have a table that converts to and from and manually maintaining this?

As an example of what I'd like:

Code: Select all

local someDefinitionValue = getDefinition(game.player.controller_type)
-- someValue == "defines.controllers.character"
I suspect this isnt possible since it is just a integer value but I thought I'd ask just in case any one knows of built in way.

Here's my code if anyones interested:
https://github.com/TomCaserta/FactorIO/ ... ialize.lua

As a side note, if thats not possible, does anyone know if the order of the defines mentioned on the documentation directly relates to the int value it gives?

Re: Any way to get the variable name of a definition value?

Posted: Fri Dec 23, 2016 11:35 am
by Choumiko

Code: Select all

function getKey(value, someTable)
for k, v in pairs(someTable) do
if v == value then return k end
end
Something like that should work if you pass defines.controllers as someTable

Re: Any way to get the variable name of a definition value?

Posted: Fri Dec 23, 2016 11:44 am
by Plorntus
Choumiko wrote:

Code: Select all

function getKey(value, someTable)
for k, v in pairs(someTable) do
if v == value then return k end
end
Something like that should work if you pass defines.controllers as someTable

Ah yes, thank you, not sure why I didn't think of that, should be easy to do since the docs contain which definition type it is. I will do that then, I think I should lazilly load/create a reverse look up table to make it more efficient (if that actually would make it more efficient).