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

What's Wrong?

Started by Rodrick, 02 April 2013 - 12:35 PM
Rodrick #1
Posted 02 April 2013 - 02:35 PM
I made a wireless turtle cooblestone generator, but it is not working, when I run, he does not print or do anything.

The program:

[COMPUTER]
Program name: stoneGenerator
local turtleId = 6
local id, message = rednet.receive()
local command = shell.run(generator, input)
local side = bottom

rednet.open(lado)
print("Modem connected.")
sleep(0.5)

print("Trying to connect to the turtle...")
rednet.send(turtleId, try)
if id == turtleId and message == ok then
print("Turtle connected.")
end

print("How many cooblestones want?")
input = read("")

print("Sending request of cooblestones......")
rednet.send(turtleId, sending)
rednet.send(turtleId, command)

if id == turtleId and message == generating then
print("Request accepted. Generating cooblestones.")
end

print("Ending Program......")

rednet.close(side)

[TURTLE]
Program name: Wireless
local computerId = 9
local id, message = rednet.receive()

rednet.open(right)

if id == computerId and message == try then
	rednet.send(computerId, ok)
	end

if id == computerId and message == sending then
	rednet.receive(5)
	end

rednet.close(right)

shell.run(wireless)

The program that break the cooblestone:
Program Name: generaator
local arg = { ... }
local i = 1
local quantity =  arg + 1

repeat -- O loop 

	turtle.dig()
	sleep(0.25)
		turtle.turnRight()
		sleep(0.25)
		
until i == quantity
grand_mind1 #2
Posted 02 April 2013 - 05:18 PM
On your turtle, you put
"local id, message = rednet.receive()"
before you opened the rednet modem. I think if you open the modem before trying to receive then your problem should be fixed.
remiX #3
Posted 02 April 2013 - 09:57 PM
On your turtle, you put
"local id, message = rednet.receive()"
before you opened the rednet modem. I think if you open the modem before trying to receive then your problem should be fixed.

Same with computer code

When setting a variable, you need to put them in quotation marks
local side = "bottom"

Same with shell.run (if you're not using a variable)
shell.run('program')

Instead of using shell.run to repeat the program, use a while true do loop
Rodrick #4
Posted 03 April 2013 - 06:12 AM
Oh, okey… I also thought that was it but was not sure did not know what to do …

I made the changes in the programs, but in the turtle program says:
rednet:11: string expected

The code:
local computerId = "9"

rednet.open(right)

local id, message = rednet.receive()

if id == computerId and message == try then
	rednet.send(computerId, ok)
	end

if id == computerId and message == sending then
	local quantity = rednet.receive()
	end

rednet.close(right)

shell.run(generator, quantity)
LBPHacker #5
Posted 03 April 2013 - 06:52 AM
rednet.send(computerId, "ok")
instead of
rednet.send(computerId, ok)

(In your code, "ok" is now a not-implemented variable (nil) - and that's not a string)
Rodrick #6
Posted 03 April 2013 - 01:01 PM
Okey, I patched some code became simpler and without checking the connection, for example.
But now the computer sends to the turtle the quantity, but nothing happens.

The actual code:

COMPUTER:
Spoiler
local turtleId = 12
local command = 'shell.run(gerador, input)'
local side = 'bottom'

rednet.open(side)
	print("Modem on.")
	sleep(0.5)

print("How many cooblestones do you want?")
	input = read()

print("Cooblestone request send")
	rednet.send(turtleId, command)

print("Ending program")

rednet.close(side)

TURTLE:
Spoiler
local computerId = '9'

rednet.open('right')
print("Modem on")
print("Waiting request...")

id, quantity = rednet.receive()

if id == computerId then
	shell.run(generator, quantity)
end

TURTLE 2:
Spoiler
local arg = { ... }
local i = 1
local quantidade =  arg + 1

repeat --

	turtle.dig()
	sleep(0.25)
		turtle.turnRight()
		sleep(0.25)
		
until i == quantity

shell.run('wireless')
Bubba #7
Posted 03 April 2013 - 01:05 PM



local computerId = '9' --This should be 9 without the quotes.

rednet.open('right')
print("Modem on")
print("Waiting request...")

id, quantity = rednet.receive()

if id == computerId then
		shell.run(generator, quantity)
end

You use a string for the computerID. It should be a number. rednet.receive() will return a number value as the ID so trying to compare the two will not work if one is a string.
TheArchitect #8
Posted 03 April 2013 - 01:10 PM
The problem is that you're not concatenating the command string correctly. Look:

local turtleId = 12
local command = 'shell.run(gerador, input)'  --<<-- You're defining the string here. BEFORE you ask the user to input the data
local side = 'bottom'

rednet.open(side)
	print("Modem on.")
	sleep(0.5)

print("How many cooblestones do you want?")
	input = read()	 --<<-- You're asking the user for input HERE, but you're not putting this back into the command string.

print("Cooblestone request send")
	rednet.send(turtleId, command)	 --<<-- You're sending the generic command defined first thing above.

print("Ending program")

rednet.close(side)

You should change the above code to this:

local turtleId = 12
local command = ''  --<<-- Doesn't matter that it's empty, because you're going to fill it later
local side = 'bottom'

rednet.open(side)
	print("Modem on.")
	sleep(0.5)

print("How many cooblestones do you want?")
	input = read()	 --<<-- You're asking the user for input HERE

	command = 'shell.run("generator", ' .. input .. ');'	  --<<-- And here you insert it into the command string.

print("Cooblestone request send")
	rednet.send(turtleId, command)	 --<<-- Now you're sending the complete command string.

print("Ending program")

rednet.close(side)

[Edit] Silly code tags, parsing ' as comment separator.
Bubba #9
Posted 03 April 2013 - 01:49 PM
-snip-

I don't think you're not really think this one through. Now that I look over the entire code I'm seeing quite a few strange things.

Why are you trying to send a shell.run command? Nowhere in the code is loadstring used, and ergo it cannot be used as a valid command. Also, it was my assumption that you wanted to send the amount of cobblestone to create, not an actual command to run a program.


Going against my "moral code", I'm going to write you a fully fledged program to show you how it should be done. shell.run is really not necessary in this situation.

Computer

local turtleID = 12
local modem_side = "bottom"
rednet.open(modem_side)
write("How much cobblestone do you want? ")
local amount = read()
rednet.send(turtleID, amount) --I'm sending just the amount of cobblestone to make, no commands
print("Sent to the turtle. Ending program.")

Turtle

local computerID = 9
rednet.open("right")
while true do
  local id, message = rednet.receive()
  if id == computerID then
	print("Generating "..message.." cobblestone.")
	local amount = tonumber(amount) --Convert the string that is sent by the computer into a number so we can use it in a for loop
	for i=1, amount do --A for loop (not a repeat until loop) is the best thing to use in this situation
	  turtle.dig()
	  sleep(0.25)
	  turtle.turnRight()
	  sleep(0.25)
	end
  end
end