385 posts
Location
San Diego, CA
Posted 26 August 2015 - 10:13 PM
Disclaimer: yes I know that most people are well aware how to do this. This is a tutorial aimed at newbies who perhaps want this handy feature on older versions of ComputerCraft.Have you been wanting to use the handy dandy term.getTextColor() and term.getBackgroundColor() functions on older versions of ComputerCraft? Well, you can! And real simple too!
Simply add this code and have it run when the computer starts:
if _CC_VERSION == nil then
print("Patching term.getTextColor() and term.getBackgroundColor() in due to pre1.74 CC.")
sleep(0.1)
oldTermSetTextColor = term.setTextColor
oldTermSetBackColor = term.setBackgroundColor
currentTermGetTextColor = colors.white
currentTermGetBackColor = colors.black
function term.setTextColor(color)
oldTermSetTextColor(color)
currentTermGetTextColor = color
end
function term.setBackgroundColor(color)
oldTermSetBackColor(color)
currentTermGetBackColor = color
end
function term.getTextColor()
return currentTermGetTextColor
end
function term.getBackgroundColor()
return currentTermGetBackColor
end
end
EDIT: an explanation of how this works.
So basically, it checks if _CC_VERSION is nil (since term.getTextColor() and term.getBackgroundColor() were added at the same time as _CC_VERSION was added). If it is, it overrides the term.setTextColor and term.setBackgroundColor functions to store the colors they set in a variable (also backing up the originals so they can be called). Then, it creates two functions term.getTextColor() and term.getBackgroundColor() that access these variables and return them.
http://pastebin.com/hXymBQgc
Edited on 26 August 2015 - 11:15 PM
1023 posts
Posted 27 August 2015 - 12:06 AM
Any reason why it is all on the same line? Just looks a little suspicious, along with making it incredibly annoying to read.
It might also be better to add a pastebin link to this seeing as not all versions of the CC edit program provide support for pasting.
Edited on 26 August 2015 - 10:08 PM
385 posts
Location
San Diego, CA
Posted 27 August 2015 - 12:21 AM
Any reason why it is all on the same line? Just looks a little suspicious, along with making it incredibly annoying to read.
It might also be better to add a pastebin link to this seeing as not all versions of the CC edit program provide support for pasting.
A ) It's minified because it's easier to paste. Believe me I'm not linking malicious code in a tutorial.
B ) They do if you use the right control key :P/>
Edited on 26 August 2015 - 10:21 PM
1023 posts
Posted 27 August 2015 - 12:26 AM
Any reason why it is all on the same line? Just looks a little suspicious, along with making it incredibly annoying to read.
It might also be better to add a pastebin link to this seeing as not all versions of the CC edit program provide support for pasting.
A ) It's minified because it's easier to paste. Believe me I'm not linking malicious code in a tutorial.
B ) They do if you use the right control key :P/>
The code is unfortunately over 400 characters long, while the CC paste event can only handle 255 characters at a time. As i said it would be smarter to post a pastebin link.
797 posts
Posted 27 August 2015 - 12:31 AM
Or post it on two lines, split at the 255 character mark :P/>
This isn't really a tutorial as such, maybe give an explanation of how and why it works?
7083 posts
Location
Tasmania (AU)
Posted 27 August 2015 - 12:35 AM
I'm pretty sure the limit was lower once upon a time, but it seems up to 512 characters can be pasted under 1.74.
That said, "here's a line of code to paste" does not a "tutorial" make. Consider explaining why you'd want the code, what it does, and how it works.
1023 posts
Posted 27 August 2015 - 01:10 AM
I decided to go look up the paste event. So the event was added in CC 1.6, and at that time could only handle 128 characters. I believe it was increased in 1.74.
*still suggests pastebin link*
Edited on 26 August 2015 - 11:10 PM
385 posts
Location
San Diego, CA
Posted 27 August 2015 - 01:13 AM
Okie dokie. I made it a bit more tutorial like. Let me put it on pastebin…
1140 posts
Location
Kaunas, Lithuania
Posted 27 August 2015 - 11:35 AM
I wouldn't count this as a tutorial, rather a code snippet, which should probably go to somewhere like Utilities subforum. A tutorial should teach people something, not give some code and explain it in a few short sentences. I also don't like that this encourages people to use sleep()'s, global variables and un-indented code. Rather than checking for a constant, you could simply be checking for non-existent term functions.
385 posts
Location
San Diego, CA
Posted 28 August 2015 - 06:36 PM
Yeah this tutorial is crappy, I'm done posting tutorials since clearly I can't do them well. Lol.