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

[Math help] Loading screen(not fake loading)

Started by Cranium, 01 October 2012 - 05:40 PM
Cranium #1
Posted 01 October 2012 - 07:40 PM
I'm trying to write a function that allows the user to input the name of the program, and the percentage of install completion, so that those with silly fake loading screens will have something to fall back on. Right now though, I'm stuck with the math. Teacher always said I would one day need to use this, but I never did listen…
Here's my (probably derpy) function right now. It does use a custom API that I wrote that just prints in the center of the screen, so pay that no mind.

local function INloading(program,percent)
term.clear()
term.setCursorPos(1,1)
print(".-----------------------------------------------.")
print("|				 Installing				    |")
print("|											   |")
smartWrite.writeC(program,0,3)
print("|											   |")
smartWrite.writeC("Percent Complete: "..math.floor(percent).."%",0,4)
print("| +-------------------------------------------+ |")
print("| |										   | |")
for i = 1,percent,.1 do
  term.setCursorPos(i+3,6)
  write("/")
end
print("| +-------------------------------------------+ |")
print("'-----------------------------------------------'")
end
It's supposed to display a box as wide as the screen is, with it displaying the string name of the program, and the percentage of completion. The percentage can be entered as fractions, such as for a program that has three components, entering (1/3), and it would do 33.33%, and so on…
So far though, it will overfill the loading bar, because the screen is much less than 100 characters wide.
Any help would be appreciated.
GopherAtl #2
Posted 01 October 2012 - 08:04 PM
A note, percentages are displayed as, ex, 50%, but the math value of 50% is just 0.5, not 50, just as 1/3 is .33, which is 33%. So if you want to be able to take 1/3, 2/3, etc. you'll need to assume the percent parameter is a number between 0 and 1. So that math.floor(percent) in there will be making any valid percentage in that range into 0. Prolly wanna do math.floor(percent*100) there instead.

That said, just multiply that percent by the maximum with of your progress meter, ex:


local meterChars=percent*10 --scale percent to 0-10 char long meter
print("+----------+") --12 - 10 "-" plus 2 "+"s at ends
print("|"..string.rep("*",meterChars)..string.rep(" ",10-meterChars).."|")
print("+----------+")
Also, string.rep. Useful.
Cranium #3
Posted 01 October 2012 - 08:27 PM
I am super illiterate when it comes to the lua String Library. I had to look it up(again) to find out what string.rep did….For some reason, I had it in my head that it did "replace" instead of "repeat"… :)/>/>
Here's my revised function:

local function INloading(program,percent)
local meterChars=percent*10
term.clear()
term.setCursorPos(1,1)
print(".-----------------------------------------------.")
print("|				 Installing				    |")
print("|											   |")
smartWrite.writeC(program,0,3)
print("|											   |")
smartWrite.writeC("Percent Complete: "..math.floor(percent*100).."%",0,4)
print("| +-------------------------------------------+ |")
print("| |"..string.rep("/",meterChars)..string.rep(" ",43-meterChars).."| |")
print("| +-------------------------------------------+ |")
print("'-----------------------------------------------'")
end
GopherAtl #4
Posted 01 October 2012 - 08:31 PM
ehrm. Well, I used percent*10 because my meter was 10 chars wide. You should use percent*43, since yours is 43 chars wide…:)/>/>
Cranium #5
Posted 01 October 2012 - 08:45 PM
Whoops…forgot to change that line. But thank you, I think I'll use string.rep a lot more now… :)/>/>
jag #6
Posted 01 October 2012 - 08:48 PM
Wouldn't this function be kind of "blinky" because you are clearing the entire screen each time…?
Cranium #7
Posted 01 October 2012 - 08:51 PM
Nope. Since it prints the same thing everytime, you wouldn't even notice the change. Not unles your computer is super laggy…
jag #8
Posted 01 October 2012 - 08:56 PM
Well sometimes when I do term.clear() and a print() or write() in a loop I get this weird blinking effect, and I always test my scripts in singleplayer (and it's not lag that is causing it).
I usually fix it by just changing the line that you need to change, not all of them.
Cranium #9
Posted 01 October 2012 - 09:01 PM
I think singleplayer has a bit of a problem handling code like this. I have noticed this, but since I do most of my code for servers, I never see that. Normally, it updates instantly. But I do know that the way that I am calling it, there is some significant, noticable time between each call(I'm using the HTTP API to get some information, and then saving that to a file).
I always strive for everything to look as good as possible, but sometimes, we can't get that…. I do know that computers like this in real life have that little blinking effect sometimes too.
hego555 #10
Posted 04 October 2012 - 07:24 AM
Isnt this the loading screen you used on the server?

It was cool, but goes so fast that it was kinda un-needed!
jag #11
Posted 04 October 2012 - 09:09 AM
Isnt this the loading screen you used on the server?

It was cool, but goes so fast that it was kinda un-needed!
Well if you got like a giant OS then this is kind of necessary
hego555 #12
Posted 04 October 2012 - 03:45 PM
Isnt this the loading screen you used on the server?

It was cool, but goes so fast that it was kinda un-needed!
Well if you got like a giant OS then this is kind of necessary

In the case of that yes, but it has to be pretty big!
Cranium #13
Posted 04 October 2012 - 05:09 PM
Um…."Go big or go home"
My loading screen is for being able to download any repository combination I specify in a certain table. So having it display a status would be nice. Especially if there is lag on the server. If they cannot download right away, and they do not see a status, or an update it would be nice to know that Something is being done, rather than just sitting there.
hego555 #14
Posted 04 October 2012 - 11:42 PM
Um…."Go big or go home"
My loading screen is for being able to download any repository combination I specify in a certain table. So having it display a status would be nice. Especially if there is lag on the server. If they cannot download right away, and they do not see a status, or an update it would be nice to know that Something is being done, rather than just sitting there.

I thought it was just a private download script for one program!
Cranium #15
Posted 04 October 2012 - 11:53 PM
Ha! No, it is going to be huge, if I ever get it all working together.
hego555 #16
Posted 05 October 2012 - 12:44 AM
Well when you get a chance help me with that!

I coded a money system in-game its in Beta right now :(/>/>
Cranium #17
Posted 05 October 2012 - 01:01 AM
Kinda obsessed with Borderlands 2 at the monent, when I get away from it, I'll hop on the server.
jag #18
Posted 05 October 2012 - 07:25 AM
Kinda obsessed with Borderlands 2 at the monent, when I get away from it, I'll hop on the server.
Wich server is "the server"?
Cranium #19
Posted 05 October 2012 - 04:28 PM
The server is the one that Hego0555 hosts. There is a post in the servers section if you are interested.