Why isn't this a string?

Place to get help with not working mods / modding interface.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Why isn't this a string?

Post by DRY411S »

This is more about me not understanding Lua data structures I think, but If I serpent_block dump a recipe in the data stage I get something like this:

Code: Select all

  {
    category = "recycling-1",
    enabled = false,
    energy_required = 1,
    group = "dry411srev-combat",
    hidden = false,
    icon = "__base__/graphics/icons/firearm-magazine.png",
    ingredients = {
      {
        "firearm-magazine",
        1
      }
    },
    name = "dry411srev-firearm-magazine",
    order = "a[basic-clips]-a[firearm-magazine]",
    results = {
      {
        "iron-plate",
        4
      }
    },
    subgroup = "dry411srev-ammo",
    type = "recipe"
  },
In an event handler in control.lua I have some code like this:

Code: Select all

            local setting = string.gsub(force.recipes["dry411srev-firearm-magazine"].subgroup,rec_prefix,"ZRecycling")
In the console (but not the log) this gives an error like:

Code: Select all

bad argument #1 to 'gsub' (string expected, got table) ....
1. Why is the subgroup property a table and not just a string?
2. How can I get the value of the string from the table that the console says that subgroup is in?
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Why isn't this a string?

Post by DaveMcW »

How Factorio works:

a. Use data.lua to build data.raw.
b. Use data.raw to build C++ objects.
c. Delete data.raw.
d. Use C++ objects to build lua objects. This process is documented here: http://lua-api.factorio.com/latest/
e. Use lua objects in control.lua.

1. tldr; data.lua and control.lua are totally different, and follow different rules.
2. viewtopic.php?f=25&t=51264
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Why isn't this a string?

Post by DRY411S »

Thank you. So for people who remember to use search (unlike me :facepalm:) my code should be:

Code: Select all

local setting = string.gsub(force.recipes["dry411srev-firearm-magazine"].subgroup.name,rec_prefix,"ZRecycling")
i.e. subgroup property is a table of type LuaGroup. The string from the .subgroup property in the recipe (in data stage) is now stored in .subgroup.name
Rseding91
Factorio Staff
Factorio Staff
Posts: 16016
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Why isn't this a string?

Post by Rseding91 »

DRY411S wrote:i.e. subgroup property is a table of type LuaGroup. The string from the .subgroup property in the recipe (in data stage) is now stored in .subgroup.name
Correct.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”