[Guide] Modding and Scripting changes in 0.3.0

Place to post guides, observations, things related to modding that are not mods themselves.
slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

[Guide] Modding and Scripting changes in 0.3.0

Post by slpwnd »

Modding
There is an introduction into the modding in 0.3.0 at https://forums.factorio.com/wiki/inde ... g_overview. Basically we redone a lot of things to make mods more self-sustained and stackable. The pre 0.3.0 mods are not compatible with the >= 0.3.0 release! Also changing the mods to match the 0.3.0 specs is not that bad (we have migrated all the data in the game to mee the new specs).

Scripting
* The event registering mechanism has been changed in the 0.3.0 and it is not compatible with the old mechanism. You can read the details at the https://forums.factorio.com/wiki/index.php/Lua/Events
* We have introduced a new mechanism for communications between multiple scripts from different mods. This is called Script interfaces and you can read more about it on the wiki: https://forums.factorio.com/wiki/inde ... interfaces

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

:P Awesome!!!
I thought you were going to add something like NBT in minecraft, to be able to change the data of some specific machines, not all the machines in the game, that would be even moar awzum.

Hope to see more lua documentation :D im so excited.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

Im ficolas from the future and I have a message.

Its day 269 and im still updating the mod.
There is only a can of water, and there is no food left, I think I will die without updating it...
There are so many ":" to change...

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by slpwnd »

This is the script I have used to migrate the JSON files from base to Lua.

Code: Select all

-- Migration of the json prototype-definition files into lua data files
-- Requires lua packages serpent and luajson to be on the package.path
-- Note: use luarocks for easy lua package installation
--
-- Usage:
-- lua migratejsons.lua json-paths
-- json-paths is a list of paths to json data files to be migrated

local serpent = require("serpent")
local json = require("json")

