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

Loading Bar

Started by HiddenShadow55, 12 August 2012 - 08:26 PM
HiddenShadow55 #1
Posted 12 August 2012 - 10:26 PM
In my code for a password door i want to add a loading bar. At the moment this is the code for the loading bar i want.

textutils.slowPrint("<=======================>", 10)
textutils.slowPrint(" Processing ", 10)
textutils.slowPrint("<=======================>", 10)

I was wondering if there was any way for me to have all three of these appear on the screen at the same time on separate lines. I have been unable to figure it out.

Help is appreciated greatly.
Ponder #2
Posted 13 August 2012 - 12:14 AM
Have a look at the Term Api. Especially the getCursorPos and setCursorPos methods together with string indexing (which is a little bit awkward in Lua) you can do something like this:


local DELAY = 0.1
local LOADING_BAR = "<=======================>"
local LOADING_TEXT = "Processing"
local LOAD = {
	LOADING_BAR,
	LOADING_TEXT,
	LOADING_BAR,
}

function writePos (char, x, y)
-- moves the cursor to the given position and writes the character $char
  term.setCursorPos (x, y)
  term.write (char)
end

function multiSlowPrint (string_table, delay)
-- string_table holds the strings which shall be printed slow across several lines, each string representing one line; delay is the time to wait between each step of the function

  local start_x, start_y = term.getCursorPos ()
  local size_x, size_y  = term.getSize ()			

  local longest_x = 0  -- we need this function to know when we have reached the end of the given strings
  for i = 1, #string_table do
	local len = #string_table [i]
	if len > longest_x then
	  longest_x = len
	end
  end

  for x = 0, size_x - 1 do
	if x >= longest_x then
	  term.setCursosPos (0, start_y + #string_table + 1)
	  break   -- if we have reached the rightmost position of our given string we want to abort the function
	end
	for y = 0, #string_table - 1 do
  
	local char = string_table [y + 1]:sub (x + 1, x +1)  -- you can read about string indexing in Lua here http://lua-users.org/wiki/StringIndexing
	if char nil == then
	  char = " "
	end

	writePos (char, x, y)

   end
  sleep (delay)
  end
end

multiSlowPrint (LOAD, DELAY)

That should be it, it's not perfect, but it should give you the idea how to do it. If you got questions just ask.