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

Refueling script is silent

Started by WelshHeron, 30 May 2014 - 11:04 PM
WelshHeron #1
Posted 31 May 2014 - 01:04 AM
Background Knowledge:
I'm 100% new to Lua, and relatively new to the practical side of programming. I basically just threw myself into CC a few hours ago, and threw together this script as a test-run.

The script's purpose is to detect how much fuel a turtle requires, and then to fill it up with the appropriate number of Coal Blocks (800 units of fuel)

The Script:

local y = turtle.getFuelLevel()
local totalFuelCap = 100000
local a = True
local slot = 1
y = (totalFuelCap - y)
--[[ COAL BLOCKS ]]--
remainder = y % 800
y = (y - remainder) / 800
if y ~= 0 then
  turtle.select(slot)

  while a do
	if y <= 64 and y > 0 then
	  turtle.refuel(y)
	  print("All filled up!")
	  a = False
	elseif y > 64 then
	  turtle.refuel()
	else
	  print("Left your zipper open.")
	  a = False
	end
  
	slot = slot + 1
	turtle.select(slot)
  end
else
  print("The tank's already full!")
end
turtle.select(1)

My Problem:
I like debugging. And I've already had to debug a few little errors I made because of my inexperience with Lua. BUT, when I run this script, I get no response from the console. It seems to run, but nothing happens…which seems impossible, because as I see it, every possible route the program could have taken would have resulted in SOME sort of output to the console.

What I've Done:
I've looked it over a couple dozen times and swapped some stuff around and tried commenting out a few things, but I always end up back here. I can google and backtrack if I've got an error, but I'm kind of at a loss with this one, since it looks perfectly fine to me. I figured with a program this simple, it's most likely blatantly obvious for someone more experienced (either in Lua or in CC ), so I figured I'd ask here for help.

Anyways, thanks in advance.

P.S. Oh, and on an unrelated note, how does one make a spoiler? This site seems to be using unconventional bbcode for the spoiler tag (or it just doesn't show up in the 'Preview Post'…which is possible too).
Bubba #2
Posted 31 May 2014 - 01:23 AM
You're code looks very good for someone who's only been into it for a few hours :)/> however, you do have an issue with your while loop.


a = True
...
while a do
   ....
end

Note the capital letter in "True". In Lua, booleans (true/false) are lowercase. What this means is that your program sees a as "nil" and your while loop is skipped over entirely, and in the process all of the printed statements are skipped as well.

As to the spoiler, you can simply use:

[spoiler]
Content here
[/spoiler]

SpoilerHello, world!
Edited on 30 May 2014 - 11:41 PM
WelshHeron #3
Posted 31 May 2014 - 02:15 AM
Thank you! I guess book knowledge translates to practical knowledge a bit easier than I imagined it would.

Ah, I see…that makes sense. Thanks for the help…I'm glad it was that simple. I tried it, and it works like a charm now!