About on_loaded event

Place to get help with not working mods / modding interface.
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

Re: About on_loaded event

Post by kendoctor »

Qon wrote: Wed Aug 12, 2020 4:33 pm Avoid desyncs by avoiding to do anything the manual tells you to not do, and don't do anything that gives defferent clients different states for the same tick. You don't have to figure out what clients are doing what etc, because then you are doing something wrong. You don't think about manipulating gamestates in clients, only about doing deterministic changes to the abstract singular gamestate, even i multiplayer. The game itself will sync correctly it as long as you do your part correct.

The random() function depends on map seed and is deterministic. There's also non-map seed dependent random() which is also deterministic. Read the manual for info.
thx your reply. I commented this in another post. Just for example. Random thing in different client. that will difference the gameState.
Also if only depend ons map seed, theoratically, it will also give different result. Should be two factors. Mapseed and Game.tick. As i think. only Game.tick is enough.
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: About on_loaded event

Post by mrvn »

kendoctor wrote: Wed Aug 12, 2020 11:08 pm
Qon wrote: Wed Aug 12, 2020 4:33 pm Avoid desyncs by avoiding to do anything the manual tells you to not do, and don't do anything that gives defferent clients different states for the same tick. You don't have to figure out what clients are doing what etc, because then you are doing something wrong. You don't think about manipulating gamestates in clients, only about doing deterministic changes to the abstract singular gamestate, even i multiplayer. The game itself will sync correctly it as long as you do your part correct.

The random() function depends on map seed and is deterministic. There's also non-map seed dependent random() which is also deterministic. Read the manual for info.
thx your reply. I commented this in another post. Just for example. Random thing in different client. that will difference the gameState.
Also if only depend ons map seed, theoratically, it will also give different result. Should be two factors. Mapseed and Game.tick. As i think. only Game.tick is enough.
No, if you seed the random number generator with the map seed then it will give one precisely determined series of random numbers. On every client. They all have the same map seed.
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

Re: About on_loaded event

Post by kendoctor »

mrvn wrote: Wed Aug 12, 2020 11:12 pm
kendoctor wrote: Wed Aug 12, 2020 11:08 pm
Qon wrote: Wed Aug 12, 2020 4:33 pm Avoid desyncs by avoiding to do anything the manual tells you to not do, and don't do anything that gives defferent clients different states for the same tick. You don't have to figure out what clients are doing what etc, because then you are doing something wrong. You don't think about manipulating gamestates in clients, only about doing deterministic changes to the abstract singular gamestate, even i multiplayer. The game itself will sync correctly it as long as you do your part correct.

The random() function depends on map seed and is deterministic. There's also non-map seed dependent random() which is also deterministic. Read the manual for info.
thx your reply. I commented this in another post. Just for example. Random thing in different client. that will difference the gameState.
Also if only depend ons map seed, theoratically, it will also give different result. Should be two factors. Mapseed and Game.tick. As i think. only Game.tick is enough.
No, if you seed the random number generator with the map seed then it will give one precisely determined series of random numbers. On every client. They all have the same map seed.
You mean. for example
script.on_tick(function()
game.print(math.random(map.seed))
--- create_random_generator(seed) → LuaRandomGenerator
--- here just sudo. using facto mod random genartor
end)

this will give all the same results ?
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

Re: About on_loaded event

Post by kendoctor »

kendoctor wrote: Wed Aug 12, 2020 11:52 pm
mrvn wrote: Wed Aug 12, 2020 11:12 pm
kendoctor wrote: Wed Aug 12, 2020 11:08 pm
Qon wrote: Wed Aug 12, 2020 4:33 pm Avoid desyncs by avoiding to do anything the manual tells you to not do, and don't do anything that gives defferent clients different states for the same tick. You don't have to figure out what clients are doing what etc, because then you are doing something wrong. You don't think about manipulating gamestates in clients, only about doing deterministic changes to the abstract singular gamestate, even i multiplayer. The game itself will sync correctly it as long as you do your part correct.

The random() function depends on map seed and is deterministic. There's also non-map seed dependent random() which is also deterministic. Read the manual for info.
thx your reply. I commented this in another post. Just for example. Random thing in different client. that will difference the gameState.
Also if only depend ons map seed, theoratically, it will also give different result. Should be two factors. Mapseed and Game.tick. As i think. only Game.tick is enough.
No, if you seed the random number generator with the map seed then it will give one precisely determined series of random numbers. On every client. They all have the same map seed.
You mean. for example
script.on_tick(function()
game.print(math.random(map.seed))
--- create_random_generator(seed) → LuaRandomGenerator
--- here just sudo. using facto mod random genartor
-- https://lua-api.factorio.com/latest/Lua ... rator.html
end)

