Page 1 of 1

Get prototype name from LuaEntity

Posted: Thu Dec 12, 2024 6:17 pm
by alu
Hi,

In my mod I have access to a LuaEntity from an event in the control stage, which for this example let's say is a spider-vehicle. I'm trying to somehow access all the information about this entity, which can be found in the the runtime-api.json file in the docs.

I've written a small script to extract all the methods and attributes from each entity and sort them into a lua-file which I can then use to look up all attributes and methods of an entity in my mod. The problem is that in the JSON file the data is related to "subclasses", for instance SpiderVehicle (example below). The LuaEntity provided is just that, a LuaEntity. So entity.object_name is LuaEntity, while entity.name is spider-vehicle. So how do I connect a "class name"/SpiderVehicle with a "dash delimited name"/spider-vehicle?

I've found that the prototype in prototype-api.json has a "typename" field which contains a dash delimited name, but I've got no prototype name available (I think?) for my LuaEntity. I guess I could do some converting to camelcase and add Prototype as a suffix but that seems like a solution that could break easily, not sure how ridgid that would be. Any suggestions?


Example of attribute autopilot_destinations for SpiderVehicle in runtime-api.json:

Code: Select all

{
  "name": "autopilot_destinations",
  "order": 161,
  "description": "The queued destination positions of spidertron's autopilot.",
  "subclasses": [
    "SpiderVehicle"
  ],
  "read_type": {
    "complex_type": "array",
    "value": "MapPosition"
  },
  "optional": false
},

Re: Get prototype name from LuaEntity

Posted: Tue Dec 17, 2024 8:53 am
by Natha

Re: Get prototype name from LuaEntity

Posted: Thu Dec 19, 2024 4:23 pm
by alu
Natha wrote: Tue Dec 17, 2024 8:53 am Is https://lua-api.factorio.com/latest/cla ... #prototype what you're looking for?
I though so at first, but it doesn't give me what I want.
Example output, coming from the last_entity field of the event on_selected_entity_changed:

Code: Select all

entity.prototype.name: spidertron
entity.prototype.object_name: LuaEntityPrototype
entity.prototype.type: spider-vehicle
I'd like to somehow get the "subclass" of this entity as stated in the API docs:

Snippet from runtime-api.json:

Code: Select all

{
      "name": "LuaEntity",
      "order": 37,
      "description": "The primary interface for interacting with entities through the Lua API. Entities are everything that exists on the map except for tiles (see [LuaTile](runtime:LuaTile)).\n\nMost functions on LuaEntity also work when the entity is contained in a ghost.",
      "parent": "LuaControl",
      "abstract": false,
      "methods": [
        {
          "name": "add_autopilot_destination",
          "order": 108,
          "description": "Adds the given position to this spidertron's autopilot's queue of destinations.",
          "subclasses": [
            "SpiderVehicle"
          ],
          "parameters"..
          "format": ...
          "return_values": ..
        }
        ...
The above method add_autopilot_destination is only valid if the LuaEntity is/has a subclass of SpiderVehicle. Can I somehow translate between camel-case and dashed-name? For a SpiderVehicle/spider-vehicle you can do string manipulation to get a connection between the two, but what about other subclasses like RollingStock?

Re: Get prototype name from LuaEntity

Posted: Sat Dec 21, 2024 9:34 pm
by Natha
Still don't know what you want to achieve.
If you have the prototype name, you can look in prototype-api.json for a class that has this value in typename The name of this class, trimmed by the "Prototype" end, is your entity subclass. (Needs a check for all subclasses menioned in runtime-api.json if they have their +"Prototype" prototype pendant but I'd say so)