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

Place to post guides, observations, things related to modding that are not mods themselves.
Plorntus
Burner Inserter
Burner Inserter
Posts: 14
Joined: Mon Nov 24, 2014 10:43 am
Contact:

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

Post 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?
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

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

Post 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
Plorntus
Burner Inserter
Burner Inserter
Posts: 14
Joined: Mon Nov 24, 2014 10:43 am
Contact:

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

Post 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).
Post Reply

Return to “Modding discussion”