Page 1 of 1
Equivalent event to on_player.joined_game in single player?
Posted: Thu Apr 11, 2019 4:02 pm
by _Attila_
I want to detect when a player starts a new playing session, so that session specific variables can be set.
on_player_joined_game seems to do the job for multi-player, but there is no event when a single player save is loaded.
As an illustrative example: How would I be able to print "Welcome back" when a save is loaded?
Re: Equivalent event to on_player.joined_game in single player?
Posted: Thu Apr 11, 2019 4:15 pm
by Bilka
How would I be able to print "Welcome back" when a save is loaded?
There is no event that allows this.
In general, "I want to detect when a player starts a new playing session, so that session specific variables can be set." is not possible because it would not be save-load stable, but save-load stability is required for multiplayer, so we do not allow Lua to break it.
Re: Equivalent event to on_player.joined_game in single player?
Posted: Thu Apr 11, 2019 4:50 pm
by _Attila_
Well, this obviously makes sense to you, but I don't get it.
A player can generate an event by clicking something. On this event I can print "Welcome back", but I can't do this in an event handler automatically when a save is loaded in single player mode?
Why not just send the player_joined_game event when a new session is started by a player - single or mp? The event would be handled the same way in both cases.
Re: Equivalent event to on_player.joined_game in single player?
Posted: Thu Apr 11, 2019 5:28 pm
by quyxkh
I maintain a global.is_multiplayer. You can check that in on_load, if it's off I set a self-cancelling on_tick, otherwise on_player_joined_game, either way I update global.is_multiplayer from game.is_multiplayer() when the selected event fires. So if a saved singleplayer map's being reloaded multiplayer for the first time, the server will do a one-off on_tick when there's no players yet, no desync there, and all players joining will see the updated value and do opjg correctly; if a saved multiplayer map's being loaded singleplayer I don't see how to do the right thing cleanly. You show up as a saved player, the only way I can figure to do it would be to check global.is_multiplayer against game.is_multiplayer() pretty much everywhere and fire the sp-join code on a mismatch. I guess a permanent 5-second nth_tick would be negligible enough overhead.
Re: Equivalent event to on_player.joined_game in single player?
Posted: Fri Apr 12, 2019 10:47 am
by _Attila_
Thanks for the info, I will try this.