game.print in on_init?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

game.print in on_init?

Post 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.

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: game.print in on_init?

Post 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)

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: game.print in on_init?

Post 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.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: game.print in on_init?

Post 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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: game.print in on_init?

Post 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
If you want to get ahold of me I'm almost always on Discord.

User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12888
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: game.print in on_init?

Post 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.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: game.print in on_init?

Post 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. :roll:

There should be an event on_load/init_complete that runs once when the Bootstrap phase is complete.
Last edited by Optera on Sun Dec 04, 2016 11:12 am, edited 1 time in total.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: game.print in on_init?

Post 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. :roll:
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

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: game.print in on_init?

Post by Optera »

Is on_player_created always after on_load and on_init but before the first on_tick?

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: game.print in on_init?

Post 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

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: game.print in on_init?

Post 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.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: game.print in on_init?

Post 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 :)

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: game.print in on_init?

Post 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
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”