Page 1 of 1

game.write_file() : why no read_file ?

Posted: Thu Mar 31, 2016 12:58 pm
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 !

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

Posted: Thu Mar 31, 2016 1:03 pm
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.

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

Posted: Thu Mar 31, 2016 8:10 pm
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