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

[Lua] What's wrong with this script?

Started by rinux, 24 March 2013 - 04:22 PM
rinux #1
Posted 24 March 2013 - 05:22 PM
Hello everyone, I have to paint a massive area for my new base using Painters from IndustrialCraft 2. I realized that it'd take too long by hand so I thought I'd try to make it paint using turtles. I found out that it works, so I decided to create a script for it to do my walls for me, from left to right. I will paste the code below:

– Functions
function paint()
turtle.select(1)
turtle.craft(1)
turtle.place()
end

function upanddown()
repeat
repeat
turtle.craft(1)
paint()
turtle.up()
until turtle.place() == false
turtle.craft(1)
until turtle.up() == false
repeat
repeat
turtle.craft(1)
paint()
turtle.down()
until turtle.place() == false
turtle.craft(1)
until turtle.down() == false
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end

– Script
repeat
upanddown()
until
turtle.getFuelLevel() == 0
eof = 1

Now I'm no programmer so this script might seem silly. It's a bit messy and repetitive because I was testing for the source of the problem. The turtle was painting every other block so I decided to make it go back down once it hits the ceiling in order to paint the whole column of Construction Foam blocks. I don't mind wasting the extra fuel, but the big problem is in the turtle.craft() portion. To craft a white painter, you need 1 bonemeal and 1 uncolored painter. I put them both in the turtle, one painter and a few stacks of bonemeal. Sometimes the crafty turtle actually crafts the white painter, but other times it skips that step for whatever reason. The times that it does craft the white painter, when it has to craft it again later after the 32 uses of the painter are used up, it skips that step again.

Any ideas on what's causing this?
Mads #2
Posted 24 March 2013 - 07:49 PM
Please format your code. It's basically un-readable in it's current state.
sleawnis #3
Posted 25 March 2013 - 02:50 AM
1) what error are u getting

2) please format

and it might be you need to do:

local function ...
not:

function ...
Engineer #4
Posted 25 March 2013 - 07:30 AM
1) what error are u getting

2) please format

and it might be you need to do:

local function ...
not:

function ...
local only makes the function well, local. Otherwise it would be global so that is not the thing that is messing up things.
But a question that should be answered on every post (according to me), what error does it give or what is the unexpected behaviour?
TheOddByte #5
Posted 25 March 2013 - 09:26 AM
And please indent it and put [.CODE] [./CODE] (without '.') So it gets easier to read..
And put the error you are getting etc
TheOddByte #6
Posted 25 March 2013 - 09:32 AM
And why do you do this?

repeat
repeat
Have'nt you ever heard of a 'while <condition> do' loop?

And what exactly do you want the code todo?
rinux #7
Posted 25 March 2013 - 09:35 AM
please format
Not sure what you mean, I'm not a programmer. Do you mean add spaces to emphasize the functions, etc?

what error does it give or what is the unexpected behavaviour

There is no error outputted.
The script is supposed to make the turtle go straight up to the top while painting every block, then turn right, go forward, turn left, then go straight down to the bottom while painting every block. The painter has 32 uses before needing to be re-bonemealed, which is done by putting an uncolored painter in the crafting table with a bonemeal. I use the function

turtle.craft()
to craft the white painter.

There are two problems, however,

1) It goes up straight, left and right perfectly, the movement is fine. But it skips painting every block. Like it will paint one, go up one, not paint that one, go up one, then paint that one, so essentially every other block is painted.

I remedied this by making it go straight up, then back down, it's a temporary and fuel-wasting fix, but I have lots of fuel, so it doesn't matter.

2) this part is the more important problem. The turtle doesn't recraft the painter for me. When the painter depletes, it becomes an uncolored painter and needs to be recolored white to allow 32 more uses of white paint. I wanted to set a way for the turtle to detect how many times it actually paints the block by checking if it does paint by using

p = 0
if turtle.place() == true then
  p = p + 1
end

then let it recraft the painter when the 32 uses happen, but it didn't seem to work.
rinux #8
Posted 25 March 2013 - 09:37 AM
And why do you do this?

