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

Sending Programs via Rednet

Started by Obsidia, 03 January 2016 - 08:12 PM
Obsidia #1
Posted 03 January 2016 - 09:12 PM
Hello guys,
I havent been playing around with FTB/cc in a long time since I wanted to get back into Diamond 1 in LoL.

Now I'm back but all my worlds are gone cuz of a system reset.
I used to have a program I cant figure out how to write it anymore so I'm here for help. :P/>

I want to have a main computer sending a turtle what to do by simply entering it in the computer

It should look somewhat like this:


---- Computer
rednet.open("right", "92")   --- The wireless modem is on the right
										   --- and 92 should be the channel!?
while true do
term.clear()		 
term.setCursorPos(1,1)
write("Command: ")
input = read()
rednet.broadcast(input)
end
--- I already need some help here. Is the code above right? Can I use
--- it like this or do I need it to be "rednet.send(input)"?

----Turtle
rednet.open("left","92")  ---- Opening the modem and the channel
while true do
local event message = os.pullEvent("rednet_message")
--- Im pretty sure that code is somewhat wrong in so many ways
if message == "1" then
turtle.dropUp(1)
elseif message == "2" then
turtle.dropUp(2)
end
end


Can anyone tell me where my mistakes are and or if this is correct?



I know It would work like this on the turtle:


rednet.open("left", "92")
id, message = rednet.receive()
print(message)
shell.run(message)

Could I use something like this? I wouldnt want to write a program for every single command so I dont like it this way.

Thanks!
Creator #2
Posted 03 January 2016 - 09:28 PM
you don't need a number for rednet.open, since the channel is the id of the computer.

Also, put a comma between event and message.

.turtle.dropUp() does not exist.
Edited on 03 January 2016 - 08:29 PM
Obsidia #3
Posted 03 January 2016 - 09:33 PM
you don't need a number for rednet.open, since the channel is the id of the computer.

Also, put a comma between event and message.

.turtle.dropUp() does not exist.



Im quiet retarded. I realised seconds after my post what I am doing wrong. I got it like this now:

Computer:

rednet.open("SIDE", "CHANNEL")
while true do
term.clear()
term.setCursorPos(1,1)
write("Command: ")
input = read()
rednet.broadcast(input)
end

Turtle:

rednet.open("SIDE", "CHANNEL")
-- a simple function to reset it easily without writing it over and over again
function reset()
turtle.suckUp()
end
while true do
id, msg = rednet.receive()
print(msg)
if msg == "1" then
   reset()
   turtle.select(1)
   turtle.dropUp()
elseif msg == "2" then
   reset()
   turtle.select(2)
   turtle.dropUp()
--- etc.
end
end

New question though:

Can I cut the code down a bit?
I mean something like this:
if message == "1" then
reset()
select(1)
drop()

What would the function look like?

function select()
turtle.select(message)
end
Somewhat like this?
KingofGamesYami #4
Posted 03 January 2016 - 09:47 PM
tonumber converts a string into a number, or nil (if the string wasn't a number).


tonumber( "2" )
--# 2
tonumber( "Two" )
--# nil