Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
SuperSamir

How to write files from outside the directory?

Question

14 answers to this question

Recommended Posts

  • 0
  • Solution

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

 

Link to post
Share on other sites
  • 0

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.

Link to post
Share on other sites
  • 0

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).

Link to post
Share on other sites
  • 0

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.

Link to post
Share on other sites
  • 0

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

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.