this will give all the same results ?
Also we are talking about different functions.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: About on_loaded event

Post by eradicator »

kendoctor wrote: Wed Aug 12, 2020 3:13 pm 8. What will cause desyncs in this catchup period? Even excuting the same events?
on_load, on_init and on_config_changed are only executed on every client once per session. Aka if PlayerB joins then PlayerA will *not* execute them again. Thus A+B are not processing the exact same events. Infact PlayerB will never execute init/config_changed because they were already executed when PlayerA started/loaded the map and their result became part of the game state that is saved and synced to PlayerB when he starts joining.

In factorio you have to basically assume that once a map is started it is never stopped or save/loaded. This is the basic state that all mods run in. Like @Rseding already said on_load exists soley to bring the lua-state (which is not saved) back into it's former state if a save/load cycle happens so that the game behaves exactly as if the save/load cycle never happend.
kendoctor wrote: Wed Aug 12, 2020 3:13 pm 9. random() has different seed in different client at different time.
No. In Soviet Factorio random is deterministic.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: About on_loaded event

Post by mrvn »

kendoctor wrote: Wed Aug 12, 2020 11:52 pm
mrvn wrote: Wed Aug 12, 2020 11:12 pm
kendoctor wrote: Wed Aug 12, 2020 11:08 pm
Qon wrote: Wed Aug 12, 2020 4:33 pm Avoid desyncs by avoiding to do anything the manual tells you to not do, and don't do anything that gives defferent clients different states for the same tick. You don't have to figure out what clients are doing what etc, because then you are doing something wrong. You don't think about manipulating gamestates in clients, only about doing deterministic changes to the abstract singular gamestate, even i multiplayer. The game itself will sync correctly it as long as you do your part correct.

The random() function depends on map seed and is deterministic. There's also non-map seed dependent random() which is also deterministic. Read the manual for info.
thx your reply. I commented this in another post. Just for example. Random thing in different client. that will difference the gameState.
Also if only depend ons map seed, theoratically, it will also give different result. Should be two factors. Mapseed and Game.tick. As i think. only Game.tick is enough.
No, if you seed the random number generator with the map seed then it will give one precisely determined series of random numbers. On every client. They all have the same map seed.
You mean. for example
script.on_tick(function()
game.print(math.random(map.seed))
--- create_random_generator(seed) → LuaRandomGenerator
--- here just sudo. using facto mod random genartor
end)

this will give all the same results ?
Not sure what "math.random(map.seed)" will give. A random number between 1 and seed and a desync?
To seed the math.random() function you need to call math.randomseed(map.seed). Then I'm pretty sure you get the same sequence on every client every time. Unless some other mod tries to use math.random() too and seeds it differently. Best not use that.

I was actually refering to "game.create_random_generator(seed)". If you use the map.seed that will give the same result on every client every tick. So while perfectly fine that makes little sense for on_tick.

You should either create the generator once at the start and keep pulling numbers from it or you have to include the game tick so the seed changes over time. Note that "game.create_random_generator(map.seed + tick)" only changes every few ticks as near seeds have the same sequence. But really why not store a random number generator in global. I see no point in creating a new one every tick.

PS: A valid use of game.create_random_generator() would be in on_chunk_generated(). Create a new random number generator using the map.seed and (x, y) coords of the chunk as seed to do random stuff to the new chunk. That way no matter in what order the chunks are generated the chunk will always be the same. This is relevant when people run around a lake clockwise, die, reload the autosave and then run around the lake anti-clockwise. You don't want the chunks to suddenly be different.
Qon
Smart Inserter
Smart Inserter
Posts: 2164
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: About on_loaded event

Post by Qon »

https://lua-api.factorio.com/latest/Libraries.html wrote:
math.random()
math.random() is implemented within Factorio so that it is deterministic, in both the data stage and during runtime scripting. In the data stage, it is seeded with a constant number. During runtime scripting, it uses the map's global random generator which is seeded with the map seed. The map's global random generator is shared between all mods and the core game, so they can affect the random number that is generated. If that behaviour is not desired, LuaRandomGenerator can be used to create a random generator that is completely separated from the core game and other mods. In the runtime stage, math.random() cannot be used outside of events or during loading.
math.randomseed()
Using math.randomseed() in Factorio has no effect on the random generator, the function does nothing. If custom seeding or re-seeding is desired, LuaRandomGenerator can be used instead of math.random().
math.randomseed() is safe to use...
nop is deterministic.
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: About on_loaded event

Post by mrvn »

There goes the idea of writing a mod that resets the math.randomseed() in such a way to cause the game or other mods to (mis)behave in specific ways.
Post Reply

Return to “Modding help”