I play factorio since some months already, (I never finished a game though !).
I started to want to know more about modding, but like a lot of people saw,
there not much useful information about it.
We have:
- the overview: https://forums.factorio.com/wiki/inde ... g_overview, very (very) basic
- the only one tutorial: https://forums.factorio.com/wiki/inde ... g_Tutorial, a lot more useful, although it's not working with last versions. But explanations are valid.
- the others pages on the wiki available from https://forums.factorio.com/wiki/inde ... le=Modding. But we don't learn how to write a mod.
So, let's try to do it !
There's a lot of information to give, I don't know how to order them (and I don't know a lot yet).
First, the already existing tutorial. Here is a little summarize of what we learn about
- mod setup: the directories and files to create
- the items: objects we can create and store in inventory
- the recipes: what do we need to create a given object
- the entities: what do we get once we use an item
- the technologies: which tech exists, which recipes it allows, ...
- localization: how to translate your mod
- lua code: how to add a new behavior to factorio
- the 8th chapter has to be read too, it shows the real structure of a mod (additional directories and files)
- the last chapter shows some idea to expand the example mod (and a weird error if the mod is a directory rather than a zip file ???)
My turn
First an example to show the variables initialization mechanism.
- How to initialize your data ?
Code: Select all
-- import 'defines.lua' file.
-- It's located (on linux) in factorio/data/core/lualib and contains some
-- definitions. The most often used are the events.
require "defines"
-- some variables which will be modify later
glob.var = ""
var = ""
-- boolean used later to print the variables
local do_print = true
-- init is the 1st event called when you start a new game
game.oninit
(function()
-- glob.var and var are set to the concatenation of their actual value "" and "init, "
glob.var = glob.var .. "init, "
var = var .. "init, "
end)
-- playercreated is an event called when you start a new game. It's called
-- after oninit
game.onevent
(defines.events.onplayercreated,
function(event)
glob.var = glob.var .. "playercreated, "
var = var .. "playercreated, "
end)
-- load is the 1st event called when you load a game
game.onload
(function()
glob.var = glob.var .. "load, "
var = var .. "load, "
end)
-- tick is an event called with each tick when a game is running. 1 tick is
-- 1/60 second.
-- Here we print all our variable. The first tick occurs after the other
-- events we used here (either init or playercreated or load).
game.onevent
(defines.events.ontick,
function(event)
-- do_print is set to true at the beginning of the script
if do_print then
dbg("glob.var = " .. glob.var)
dbg("var = " .. var)
-- do_print is set to false, so we won't print when other ticks occured
do_print = false
end
end)
-- my little function to print a message on the screen.
-- WARNING: since it uses game.player, it CAN'T be called by oninit, which
-- occures before onplayercreated
function dbg(msg)
game.player.print(msg)
end
When we start a new game, we get
Code: Select all
glob.var = init, playercreated,
var = init, playercreated,
The game started, so it reads our file.
The 2 variables are initialized to empty strings.
Code: Select all
glob.var = ""
var = ""
Code: Select all
glob.var = glob.var .. "playercreated, "
var = var .. "playercreated, "
Code: Select all
glob.var = glob.var .. "playercreated, "
var = var .. "playercreated, "
Now, we save the game and load it.
This time we get
Code: Select all
glob.var = init, playercreated, load,
var = load
Again, the variables are initialized to empty strings (in fact, glob isn't initialized, see later)
Code: Select all
glob.var = ""
var = ""
The event 'load' is called
Code: Select all
glob.var = glob.var .. "load, "
var = var .. "load, "
'glob' is a special variable in factorio, it has 2 properties:
- it's value remains when we save a game.
- when you load a game, every affection outside a function / event is ignored,
so it's not reset and keep it's previous value. 'glob.var' was set to
"init, playercreated, ", so now it's "init, playercreated, load, ".
'var' is a normal variable, so it's reset to an empty string, then set to
"load, ".
I'm done for now. I hope it's help to understand how to set and keep your data.
I'll add some others parts, like "gui", and
"multiplayer" (I need more informations for this one)
Should I create a new topic for each part and keep this one as a table of contents?