This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Explorer's profile picture

A little rednet question...

Started by Explorer, 17 July 2012 - 07:27 AM
Explorer #1
Posted 17 July 2012 - 09:27 AM
It has been nearly 2 years since i stopped programming, by now ive forgotten most lua syntax… anyways, on to my question, well more of an explanation. ive been experimenting with computerCraft for a few days now, and at the moment i am trying to control one computer with another.

the "home" comp (the one ill be controlling with)
is a normal terminal with an attached wireless modem

the controlled compueter is attached to both a wireless modem and an array of redstone wiring.

i seek to send a rednet message such as

"hatch" from the home comp to the controlled
and at the controlled the computer would notice the message was "hatch"
and would do a redpulse in a specific wire, or sequence of specific wires.

if someone could maybe give an example of how this is done, such as the code to make the comp analyze the message and connect it with a specific function and the coding of the function itself, I would greatly appreaciate it

-Explorer

p.s. please add desc. after lines of code
Thanks!
gamesaucer #2
Posted 17 July 2012 - 04:21 PM
a Rednet message is a string, so

id, str, dist = rednet.receive()
if str == "hatch" then
–Code
end

should work.
Explorer #3
Posted 17 July 2012 - 05:47 PM
Let me clarify- I've already programmed this

rednet.open("bottom")
while true do
event, id, text = os.pullEvent()
if event == "rednet_message" then
print(id .. "> " .. text)
end
end

This is to loop for receiving of course- how should I change/ what should I add- to do what needs to be done

Also can I write several functions such as "hatch" in the same script as os.pullEvent() ?
gamesaucer #4
Posted 17 July 2012 - 08:31 PM
yes, you can create functions and call them whenever you need them, like in

if text == "hatch" then
hatch()
end

I don't know if it's possible in lua to do something like calling a string as a function, like so:
text = text .. "()"
execute text
OmegaVest #5
Posted 17 July 2012 - 08:49 PM
You can use loadstring, so far as I know. It's not a perfect solution by any means, but it works. . . most of the time, for simple uses like this. Though, I've only seen it run whole programs with any kind of success.
MysticT #6
Posted 17 July 2012 - 09:03 PM
Or you can store the functions in a table, and use the messages as keys/names:

local tFunctions = {}
tFunctions["hatch"] = function()
  -- do something here
end

local id, msg = rednet.receive()
if tFunctions[msg] ~= nil then
  tFunctions[msg]()
end
gamesaucer #7
Posted 18 July 2012 - 08:39 AM
^That's very powerful, I should start using that as well.
That's indeed more or less what I meant with calling a string as a function.
You should then be able to create every string you want to be recognizable as command as a function, sending a specific amount of pulses.