Page 1 of 1

Factorio userdata class name api

Posted: Wed Feb 28, 2018 8:26 pm
by golfmiketango
Although the new is_player api is a nice start, it would be much appreciated if we could just ask an arbitrary userdata object what its factorio API "class" name is, i.e.,:

Code: Select all

function pretty(thing)
  if type(thing) == 'table' and thing.factorio_class then
    return '[Factorio: ' .. thing.factorio_class .. ']'
  else
    ...
  end
end

print(pretty(game))
(output: [Factorio: LuaGameScript])
Perhaps, somehow, modders can already create a big matrix of clever hacks to achieve the equivalent but why not make it easy?

This would be especially helpful for people tackling the initial learning curve of mod design like myself, as we are the ones who are most prone to get mixed up and lose the What The Heck Have I Got a Handle For Here? game.

Re: Factorio userdata class name api

Posted: Sat Mar 17, 2018 5:13 pm
by Rseding91
What's the utility of knowing the class type? You can already know what a specific class type is based off what event or method you got it from.

Re: Factorio userdata class name api

Posted: Sat Mar 17, 2018 5:31 pm
by golfmiketango
Rseding91 wrote:What's the utility of knowing the class type? You can already know what a specific class type is based off what event or method you got it from.
Well, pretty printing is a good example isn' t it? Lua is dynamically typed so subproblems of this one come up all the time. Especially in debugging, any kind of serialization we might want to do (increasingly important once rcon enters the picture), higher-level Gui framework-design, metatable operator-overload implementations, etc. Basically anywhere we might benefit from being in a dynamically-typed language, we can't, because of mysterious userdata problems.

Re: Factorio userdata class name api

Posted: Tue Sep 01, 2020 1:18 pm
by Bilka
Hey, this was implemented at some point: https://lua-api.factorio.com/latest/Com ... bject_name