Page 1 of 1
game.print in on_init?
Posted: Sat Dec 03, 2016 12:32 pm
by Optera
I'm a bit confused as to why game.print doesn't work in on_init.
Checking active mods with game.active_mods works fine, but posting a message about found mods wont.
Re: game.print in on_init?
Posted: Sat Dec 03, 2016 12:46 pm
by Choumiko
i think on_init() is run before any player is created.
Doing
Code: Select all
for i, p in pairs(game.players) log(i) end
in on_init() should confirm it (or not)
Re: game.print in on_init?
Posted: Sat Dec 03, 2016 3:55 pm
by Optera
Seems like players are not created in on_init.
Probably game.print can write to nil since it doesn't throw an error. I was kind of hoping it'd buffer messages and post them once players are created.
Re: game.print in on_init?
Posted: Sat Dec 03, 2016 4:57 pm
by Nexela
Optera wrote:Seems like players are not created in on_init.
There are no players in on_init in a new map. Game.print should work on an existing world that already has players during on_init because:
Probably game.print can write to nil since it doesn't throw an error. I was kind of hoping it'd buffer messages and post them once players are created.
Game.print is probably doing something equivalent to this
Code: Select all
for _, player in pairs(game.players) do
print message
end
So if there are no players the loop won't run. since no one is in the forest they don't hear the tree fall
Re: game.print in on_init?
Posted: Sat Dec 03, 2016 7:54 pm
by Rseding91
Nexela wrote:Game.print is probably doing something equivalent to this
Code: Select all
for _, player in pairs(game.players) do
print message
end
So if there are no players the loop won't run. since no one is in the forest they don't hear the tree fall
Correct. It's a simple shortcut so you don't have to do the loop yourself. It's the same with
http://lua-api.factorio.com/latest/LuaF ... orce.print
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 10:54 am
by ssilk
Hm. In theory the way to implement such a print would be to look, if any player is connected to the game. If not it remembers the to printed string in a global variable and initialize some event handler that prints this in the moment when all players are (re)connected.
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 11:08 am
by Optera
ssilk wrote:Hm. In theory the way to implement such a print would be to look, if any player is connected to the game. If not it remembers the to printed string in a global variable and initialize some event handler that prints this in the moment when all players are (re)connected.
Basically what we have to do right now is store the strings, trigger a run_once part of on_tick, print all strings.
With that effort we can just as well move most of on_init and on_load into that run_once.
There should be an event on_load/init_complete that runs once when the Bootstrap phase is complete.
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 11:11 am
by Klonan
Optera wrote:ssilk wrote:Hm. In theory the way to implement such a print would be to look, if any player is connected to the game. If not it remembers the to printed string in a global variable and initialize some event handler that prints this in the moment when all players are (re)connected.
Basically what we have to do right now is store the strings, trigger a run_once part of on_tick, print all strings.
With that effort we can just as well move most of on_init and on_load into that run_once.

It wouldn't be too hard to do:
Code: Select all
function on_init()
string = "This is what i want to print"
string = string.." - This is some more info and what not"
global.string = string
end
function on_player_created()
if global.string then
game.print(global.string)
global.string = nil
end
end
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 11:17 am
by Optera
Is on_player_created always after on_load and on_init but before the first on_tick?
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 1:37 pm
by Klonan
Optera wrote:Is on_player_created always after on_load and on_init but before the first on_tick?
On player created is after both on_init and on_load (you shouldn't need to use on_load for anything simple like this)
Im not sure, but i think it is before the first tick, the first player is created at tick 0, such that no tick has happened,
But i might be wrong
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 2:26 pm
by Optera
Klonan wrote:Optera wrote:Is on_player_created always after on_load and on_init but before the first on_tick?
On player created is after both on_init and on_load (you shouldn't need to use on_load for anything simple like this)
Im not sure, but i think it is before the first tick, the first player is created at tick 0, such that no tick has happened,
But i might be wrong
Well it won't matter for my intent of simply informing players when RailTanker was found. Using on_player_created shaves one check off from on_tick so I'll use it to display the messages in console.
If on_player_created was guaranteed to always be called before a player experiences a tick I could also move the actual check for other mods there from on_init and on_configuration_Changed.
Re: game.print in on_init?
Posted: Sun Dec 04, 2016 4:47 pm
by Nexela
Optera wrote:
If on_player_created was guaranteed to always be called before a player experiences a tick I could also move the actual check for other mods there from on_init and on_configuration_Changed.
Some things Keep in mind
on_player_created is only run once for each player (when the player is created) so adding your mod to an existing world this wouldn't run.
on_configuration_changed is run anytime any mod is added, removed, or version num changes (including "base")
on_init is run ONCE per world when it is first created. I.E in a new MP world New players joining won't get this, (same with on_config_changed I believe)
ssilk wrote:Hm. In theory the way to implement such a print would be to look, if any player is connected to the game. If not it remembers the to printed string in a global variable and initialize some event handler that prints this in the moment when all players are (re)connected.
Stdlib handles this by logging the tick. If game.tick is not available then tick is 0. then on any subsequent calls to the logger it flushes everything in the buffer if tick>last_tick.
Optera wrote:on_player_created was guaranteed to always be called before a player experiences a tick
A player that isn't created won't have a tick

Re: game.print in on_init?
Posted: Sun Dec 04, 2016 5:05 pm
by Rseding91
Nexela wrote:Optera wrote:
If on_player_created was guaranteed to always be called before a player experiences a tick I could also move the actual check for other mods there from on_init and on_configuration_Changed.
Some things Keep in mind
on_player_created is only run once for each player (when the player is created) so adding your mod to an existing world this wouldn't run.
on_configuration_changed is run anytime any mod is added, removed, or version num changes (including "base")
on_init is run ONCE per world when it is first created. I.E in a new MP world New players joining won't get this, (same with on_config_changed I believe)
ssilk wrote:Hm. In theory the way to implement such a print would be to look, if any player is connected to the game. If not it remembers the to printed string in a global variable and initialize some event handler that prints this in the moment when all players are (re)connected.
Stdlib handles this by logging the tick. If game.tick is not available then tick is 0. then on any subsequent calls to the logger it flushes everything in the buffer if tick>last_tick.
Optera wrote:on_player_created was guaranteed to always be called before a player experiences a tick
A player that isn't created won't have a tick

http://lua-api.factorio.com/latest/LuaB ... ap.on_init
http://lua-api.factorio.com/latest/Data-Lifecycle.html