Page 1 of 1

Reading file by lua

Posted: Sun Aug 09, 2015 3:35 pm
by craftoBot
I need to read file contents by lua
file was in directory script-output
and contains text

Re: Reading file by lua

Posted: Sun Aug 09, 2015 8:42 pm
by Choumiko
That's not possible.
You can work around it though, at least in single player. Have your mod write to the file via serpent.dump to let's say output.lua
Create a symlink or the Linux equivalent in your mod folder pointing to output.lua
Now you can do something like: variable = require "output" in control.lua
Each time you update/rewrite the file you need to reload the game for the changes to be read.
Quite hacky :D

Re: Reading file by lua

Posted: Sun Aug 09, 2015 9:34 pm
by craftoBot
It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....

Re: Reading file by lua

Posted: Sun Aug 09, 2015 9:45 pm
by ratchetfreak
craftoBot wrote:It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....
because desyncs

factorio relies on each mod doing the exact same thing on each client

arbitrary files don't allow that

Re: Reading file by lua

Posted: Sun Aug 09, 2015 10:52 pm
by jorgenRe
craftoBot wrote:It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....
Wow wow relax. I don't think it even is neccecary to read/write files because what is in global tables stays there no matter how many times you restart Factorio. And I think that'd way easier too ;)!

Edit i take that back :lol: !
I'm now working on my The Underground mod and i wanted to just write down the entity names and their position relevant to the others in a file...

Re: Reading file by lua

Posted: Sun Aug 09, 2015 11:29 pm
by craftoBot
ratchetfreak wrote:
craftoBot wrote:It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....
because desyncs

factorio relies on each mod doing the exact same thing on each client

arbitrary files don't allow that

I can check for file existance, i need only to read file content to send it into sync tables.....
It's strange that standart io.* package of lua is removed

Re: Reading file by lua

Posted: Sun Aug 09, 2015 11:30 pm
by craftoBot
jorgenRe wrote:
craftoBot wrote:It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....
Wow wow relax. I don't think it even is neccecary to read/write files because what is in global tables stays there no matter how many times you restart Factorio. And I think that'd way easier too ;)!
I know, and i just need to export file from factorio to use by another program e.g texteditor and then import edited file back to game

Re: Reading file by lua

Posted: Sun Aug 09, 2015 11:36 pm
by ratchetfreak
craftoBot wrote: I know, and i just need to export file from factorio to use by another program e.g texteditor and then import edited file back to game
then put the result data in a lua file that the mod imports

Re: Reading file by lua

Posted: Sun Aug 09, 2015 11:54 pm
by craftoBot
ratchetfreak wrote:
craftoBot wrote: I know, and i just need to export file from factorio to use by another program e.g texteditor and then import edited file back to game
then put the result data in a lua file that the mod imports
I already understand, but i want this data in plain text, not lua, but i will do like this....

Re: Reading file by lua

Posted: Wed Aug 17, 2016 12:00 pm
by sebgggg
Choumiko wrote:That's not possible.
You can work around it though, at least in single player. Have your mod write to the file via serpent.dump to let's say output.lua
Create a symlink or the Linux equivalent in your mod folder pointing to output.lua
Now you can do something like: variable = require "output" in control.lua
Each time you update/rewrite the file you need to reload the game for the changes to be read.
Quite hacky :D
OMFG! I'm ashamed i did not think of that... For my defense, i play on Windows, on which you can do the same with an admin command line:
mklink link target

Re: Reading file by lua

Posted: Sat Aug 25, 2018 7:27 pm
by _Peter_
ratchetfreak wrote:
craftoBot wrote:It's it's unbelivable...
WHY I CAN"T JUST READ FILE?! FACTORIO, WHY YOUR LUA IS SO STRANGE!!!
WHYYYY>>>>
Okay...
Let's try to hack it like this, so this is strange.....
because desyncs

factorio relies on each mod doing the exact same thing on each client

arbitrary files don't allow that
I think I understand the desync problem, but what if I want to make a mod that works only in single player? Then it would be perfectly ok to change the mod behavior from outside, or not?

I think it should be left to the discretion of the mod programmer whether or not to use methods that would not work in multiplayer, that means whether or not to read from files.

Re: Reading file by lua

Posted: Wed Sep 12, 2018 5:55 pm
by _Peter_
I was planning to write a mod that cannot do things that the player cannot do, in essence a voice interface for playing Factorio, including some natural language understanding parts.

This should not cause any desyncs, but of course it would require to get some streaming data from the outside into a mod.

Has such a type of mod be considered (and maybe ruled out for certain reasons)?

Re: Reading file by lua

Posted: Wed Sep 12, 2018 6:38 pm
by DaveMcW
Since 2015, the devs have added RCON support to the headless server. This allows arbitrary input while keeping multiplayer compatibility.

Re: Reading file by lua

Posted: Sun Oct 14, 2018 12:12 pm
by ownlyme
cant you just use this require hack with different filenames?
edit: works only on start
Screenshot_1.png
Screenshot_1.png (59.72 KiB) Viewed 5206 times