game.write_file() : why no read_file ?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
ritonlajoie
Inserter
Inserter
Posts: 46
Joined: Tue Mar 29, 2016 10:31 am
Contact:

game.write_file() : why no read_file ?

Post by ritonlajoie »

Hi

I'm looking at the list of methods available through the API.
I read on the wiki that there is a read_file method.

https://wiki.factorio.com/index.php?tit ... write_file

I wonder what it's used for, since I see no read_file() method :p
Is it an error on the wiki ?

Thanks !

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

Re: game.write_file() : why no read_file ?

Post by Rseding91 »

Writing files can easily be done in a deterministic way - the contents written out won't change the game state at all.

Reading files would be completely non-deterministic - the contents of the file could (and would most likely) change between every save load and be different on each peer in a multiplayer environment.

Essentially, it doesn't work if you want to support replays and it doesn't work if you want to support multiplayer. There's also the entire security issue that would need to be addressed (what can a mod read, how much at a time, and so on) that makes it just not worth the time to look into.
If you want to get ahold of me I'm almost always on Discord.

gheift
Fast Inserter
Fast Inserter
Posts: 188
Joined: Tue Mar 03, 2015 9:20 pm
Contact:

Re: game.write_file() : why no read_file ?

Post by gheift »

One way to make it deterministic would be something like this:
create a function read_file(name) in the LuaPlayer class/interface. The behavior is as follows:

Code: Select all

function read_file(player, name)
    local str
    if game.is_live then
        if player == local_player then
            str = read_from_os(name)
            send_to_peers(player, name, str)
        else
            str = read_received(player, name)
        end
        record_in_replay(player, name, str)
    else
        -- in replay
        str = load_from_repaly(player, name)
    end

    return str
end

Post Reply

Return to “Modding discussion”