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

Turtle bomber

Started by DziugsBugs, 29 July 2017 - 01:43 PM
DziugsBugs #1
Posted 29 July 2017 - 03:43 PM
Hello!

I am creating a turtle bomber program. It listens for rednet messages and executes the bombing, but it doesn't move!
I think i wrote something wrong.
Here's the code:

Controller:

function bomber()
print("How many blocks you want turtle to go up?")
local height = read()
rednet.broadcast(height)
term.clear()
term.setCursorPos(1, 1)
print("Height:")
print(height)
sleep(1)
term.clear()
term.setCursorPos(1, 1)
print("How many blocks you want turtle to go up?")
local lenght = read()
rednet.broadcast(lenght)
term.clear()
term.setCursorPos(1, 1)
print("Lenght:")
print(lenght)
print("Starting bomber attack!")
sleep(1)
local go = "go"
rednet.broadcast(go)
end
rednet.open("back")
bomber()

Turtle:

function bomber()
while true do
local height = rednet.receive()
local lenght = rednet.receive()
local bstart = rednet.receive()
if bstart == "go" then
print("Running bomber operation!")
for i=1, height do
turtle.up()
end
else
print("Error! Can't go up!")
end
for i=1, lenght do
redstone.setOutput("bottom", true)
turtle.forward()
turtle.forward()
turtle.placeDown()
redstone.setOutput("bottom", false)
end
end
end
bomber()

Thanks for replies :)/>
Bomb Bloke #2
Posted 30 July 2017 - 01:31 AM
read() returns strings, which won't work in for loops. You'll want to tonumber() those.

The reason your script doesn't simply error out is that rednet.receive() has two return values, the id of the sender followed by the message that was sent. Currently you're only recording the first of those, so your turtle wants to loop its movement calls a number of times equal to the controller's ID number.

As for why the turtle fails to move, either your controller's ID is 0, or you've neglected to provide fuel.
Riewest #3
Posted 30 July 2017 - 07:47 AM
I'm very satisfied that the first and only answer was from someone named "Bomb Bloke" for a bombing program.

XD
Reinified #4
Posted 30 July 2017 - 12:26 PM
You need to open your rednet modem before broadcasting. Open it first thing.
The Crazy Phoenix #5
Posted 30 July 2017 - 01:58 PM
You need to open your rednet modem before broadcasting. Open it first thing.

No, you don't. Modems only need to be opened to receive from them.