BIOS

Functionprint
Prints any number of arguments (space separated) at the current cursor position in the current term. All the arguments will automatically be converted to a string using the tostring function, and concatenated, separated by spaces. This function automatically moves to the next line after writing. As well as this, it performs text wrapping, meaning if the string is too big for the term, it will continue writing on the next line. print returns the number of lines it printed.
Syntax print(
  • toPrint... : any
)
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExamplePrint a basic string
Prints "Hello, world!" to the current term.
Code
<nowiki>
print("Hello, world!")
    </nowiki>
Output
Hello, world!
ExamplePrint multiple strings
Prints "foo bar" to the current term.
Code
<nowiki>
print("foo", "bar")
    </nowiki>
Output
foo bar
ExamplePrint multiple types
Prints strings and numbers to the current term in a single print call.
Code
<nowiki>
local cats = 5
local dogs = 3
print("Cats:", cats, "Dogs:", dogs)
    </nowiki>
Output
Cats: 5 Dogs: 3
FunctionprintError
Identical to print, however the arguments are printed in red, like an error.
Syntax printError(
  • toPrint... : any
)
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExamplePrint a basic string
Prints "Hello, world!" to the current term in red.
Code
<nowiki>
printError("Hello, world!")
    </nowiki>
Output
Hello, world!
ExamplePrint multiple strings
Prints "foo bar" to the current term in red.
Code
<nowiki>
printError("foo", "bar")
    </nowiki>
Output
foo bar
ExamplePrint multiple types
Prints strings and numbers to the current term in red, in a single print call.
Code
<nowiki>
local cats = 5
local dogs = 3
printError("Cats:", cats, "Dogs:", dogs)
    </nowiki>
Output
Cats: 5 Dogs: 3
Functionread
Reads a string of text at the current cursor position in the current term. typingCharacter will automatically be converted to a string using the tostring function. This function moves to the next line after the user presses enter. Optionally, typingCharacter can be specified so that the user's input is hidden and typingCharacter is written in each typed character's place. read returns the string the user entered. Using the history parameter, it is possible to provide a history of previous commands entered (as an array of strings), so that the user can use the up and down arrow keys to browse through them.
Syntax read(
  • typingCharacter? : string
  • history? : table
  • completeFunction? : function
  • defaultText? : string
)
Returns string textEntered
Part of CC:Tweaked (source)
API BIOS globals
ExampleRead a string of text
Reads a string of user entered text (`Hello World!`) in the current term, assigns it to a variable and then prints it.
Code
<nowiki>
local userInput = read()
print(userInput)
    </nowiki>
Output
Hello World!
ExampleRead a hidden string of text
Reads a string of user entered text (`Hello World!`) in the current term whilst keeping what the user is writing hidden (`************`), assigns it to a variable and then prints it.
Code
<nowiki>
local userInput = read("*")
print(userInput)
    </nowiki>
Output
Hello World!
Functionsleep
Pauses execution of the program for time seconds. As it waits for a fixed amount of ticks, time will automatically be rounded up to the nearest multiple of 0.05 seconds. If you are using coroutines or the parallel API, it will only pause execution of the current thread, not the whole program.

 Note: Because sleep internally uses timers, it is a function that yields. This means that you can use it to prevent "Too long without yielding" errors, however, as the minimum sleep time is 0.05 seconds, it will slow your program down.

Warning: The sleep function internally queues and waits for a timer event, however it does not listen for any other events. This means that any event that occurs while sleeping will be entirely discarded. If you need to receive events while sleeping, consider using timers.
Syntax sleep(
  • time : number
)
Returns nil
Part of CC:Tweaked (source)
API BIOS globals
ExampleSleep for a few seconds
Prints "Hello", waits 2 seconds, and then prints "world!".
Code
<nowiki>
print("Hello")
sleep(2)
print("world!")
    </nowiki>
Output
<nowiki>Hello
world!</nowiki>
ExampleSleep for the minimum amount of time
Writes 100 random numbers, one every tick.
Code
<nowiki>
for i = 1, 100 do -- Run this 100 times
	local x, y = term.getCursorPos() 
	term.setCursorPos(1, y) -- Go back to the start of the line
	term.clearLine()
	write(math.random())
	sleep(0.05)
end
    </nowiki>
Functionwrite
Writes text at the current cursor position in the current term. Unlike print, this function does not automatically move to the next line after writing, however it does perform text wrapping, meaning if the string is too big for the term, it will continue writing on the next line. write returns the number of extra lines it printed (i.e. if it did not need to wrap to the next line, it will return 0).
Syntax write(
  • text : string
)
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExampleWrite to the term
Writes "Hello, world!" to the current term.
Code
<nowiki>
write("Hello, world!")
    </nowiki>
Output
Hello, world!
ExampleWrite multiple strings the term
Writes "foobar" to the current term. Note how there is no space between writes.
Code
<nowiki>
write("foo")
write("bar")
    </nowiki>
Output
foobar
ExampleWrite a very long string
Writes "foo" to the current term 50 times. Note how it automatically wraps to the next line where necessary.
Code
<nowiki>
write(("foo "):rep(50))
    </nowiki>
Output
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.