Code: Select all
function events.on_load(event)
trigger_on_load = true
end
function events.on_tick()
if trigger_on_load then
init.initialize()
trigger_on_load = nil
end
end
function init.initialize()
step_check_config_changed()
end
Code: Select all
function events.on_load(event)
trigger_on_load = true
end
function events.on_tick()
if trigger_on_load then
init.initialize()
trigger_on_load = nil
end
end
function init.initialize()
step_check_config_changed()
end
Code: Select all
the dirtiest way would be to register and un-register a custom event in the tick handler (register in on_load and un_register after the tick handler has run) -- I do this for SP testing so I don't have to increment version and change folder names everywhere
Code: Select all
local my_event = script.generate_event_name()
script.on_load(function ()
script.on_event(my_event, function()
update code here
script.on_event(my_event, nil) --nil the event to remove it
end)
end)
script.on_event(defines.events.on_tick, function(event)
game.raise_event(my_event)
blah blah blah rest of your tick code here.
)
Would it be worth the devs doing a few youtube vids or something explaining how multiplayer works and the differences between MP and SP, and explaining how the desych happens and how to avoid it?Rseding91 wrote:If you do any of the things listed here it will desync when used in MP and or break replays in single player.
Rseding91 wrote:If you do any of the things listed here it will desync when used in MP and or break replays in single player.
Just FYI.
Guess I can change that to being 100% sure it doesn't work in mp. But as I said it was only something I did for single player testing/devolping where desyncs and replay don't matter.Nexela wrote: Also not 100% sure how this would work in MP
Where did that came from?AntiElite wrote:However the on_load cannot edit global variables(cause they are not loaded yet).
If I use global here, it tells me its not possible in on_loadAdil wrote:Where did that came from?AntiElite wrote:However the on_load cannot edit global variables(cause they are not loaded yet).
Modifying "global." is the sole purpose of on_load, It's game objects that are only a dead weight at this stage.
The desync comes from "trigger_on_load = true ". This line defines local variable, when second player connects, onload is fired only for him, so other players don't have the same variable, and on next tick it is polled for its value.
Code: Select all
function events.on_load(event)
global.triggerinitialize = true
end
I just want to run a function the tick after loading a game ... not after a player joining ... not before the entitys are loaded ... and without any desync - I've never thought this is gonna be so difficultError while running mod-UPS++::on_load().
Detected modifications to the 'global' table:
CRC before: 1095740016
CRC after: 2444180806
So what can i do instead to reach my goal Rseding91?Rseding91 wrote:If you do any of the things listed here it will desync when used in MP and or break replays in single player.
Just FYI.
Well, indeed global cannot be changed during load, the desync explanation from above post is still correct though.AntiElite wrote: If I use global here, it tells me its not possible in on_load
Well, for player1 the game is loaded on the tick1 and your function is run on tick1+1, for player2 its the tick2 and tick2+2, how do you expect it be easy not to desync if on each tick script calculated by each player's machine is supposed to arrive in the same state?AntiElite wrote:I just want to run a function the tick after loading a game ... not after a player joining ... not before the entitys are loaded ... and without any desync - I've never thought this is gonna be so difficult
Code: Select all
--this is control lua
--these are config variables
local var1=2
var2=3
--this initializes a bit more of config variables
require "config.lua"
--here go functions that use the above variables
...
Code: Select all
function events.on_player_joined_game(event)
local player_online_count = 0
for key, player in pairs(game.players) do
if player.connected then
player_online_count = player_online_count + 1
if player_online_count >=2 then break end
end
end
if player_online_count < 2 then global.trigger.initialize = true end --will trigger initialization in the mainloop only when first player joins
end
How exactly do you run this code? Is it in the control.lua of your mod?AntiElite wrote: ... So i got a workaround here to make it update on the first tick instead.
Code: Select all
function events.on_load(event) trigger_on_load = true end function events.on_tick() if trigger_on_load then init.initialize() trigger_on_load = nil end end function init.initialize() step_check_config_changed() end
Code: Select all
events={}
--previous snip pasted here
--then register the handlers in the following manner
game.on_event(defines.events.on_tick, events.on_tick() )
Yes this hack will not work in MP and shouldn't be used in a released version. About the only thing this is good for is single player development resetting recipes/code etc instead of always starting new worlds or increment version.Adil wrote:However, I'd like to recommend once again: rethink your ways if you're in need of this hack.Code: Select all
events={} --previous snip pasted here --then register the handlers in the following manner game.on_event(defines.events.on_tick, events.on_tick() )
Code: Select all
events={}
--previous snip pasted here
function events.on_load(event)
trigger_on_load = true
end
function events.on_tick()
if trigger_on_load then
init.initialize()
trigger_on_load = nil
end
end
function init.initialize()
local player = game.players[event.player_index]
player.print("loaded game")
end
--then register the handlers in the following manner
script.on_event(defines.events.on_tick, events.on_tick() )
script.on_event(defines.events.on_load, events.on_load() )
Code: Select all
-- use local to make it clear if something is local variable or function
local trigger_on_load = false
local init = {} -- you forgot to define this
local events = {}
function events.on_load()
trigger_on_load = true
end
function events.on_tick()
if trigger_on_load then
init.initialize()
trigger_on_load = nil
end
end
function init.initialize()
local player = game.players[event.player_index]
player.print("loaded game")
end
-- if you put () after a function it runs immediately
-- instead just pass in the function reference
-- wrong:
-- script.on_event(defines.events.on_tick, events.on_tick() )
-- correct:
script.on_event(defines.events.on_tick, events.on_tick )
-- ^ removed ()
-- on_load is a special type of event
-- wrong:
-- script.on_event(defines.events.on_load, events.on_load() )
-- correct:
script.on_load( events.on_load )
Code: Select all
local trigger_on_load = false
local events = {}
script.on_load(
function()
trigger_on_load = true
end
)
function events.on_tick()
if trigger_on_load then
initMod()
trigger_on_load = nil
end
end
function initMod()
for _, force in pairs(game.forces) do
force.reset_recipes()
force.reset_technologies()
end
end
script.on_event(defines.events.on_tick, events.on_tick )