A way to find the LuaObject type

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
project6
Inserter
Inserter
Posts: 22
Joined: Sat Aug 15, 2015 10:31 pm
Contact:

A way to find the LuaObject type

Post by project6 » Mon Jan 28, 2019 10:22 pm

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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 10039
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: A way to find the LuaObject type

Post by Rseding91 » Tue Jan 29, 2019 12:03 am

What's the use-case?
If you want to get ahold of me I'm almost always on Discord.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: A way to find the LuaObject type

Post by eduran » Tue Jan 29, 2019 6:31 am

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 = {
          {
 ....
 

project6
Inserter
Inserter
Posts: 22
Joined: Sat Aug 15, 2015 10:31 pm
Contact:

Re: A way to find the LuaObject type

Post by project6 » Wed Jan 30, 2019 8:23 pm

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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 10039
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: A way to find the LuaObject type

Post by Rseding91 » Wed Jan 30, 2019 11:54 pm

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".
If you want to get ahold of me I'm almost always on Discord.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: A way to find the LuaObject type

Post by eduran » Thu Jan 31, 2019 7:46 am

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.

project6
Inserter
Inserter
Posts: 22
Joined: Sat Aug 15, 2015 10:31 pm
Contact:

Re: A way to find the LuaObject type

Post by project6 » Fri Feb 01, 2019 5:26 pm

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()`

Rseding91
Factorio Staff
Factorio Staff
Posts: 10039
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: A way to find the LuaObject type

Post by Rseding91 » Fri Feb 01, 2019 8:05 pm

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.
If you want to get ahold of me I'm almost always on Discord.

curiosity
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: A way to find the LuaObject type

Post by curiosity » 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.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 3978
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: A way to find the LuaObject type

Post by Klonan » Sun Sep 22, 2019 5:13 pm

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.

Post Reply

Return to “Modding interface requests”

Who is online

Users browsing this forum: No registered users