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

A Server Intro

Started by twitch204, 08 April 2013 - 05:03 AM
twitch204 #1
Posted 08 April 2013 - 07:03 AM
Hello All,
I am very newish to Lua/ComputerCraft and have been trying to learn the basic's on and off the past couple of month. I am trying to help a friend make a server intro for an FTB server he is opening, and I am having some issues understanding colored text and how to center text. on a 5 in length, and 2 in height monitor set-up. Was wondering if someone could help me. There is more text I would like to add, but I cant seem to even put this in right.
SuicidalSTDz #2
Posted 08 April 2013 - 07:13 AM
Centering Text with a function:

local w,h = term.getSize()
local function centerText(text,ypos,colour)
 term.setCursorPos((w-#text)/2,ypos)
 term.setTextColour(colour)
 term.write(text)
end

How to call the function:

centerText("Hello World",1,colours.lime)

The output:

Hello World


How it works:
We capture three arguemts when we call the centerText function
text, ypos, and colour

We now have things to work with. First we set the cursor pos to width - the number of characters in the text divided by two – this centers the text. Next we set the text colour using the arguement colour called with the function. Lastly, we write the text to the screen.


EDIT: This works with any monitor size since we capture the width and height of the terminal in the variables w and h.
twitch204 #3
Posted 08 April 2013 - 10:34 AM
What do you mean by "call the function"? Sorry my inexpericance here.
SuicidalSTDz #4
Posted 08 April 2013 - 10:39 AM
You know how you use term.setCursorPos(x,y), yeah well that is a function call and the arguements are x and y. In this case we call the function centerText and provide three arguements for it to use.

So anytime you need to use this function just use the below code with your specific arguements filled out, being the text,y,and colour.

centerText(text,y,colour)

You must provide all three arguements or else the function will not run properly.
madbrad21 #5
Posted 08 April 2013 - 10:40 AM
Call the function means tell it to run the function just like in his example of how to call the function. centerText() is the function and what it is centering is what is between the ().
SuicidalSTDz #6
Posted 08 April 2013 - 10:48 AM
I will post another Example since I know this can be confusing for new users:

centerText("This is centered!", 14, colours.blue)

This will center the text "This is centered!" on line 14 in blue.

This is centered!
Smiley43210 #7
Posted 08 April 2013 - 01:28 PM
You ought to add 1 to the value after you subtract and divide, since the value returned without adding 1 is the xpos BEFORE it should start writing.
Ex.
Terminal is 5 wide
Text is 3 long
(5-3)/2 = 1
Starts printing at (1, ypos), and ends at (3, ypos)


local w,h = term.getSize()
local function centerText(text,ypos,colour)
term.setCursorPos(((w-#text)/2)+1,ypos)
term.setTextColor(colour)
term.write(text)
end
Also changed the term.setTextColour() to term.setTextColor(), as I believe the former is non-existant.
SuicidalSTDz #8
Posted 08 April 2013 - 02:02 PM
Also changed the term.setTextColour() to term.setTextColor(), as I believe the former is non-existant.
Ah, silly Americans :P/>
It really just depends how you learned to spell it. Like grey :)/>
Smiley43210 #9
Posted 09 April 2013 - 01:11 AM
Also changed the term.setTextColour() to term.setTextColor(), as I believe the former is non-existant.
Ah, silly Americans :P/>
It really just depends how you learned to spell it. Like grey :)/>
Oh, I thought the only one that had two different spellings was the colors/colours api (and gray/grey).
Imque #10
Posted 09 April 2013 - 02:15 AM

term.setCursorPos(((w-#text)/2)+1,ypos)

Your calling the length of a string using a pound sign? (#)
SuicidalSTDz #11
Posted 09 April 2013 - 02:17 AM
Yes here are many ways. #, string.len, etc.