Page 1 of 1

A way to find the LuaObject type

Posted: Mon Jan 28, 2019 10:22 pm
by project6
Looking at the output from `help()`, there seems to be identification of the object type. Would it be possible to make these accessible?
Being able to see the object type would be very helpful with debugging.

is_player() is cool, but also oddly specific considering there are 76 LuaObject types in 0.16 and an extra 4 added in the 0.17 API.

Code: Select all

2721.472 Script log(game.player.help()):1: Help for LuaPlayer:
Thanks.

Re: A way to find the LuaObject type

Posted: Tue Jan 29, 2019 12:03 am
by Rseding91
What's the use-case?

Re: A way to find the LuaObject type

Posted: Tue Jan 29, 2019 6:31 am
by eduran
As a workaround, you can read the type from the help string:

Code: Select all

obj_type = match(obj.help(), "Help for%s([^:]*)")
Issue is that it fails with an error if you use it on an object without .help() method. Luckily, those are rare.
Rseding91 wrote:
Tue Jan 29, 2019 12:03 am
What's the use-case?

For me, pretty-printing debug output. For example:

Code: Select all

[180820] = {
    entity = {
      LuaEntity{
        backer_name = "DEP mall, 8x",
        name = "logistic-train-stop",
        type = "train-stop"
      }
parkedTrain = {
      LuaTrain{
        id = 648,
        carriages = {
          {
 ....
 

Re: A way to find the LuaObject type

Posted: Wed Jan 30, 2019 8:23 pm
by project6
The use-case is debugging. You have some kind of LuaObject arriving somewhere and it's not what you expect. Knowing the type would help with knowing where it came from/where things are being contaminated.

Currently we use:

Code: Select all

function Debug.object_type(object)
    local obj_type = type(object)
    if obj_type == 'table' and object.isluaobject then
        return match(object.help(),'Lua%a+')
    end
    return obj_type
end
It's functional, but it's also a really janky way of doing things and if, on the factorio end, exposing the type is as easy as help() makes it seem, it would be helpful. Especially for newer mod devs who might not think to parse something like help text.

Re: A way to find the LuaObject type

Posted: Wed Jan 30, 2019 11:54 pm
by Rseding91
project6 wrote:
Wed Jan 30, 2019 8:23 pm
You have some kind of LuaObject arriving somewhere and it's not what you expect.
I don't mind adding it for debug reasons but this statement concerns me: you should *always* know what objects you're passing around are. If you've written your code such that you've lost that information then you're written incorrect code.

The only time I could see it being valid that you don't know the type of something is when you get it through a remote call.

I don't want to encourage poor quality code by making it super easy to make a big if-else table of "pass in any object, if/else to find the type, then do some logic" and have that just be "how the mod works". That's sloppy code and shouldn't be written except for debug purposes. Even then it's dubious when it would be useful over just "neat".

Re: A way to find the LuaObject type

Posted: Thu Jan 31, 2019 7:46 am
by eduran
My custom log function takes any kind of argument(s), turns them into a human-readable string and prints it to file. While I do know the type of the arguments when the logger function is called, I don't want to have to pass that information. Consider:

Code: Select all

out.info("ctrl.gui_event_handler", "Gui event triggered. Event:", event)
which generates this output:

Code: Select all

[LTNC][INFO]<ctrl.gui_event_handler>Gui event triggered. Event: {
  name = 1,
  tick = 4637592,
  player_index = 1,
  element = {
    LuaGuiElement{
      name = "ltnc_depot_tab009_Depot",
      index = 10601,
      parent = "ltnc_depot_tab004",
      children = {}
    }
  },
  button = 2,
  alt = false,
  control = false,
  shift = false
}
Not having to care about the types of objects inside the event table is extremely convenient in this case.

That being said, I fully agree with your point. And for converting objects to strings it works well enough as-is.

Re: A way to find the LuaObject type

Posted: Fri Feb 01, 2019 5:26 pm
by project6
Rseding91 wrote:
Wed Jan 30, 2019 11:54 pm
I don't mind adding it for debug reasons but this statement concerns me: you should *always* know what objects you're passing around are. If you've written your code such that you've lost that information then you're written incorrect code.
Maybe I'm confused. Isn't that what debugging is for? Tracking down incorrect code? I'm saying the use-case is to find out where the mistake is, and you're telling me that I shouldn't make mistakes.

In addition, if someone wanted to make a mod in the way you described (a big if/else statement) they can already do that in the manner described above: parsing the string generated by `help()`

Re: A way to find the LuaObject type

Posted: Fri Feb 01, 2019 8:05 pm
by Rseding91
Just because someone can do something doesn't mean they will. Making an official method for reading the object type is a very clear "you can do it that way if you want". Parsing a help string is and will always feel like a hack to anyone who wanted to do it.

Re: A way to find the LuaObject type

Posted: Sun Sep 22, 2019 11:41 am
by curiosity
Here is a better reason for you: it does not adhere to Lua's paradigm. So either add a way to get object's type or implement strict typing in Factorio's Lua.

Re: A way to find the LuaObject type

Posted: Sun Sep 22, 2019 5:13 pm
by Klonan
curiosity wrote:
Sun Sep 22, 2019 11:41 am
Here is a better reason for you: it does not adhere to Lua's paradigm. So either add a way to get object's type or implement strict typing in Factorio's Lua.
Lots of Factorio Lua breaks Lua convention. It is a tool and we will use it as best we see fit.