Page 1 of 1

Why isn't this a string?

Posted: Fri Jul 28, 2017 8:54 pm
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?

Re: Why isn't this a string?

Posted: Fri Jul 28, 2017 9:04 pm
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

Re: Why isn't this a string?

Posted: Fri Jul 28, 2017 9:17 pm
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

Re: Why isn't this a string?

Posted: Fri Jul 28, 2017 10:13 pm
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.