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

Issues with Frame Quarry program and variables

Started by ilja, 12 May 2013 - 04:03 AM
ilja #1
Posted 12 May 2013 - 06:03 AM
Hi, I'm new to computercraft and having issues.

Hi, I'm a complete LUA and programming newb, and can't get this to work. And I have no idea why. I'm trying to create a simple quarrying program for my very basic one-direction frame quarry.

This is the code so far:

-- Setting variables on startup
local Height = 80
local Direction = "Down"
local Move = 0

  while(true) do

-- Setting what the program should do
  if Height < 6 then
  local Direction = "Up"
  end
  if Height > 80 then
  local Direction = "Move"
  end
  if Move == 8 then
  local Direction = "Down"
  end

-- Mining
  if Direction == "Down" then
	redstone.setBundledOutput("back", colors.blue)
	print (Direction)
	print (Height)
	sleep(0.2)
	redstone.setBundledOutput("back", 0)
	sleep(1)
	Height = Height - 1
  else

-- Retracting
  if
	Direction == "Up" then
	redstone.setBundledOutput("back", colors.brown)
	print (Direction)
	print (Height)
	sleep(0.2)
	redstone.setBundledOutput("back", 0)
	sleep(1)
	Height = Height + 1
  else

-- Moving
  if
	Direction == "Move" then
	redstone.setBundledOutput("back", colors.green)
	print (Direction)
	print (Move)
	sleep(0.2)
	redstone.setBundledOutput("back", 0)
	sleep(1)
	Move = Move + 1

end
end
end
end

The issue is that it never changes the Direction variable even when its <6, and goes straight down into the negatives. I've tried adding the "If Height < 6 set Direction = "Up"" in the at the bottom of the Mining section, but that didn't help. I really have no idea what I've done wrong. Any aid would be wonderful!
Lyqyd #2
Posted 12 May 2013 - 04:58 PM
Split into new topic.
The_Awe35 #3
Posted 12 May 2013 - 05:41 PM
Can you post the error code or what it is doing wrong please?
KaoS #4
Posted 12 May 2013 - 06:05 PM
firstly line 6. you need a space after while

also your if statements should be elseif or it will make nested if statements and require all of those irritating ends. EXAMPLE


if self.randomAttrib=="state1" then
  --code1
else
  if self.randomAttrib=="state2" then
    --code2
  end
end
is the same as

if self.randomAttrib=="state1" then
  --code1
elseif self.randomAttrib=="state2" then
  --code2
end
ilja #5
Posted 13 May 2013 - 04:44 AM
Thanks for helping me KaoS; the code looks better now, but the problem persists.

To clarify, the issue is that it should go downwards until it reaches Height 5, then it should stop going downwards and start going upwards until it reaches 81, then it should move 8 times and then it should start going downwards again. That's how it should behave.

The issue is that it doesn't stop and turn at height 5; so instead of going like:
Down 8
Down 7
Down 6
Down 5
Up 6
Up 7
Up 8

it goes
Down 6
Down 5
Down 4
Down 3
*snip*
Down -126
Down -127
and so on.
KaoS #6
Posted 13 May 2013 - 07:41 AM
You know I just thought I'd mention that I've seen a significant increase in the politeness of forum users as a whole (this is one example of it), there are a few exceptions but it is greatly appreciated

ANYWAYS :)/> could you please pastebin your code after the recent edit and I would like to test it personally
ilja #7
Posted 13 May 2013 - 10:02 AM
Sure!
http://pastebin.com/t5DJbyaD

Thanks a lot for helping, not every forum where people would go to that length as to test it :)/>
KaoS #8
Posted 13 May 2013 - 11:51 AM
this should sort it out


local Height = 80
local Direction = "Down"
local Move = 0
  while true do
   if Height < 6 and Direction=="Down" then
    Direction = "Up"
   elseif Height > 80 and Direction=="Up" then
    Direction = "Move"
   elseif Move == 4 and Direction=="Move" then
    Direction = "Down"
    Move=0
   end
   if Direction == "Down" then
    redstone.setBundledOutput("back", colors.blue)
    print (Direction)
    print (Height)
    sleep(0.2)
    redstone.setBundledOutput("back", 0)
    sleep(0.5)
    Height = Height - 1
   elseif Direction == "Up" then
    redstone.setBundledOutput("back", colors.brown)
    print (Direction)
    print (Height)
    sleep(0.2)
    redstone.setBundledOutput("back", 0)
    sleep(0.5)
    Height = Height + 1
   elseif Direction == "Move" then
    redstone.setBundledOutput("back", colors.green)
    print (Direction)
    print (Move)
    sleep(0.2)
    redstone.setBundledOutput("back", 0)
    sleep(0.5)
    Move = Move + 1
   end
end

you don't need to put local before a variable after the first time you define it as a local var