function loadpaths(source)
  -- first argument should be a file with paths to migrate
  local lines = {}
  for line in io.lines(source) do
    lines[#lines + 1] = line
  end
  return lines
end


function migrateone(path)
  local luadata = json.decode(io.open(path, "r"):read("*all"))
  local output = io.open(path:gsub(".json$", ".lua"), "w")
  local dump = serpent.block(luadata, {comment=false})
  output:write("data:extend(\n" .. dump .. "\n)\n")
end

-- main

lines = loadpaths(arg[1])
for _, line in ipairs(lines) do
  migrateone(line)
end
It doesn't change the dashes to underscores on the left sides.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

thanx, but I alredy did something, and I think I broke something, but I dont know what, can you help?
http://rapidshare.com/files/1129952588/f-mod.rar

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by slpwnd »

ficolas wrote: thanx, but I alredy did something, and I think I broke something, but I dont know what, can you help?
http://rapidshare.com/files/1129952588/f-mod.rar
What exactly do you mean ?

I did a very quick check and the mod is not loaded properly. The name of the mod in the info.json file must be the same as the name of the directory. I will add some error reporting for that. If you change the name in the info.json file then there are some more errors to cope with.

Also there have been some scripting changes - I will write an update about those on the wiki soon.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

error reporting doesnt work fine in this case:

Code: Select all

data.raw.entity["creeper"].loot=
      {
        item = "small-alien-artifact",
        probability = 1,
        count-min= 1,
        count-max= 5
      }
it asks for a "}" more, to close the one at line 3, but there is 1 "{" and 1 "}"... what is the problem there?

Also I cant see the error here:

Code: Select all

data:extend({
  {
    type= "container",
    name= "underground-mining-drill",
    flags= {"placeable-neutral", "player-creation"},
    collision-box= {{-1.4, -1.4}, {1.4, 1.4}},
    selection-box= {{-1.5, -1.5}, {1.5, 1.5}},
    minable= { mining-time= 1, result= "underground-mining-drill" },
    "max-health= 200,
    "inventory-size= 4,
    "picture=
    {
      filename= "__f-mod__/resources/entity/underground-mining-drill/underground-mining-drill.png",
      width= 96,
      height= 96,
      priority= "extra-high"
  }
  },
  
  {
    type= "assembling-machine",
    name= "alien-assembly-machine",
    flags= {"placeable-neutral", "player-creation"},
    collision-box= {{-1.4, -1.4}, {1.4, 1.4}},
    selection-box= {{-1.5, -1.5}, {1.5, 1.5}},
    minable= { hardness= 0.2, mining-time= 0.5, result= "alien-assembly-machine" },
    crafting-categories= { "alien-crafting" },
    max-health= 150,
    drawing-position= {0.7, 0.12},
    ingredient-count= 20,
    energy-usage-per-tick= 2.5,
    effectivity= 1.5,
    energy-source=
    {
      type= "electric",
      input-priority= "secondary"
    },
    animation=
    {
      filename= "__f-mod__/resources/entity/assembling-machine-2/assembling-machine-2.png",
      priority= "medium",
      frame-width= 141,
      frame-height= 120,
      line-length= 6,
      frame-count= 32
    }
  },
  {
    type= "container",
    name= "underground-scanner",
    flags= {"placeable-neutral", "player-creation"},
    collision-box= {{-1.4, -1.4}, {1.4, 1.4}},
    selection-box= {{-1.5, -1.5}, {1.5, 1.5}},
    minable= { mining-time= 1, result= "underground-scanner" },
    max-health= 200,
    inventory-size= 1,
    picture=
    {
      filename= "__f-mod__/resources/entity/underground-mining-drill/underground-scanner.png",
      width= 96,
      height= 96,
      priority= "extra-high"
  }
  },
  {
    type= "player-port",
    name= "Spawn",
    flags= {"placeable-neutral", "player-creation"},
    collision-box= {{-0.9, -0.9}, {0.9, 0.9}},
    selection-box= {{-1, -1}, {1, 1}},
    minable= { mining-time= 1, "result= "Spawn" },
    max-health= 50,
    animation=
    {
      filename= "__f-mod__/resources/entity/Spawn/file.png",
      frame-width= 64,
      frame-height= 64,
      line-length= 6,
      frame-count= 31,
      priority= "high"
    }
   }
}
)
line 6, expected } to close { at line 2 near =

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by slpwnd »

This is how the definition should look like:

Code: Select all

data.raw["unit-spawner"]["creeper-spawner"].loot =
   {
      {
        item = "small-alien-artifact",
        probability = 1,
        count_min= 1,
        count_max= 5
      }
    }
There were 2 errors:
* There path to the loot element didn't make sense so it came out as nil indexing
* The dash ("-") can't be used in identifiers in Lua becose it is parsed as a minus sign. Therefore we have changed all the left hand identifiers
in the from the JSON definitions to use the underscores instead.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

slpwnd wrote:This is how the definition should look like:

Code: Select all

data.raw["unit-spawner"]["creeper-spawner"].loot =
   {
      {
        item = "small-alien-artifact",
        probability = 1,
        count_min= 1,
        count_max= 5
      }
    }
I didnt want to change the creeper spawner´s loot, I wanted to change creeper's loot.
slpwnd wrote:* The dash ("-") can't be used in identifiers in Lua becose it is parsed as a minus sign. Therefore we have changed all the left hand identifiers
in the from the JSON definitions to use the underscores instead
Omg moar work xD
Last edited by ficolas on Sat Mar 30, 2013 10:33 pm, edited 1 time in total.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by FreeER »

ficolas wrote:Omg moar work xD
just imagine how much they've put into this update:
They had to figure out the best way to set it up
then they had to convert the base 'mod'
and test it to make sure everything worked
Then they have to write up a wiki for everything
AND THEN they have to put up with everyone who gets confused by the update :lol:
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

Yeah, they are all awesome :D!

also, I need to change the name of all items used in recipes to use underscores?:

Code: Select all

    ingredients= { gold-ore= 1 },
because gold-ore is a variable and it interpretates it as gold - ore, so do I need to change the name to gold_ore?

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by slpwnd »

We changed the names of the properties. Not the names of the game elements. So we still have "iron-plates", "copper-ore", etc. Therefore there should be the "gold-ore". In your case

Code: Select all

ingredients = { ["gold-ore"] = 1},
This was not an easy decision because it creates some inconsistencies in the naming, however it saves a lot of typing for the property names.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding in 0.3.0 introduction

Post by ficolas »

Also what about this error:

Content Title

Code: Select all

Cannot load lua script
G:/F\factorio_0.3.0.3945\mods\F-mod\data.lua. Error:
...\Factorio_0.3.0.3945\data\core\lualib\dataloader.lua:12: table index is nil

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by slpwnd »

FreeER wrote: just imagine how much they've put into this update:
They had to figure out the best way to set it up
then they had to convert the base 'mod'
and test it to make sure everything worked
Then they have to write up a wiki for everything
AND THEN they have to put up with everyone who gets confused by the update
It was a lot of work :) However we wanted to make the needed radical changes to the mod suppost as soon as possible when there are still not that many mods. Also the changes we introduced should give modders way more powers.
ficolas wrote: Also what about this error:

Content Title
CODE: SELECT ALL
Cannot load lua script
G:/F\factorio_0.3.0.3945\mods\F-mod\data.lua. Error:
...\Factorio_0.3.0.3945\data\core\lualib\dataloader.lua:12: table index is nil
Not sure where this is exactly raised. I had a look into the dataloader (you can see that one too :)) and saw the following on line 12:

