In BMax I normally use this when printing the score on the screen;
DRAWTEXT ("score: " + Replace:String(RSet:String(Score, 7), " ", "0"), 1, 1)
This example will print the score, aligned to the right, and padded with zeros on the left to be 7 characters wide.
Is there a way to do the same, as efficiently, in GLB?
something with FORMAT$, maybe?
I've done this with a few of my GLB games, eg
(http://www.iprice.remakes.org/my_stuff/bli2x.PNG)
but there's no method as efficient as your BlitzMax example.
I use LEN to find length of the score and used a FOR/NEXT loop to add the leading zeros - I use something like this, but with a proper bitmap font routine.
GLOBAL sc=1
WHILE TRUE
FOR n=0 TO 8-LEN(sc)
PRINT "0",n*10,10
NEXT
PRINT sc,(9-LEN(sc))*10,10
INC sc
SHOWSCREEN
WEND
while len(a$)<8:a$="0"+a$:wend
Most efficient way is :
PRINT RIGHT$("000000"+score%,len(6)),0,0
Been using that since the C64 :)
Quote from: MrTAToad on 2010-Sep-30
Most efficient way is :
PRINT RIGHT$("000000"+score%,len(6)),0,0
Been using that since the C64 :)
Lol, you are winning the competition...
RIGHT$ didn't exist in GLB until recently, so I got used to using a different method.
At least mine actually works MrTaToad (test yours) ;)
I think this sentence is the equivalent in GLBasic:
PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0
Quote
PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0
The winner so far :)
I think the right$ thing might evaluate a tad faster, though.
Quote from: MrTAToad on 2010-Sep-30
Most efficient way is :
PRINT RIGHT$("000000"+score%,len(6)),0,0
Been using that since the C64 :)
Haha, I thought there wouldn't be an answer so I just made this, then came here to post it and saw there was 100 replies. =D
PRINT (LEFT$("000000",6-LEN(Score%))+Score%),0,0Mine, compared to yours, also has the bonus of actually working. With a score of 8192 your example just prints 2.
Edit: Great. I ended up solving it my own way, but thanks for the help everybody. And MrTAToad, I'll try not to say something funny about how things related to the C64 don't work. Oops. =D :whistle:
Does it have to be so many complicated solutions? ;)
RIGHT$("000000"+score, 6)
Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions? ;)
RIGHT$("000000"+score, 6)
LOL, even shorter. I don't think it gets any better than this. ;)
QuoteAt least mine actually works MrTaToad (test yours)
Yes, Mr Smarty-pants - there was a slight mistake in the code :D
It should really be something like :
SCORE_PAD$="000000"
PRINT RIGHT$(SCORE_PAD$+score%,LEN(SCORE_PAD$)),0,0
Actually, I used to use RIGHT$ as a function which just used MID$ (as does the official command)
Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions? ;)
RIGHT$("000000"+score, 6)
But what if you add or subtract a 0 and forget to update the number 6 ? :)
Quote from: Kitty Hello on 2010-Sep-30
I think the right$ thing might evaluate a tad faster, though.
How about the speed of MID$ command?
left$, right$, mid$ are basically the same command. The replace might be a tad slower when the string lengths of needle$ and haystack$ are not equal.
The problem with most of your function is/was that numbers converted to strings used to have a space in front (the hidden + (positive) sign) so you used to do:
A$=right$(str$(n),Len(str$(n))-1) just to remove this blank, on the c64 and others.
Well, the C64 didn't do automatic type conversion in BASIC :)