repeat
repeat
Have'nt you ever heard of a 'while <condition> do' loop?

And what exactly do you want the code todo?

As I said in the OP, I'm no programmer by any means, this is my first script.

I put the two repeats to test whether or not it is actually crafting the white painter, and it still didn't, it was intended redundancy.

What would the 'while <condition> do' loop do that the repeat cannot? Sorry if it seems like a silly question.
rinux #9
Posted 25 March 2013 - 12:22 PM
I tried to fix it using the while <condition> do advice from before. I added notes to help you understand it better. The goal of the script is to make the turtle go up 1 block each time, paint the block, then go up painting each block until it hits the roof, then turn right, go forward, turn left, and go down to the bottom while painting each block.

I've succeeded in making it so that each block gets painted now, I suppose the issue was that I used repeat too much before, and that caused it to skip the painting step or whatever. Anyways, the only issue now is that once it hits the ceiling and detects that it cannot go up anymore, it won't turn and go down to the next column of blocks along the wall like I intended it to. Any fix for this?


-- This will craft painter and paint
function p()
turtle.craft()
turtle.place()
end
-- This will turn the turtle
function t()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
-- This makes turtle go up + paint
function u()
while true do
  p()
  turtle.up()
end
end
-- This makes turtle go down + paint
function d()
while true do
  p()
  turtle.up()
end
end

-- loop
function h()
repeat
  u()
until turtle.up() == false
t()
repeat
  d()
until turtle.down() == false
t()
end
-- main script
repeat
h()
until turtle.getFuelLevel == 0
Brandhout #10
Posted 25 March 2013 - 01:48 PM
It's a typo, u have turtle.up() in the function that you say it should move the turtle down. You might wanne use turtle.down()
rinux #11
Posted 25 March 2013 - 02:17 PM
Nice catch! But that doesn't explain why it doesn't turn, move forward, then turn towards the wall. Any clues?
Mads #12
Posted 25 March 2013 - 10:38 PM
I'd suggest learning Lua before writing anything, or atleast write it whilst you're learning. Just writing something from a tutorial won't teach you anything, and you won't be able to do it again.
rinux #13
Posted 26 March 2013 - 04:49 AM
I'd suggest learning Lua before writing anything, or atleast write it whilst you're learning. Just writing something from a tutorial won't teach you anything, and you won't be able to do it again.

I'm not doing this from a tutorial, I'm doing this from my limited knowledge on Lua in order to help me better understand Lua. So far I learned quite a bit from this endeavor.
Bubba #14
Posted 26 March 2013 - 05:51 AM
-snip- hang on a sec, just saw a few posts above with your purpose.
Edit:

The problem lies here:

function u()
	while true do
	  p()
	  turtle.up()
	end
end
-- This makes turtle go down + paint
function d()
while true do
  p()
  turtle.up()
end
end

You are using a while loop that never ends, so it just keeps trying to go up and craft items even though there is a block above it. The same goes for going down. You could fix it with something like this:

local function u()
local status = true
  while status do
    p()
    status = turtle.up()
  end
end

This way, the turtle will go up until it can no longer do so, at which point it will set status to false and break the loop.
Brandhout #15
Posted 26 March 2013 - 03:30 PM
Bubba was totally right, it gets stuck in an infinite loop. Also, you have two loops to make ur turtle go up and down, you don't need the entire middle part. This should work, although i haven't tested it.


-- This will craft painter and paint
function p()
  turtle.craft()
  turtle.place()
end
-- This will turn the turtle
function t()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
end
 
-- loop
function h()
  repeat
    p()
  until not turtle.up()
  t()
  repeat
    p()
  until not turtle.down()
  t()
end
-- main script
while true do
  h()
end
rinux #16
Posted 27 March 2013 - 04:15 AM
Thanks guys! Helped tons. I managed to get the whole area painted with an inefficient script now so I can't really test it, but it looks to me like it'd work. Thanks again.