Code: Select all

 ...
 self.raw[e.type] = t
 ...
So based on the error name I would guess the problem is that the prototype object you are defining doesn't have a type field. The type and name are mandatory since 0.3.0 for every prototype object (should have mentioned that one earlier I guess :)).

btw. I have updated the OP with links to some scripting documentation I put up on the wiki.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by ficolas »

but this G:/F\factorio_0.3.0.3945\mods\F-mod\data.lua is the file where the files are required, do that mean that there is one prototipe that haves no type?

Edit: Recipes now have a type, that was the problem. thx.
Last edited by ficolas on Sat Mar 30, 2013 10:43 pm, edited 1 time in total.

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by kovarex »

ficolas wrote:but this G:/F\factorio_0.3.0.3945\mods\F-mod\data.lua is the file where the files are required, do that mean that there is one prototipe that haves no type?
My guess is, that you missed type and name property in recipe specification.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by ficolas »

kovarex wrote:
ficolas wrote:but this G:/F\factorio_0.3.0.3945\mods\F-mod\data.lua is the file where the files are required, do that mean that there is one prototipe that haves no type?
My guess is, that you missed type and name property in recipe specification.
yup, I didnt know that recipes now have type, I knew they had name, but didnt know anything about type.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by ficolas »

Code: Select all

data:extend({
  {
    type = "recipe",
    name = "gold-furnace",
    result= "gold-plate",
    ingredients= { ["gold-ore"]= 1 }, --this line.
	energy_required= 500,
	category= "smelting"
  },
  {
    type = "recipe",
    name = "silver-furnace",
    result= "silver-plate",
    ingredients= { ["silver-ore"]= 1 },
	energy_required= 500,
	category= "smelting"
  }
})

line 6.
} expected near =

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by slpwnd »

Actually the ingredients format has changed now it is a list of pairs instead of map (you can see this for instance in the base/prototypes/recipe/recipe.lua) so in your case it should be:

Code: Select all

...
ingredients = {
  { "gold-ore", 1}
}
  

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: [Guide] Modding and Scripting changes in 0.3.0

Post by ficolas »

Code: Select all

data:extend({
  {
    type = "recipe",
    name = "gold-furnace",
    result= "gold-plate",
    ingredients = {{ "gold-ore", 1}},
	energy_required= 500,
	category= "smelting"
  {
    type = "recipe",
    name = "silver-furnace",
    result= "silver-plate",
	ingredients = {{ "silver-ore", 1}},
	energy_required= 500,
	category= "smelting"
  }
})
same error, line 8...
why...

Post Reply

Return to “Modding discussion”