SuperSamir 0 Posted August 9, 2019 Share Posted August 9, 2019 Is there any possible way to edit files with io.write() from outside the directory? The directory being the computer ID. Quote Link to post Share on other sites
0 Molinko 43 Posted August 10, 2019 Share Posted August 10, 2019 I'm having a hard time understanding your question. Can you be more descriptive of want you want to achieve? Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 10, 2019 Author Share Posted August 10, 2019 48 minutes ago, Molinko said: I'm having a hard time understanding your question. Can you be more descriptive of want you want to achieve? So I would want computer #1 to edit a text file in computer #2's directory. Quote Link to post Share on other sites
0 Molinko 43 Posted August 10, 2019 Share Posted August 10, 2019 I don't believe one computer can edit another computers files unless they are connected via some sort of client program over a network which you would have to write. io.write it's for writing to running processes or files in the same machine Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 10, 2019 Author Share Posted August 10, 2019 Is it possible to use the internet API to do something similar then? Quote Link to post Share on other sites
0 Molinko 43 Posted August 10, 2019 Share Posted August 10, 2019 Possible. Maybe. But definitely not practical. You would use the network card along with client and host programs on each machine. Computers dont have access to each others components so any communication can be done over a network. You can write a program on one machine to request access and write data over a network connection and another program on another machine to answer requests and read/write data from that connection. Im not really sure of another way to perform this. Perhaps others can chime in. Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 10, 2019 Author Share Posted August 10, 2019 To do that, I would need to use event.pull() right? The problem is, I need to write data without pausing either of the computers. Quote Link to post Share on other sites
0 CptMercury 6 Posted August 10, 2019 Share Posted August 10, 2019 Like Molinko said, you need a custom program that will allow you to manipulate another computer. And using network cards might be the best option. There are several ways to write programs that allow sending commands to a second computer; a very naive implementation would use the function load. When passed a string, the string is loaded as a chunk of Lua code and returns a function, that, when called, does whatever was coded in the string. And yes, you need to use event.pull or register a listener via event.listen, but your code needs to "pause" at least every 5 seconds anyways, via event.pull or os.sleep (which is essentially the same as event.pull), so that shouldn't be an issue. So a simple code would look like this: The computer receiving the commands: local event = require "event" while true do local _, _, from, _, _, message = event.pull("modem_message") local res, err_msg = load(message) if not res then -- an error occured while loading the string, so here you can do error management -- like throwing an error and ending the program, printing the error message or write it to a log file, -- ignore the error and continue running the main loop etc print(err_msg) else res() -- on success, load returns a funtion, so call it end end --------------------------------------------------------------------------------- -- if you want to throw an error on failure, using assert would be the way to go --------------------------------------------------------------------------------- local event = require "event" while true do local _, _, from, _, _, message = event.pull("modem_message") assert(load(message))() -- if load returns the function, i.e. loading the string was successfull, the function is called -- when an exception was catched, an error with the error message is thrown end So then you can send a message containing "io.write('test')", and the second computer will execute this task. You can also open files etc, so basically you can do everything remotely. But as I said, this is not a very optimized program since load is quite heavy on the machine since it has to compile the code, but it is probably the most versatile approach. (keep in mind, when you want to use strings inside your code, you need to use the correct quotes, if you wrap your string in ", you can't use " for strings inside, so you need to use ' for example, or you wrap your main string in [[..your code goes here..]] these brackets). Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 11, 2019 Author Share Posted August 11, 2019 I tried something similar to this with serialization, but within the time frame I gave with event.pull(), it threw an error message when the time expires. Does this method ignore the serialization error and continue with the script? Quote Link to post Share on other sites
0 CptMercury 6 Posted August 11, 2019 Share Posted August 11, 2019 Can you post your code and a screenshot of the error message you are getting? This would help alot. I don‘t know exactly what you mean by serialization and why you have problems with the time expiring, but when I had the chance looking at the code I might be able to tell. Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 11, 2019 Author Share Posted August 11, 2019 I can't send you an error message since for whatever reason Minecraft won't open up right now, but I'll send you an excerpt of the code. function loopWait() loopOff = false db.clear(1) me.store({name = "minecraft:record_wait"}, db.address, 1) selectEmpty() me.requestItems(db.address, 1, 1) ic.equip() robot.useUp() repeat _, _, _, _, _, serialized_message = event.pull(239, "modem_message") received_table = require("serialization").unserialize(serialized_message) if recieved_table.loopSwitch == "off" then loopOff = true end robot.useUp() selectFull() ic.equip() robot.useUp() until loopOff == true robot.useUp() selectFull() me.sendItems() end Quote Link to post Share on other sites
0 Molinko 43 Posted August 11, 2019 Share Posted August 11, 2019 How big is the serialized table and what kind of values are in the table? Are there recursive entries or functions in the table? Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 11, 2019 Author Share Posted August 11, 2019 There's only one string in the table, but that part's working fine. The thing is, after the event.pull() is unable to find a message (which is deliberate for the code), the serialization part errors out, cancelling the rest of the script. Quote Link to post Share on other sites
0 Solution CptMercury 6 Posted August 11, 2019 Solution Share Posted August 11, 2019 Well, first of all, if you only need the message to contain one string, then you are better off by just sending the string as the message, so no need to make a table with that string, serialize the table transforming it into a string which is then send via the modem, once received, transformed back into a table which is then indexed to get the string you want to send... But anyways, I would assume the error with the serialization comes from trying to serialize a nil value. You're using a timeout with your event.pull, that means, event.pull returns nil if no modem_message was received within that 239 seconds. So either you simply remove the number specifying the timeout, or you add an if-statement checking whether you actually received a message and skip the serialization part when the message is nil. -- without the timeout, if this is possible, but it looks like you want your robot to execute some stuff every 239 seconds function loopWait() loopOff = false db.clear(1) me.store({name = "minecraft:record_wait"}, db.address, 1) selectEmpty() me.requestItems(db.address, 1, 1) ic.equip() robot.useUp() repeat _, _, _, _, _, serialized_message = event.pull("modem_message") received_table = require("serialization").unserialize(serialized_message) if recieved_table.loopSwitch == "off" then loopOff = true end robot.useUp() selectFull() ic.equip() robot.useUp() until loopOff == true robot.useUp() selectFull() me.sendItems() end -------------------------------- -- with an if statement verifying there actually was a message received function loopWait() loopOff = false db.clear(1) me.store({name = "minecraft:record_wait"}, db.address, 1) selectEmpty() me.requestItems(db.address, 1, 1) ic.equip() robot.useUp() repeat _, _, _, _, _, serialized_message = event.pull(239, "modem_message") if serialized_message then received_table = require("serialization").unserialize(serialized_message) if recieved_table.loopSwitch == "off" then loopOff = true end end robot.useUp() selectFull() ic.equip() robot.useUp() until loopOff == true robot.useUp() selectFull() me.sendItems() end But the easiest and best way would be to send your string directly as the message -- the computer sending the message modem.send(address, port, "off") -- receiving computer function loopWait() loopOff = false db.clear(1) me.store({name = "minecraft:record_wait"}, db.address, 1) selectEmpty() me.requestItems(db.address, 1, 1) ic.equip() robot.useUp() repeat _, _, _, _, _, message = event.pull(239, "modem_message") if message == "off" then loopOff = true end robot.useUp() selectFull() ic.equip() robot.useUp() until loopOff == true robot.useUp() selectFull() me.sendItems() end Molinko 1 Quote Link to post Share on other sites
0 SuperSamir 0 Posted August 11, 2019 Author Share Posted August 11, 2019 This was the answer I was looking for. Thank you! Quote Link to post Share on other sites
Is there any possible way to edit files with io.write() from outside the directory? The directory being the computer ID.
Link to post
Share on other sites