Question about string handling.

Previous topic - Next topic

Sokurah

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?
Website: Tardis remakes / Mostly remakes of Arcade and ZX Spectrum games. All freeware. :-)
Twitter: Sokurah

Kitty Hello

something with FORMAT$, maybe?

Ian Price

I've done this with a few of my GLB games, eg



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.

Code (glbasic) Select

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
I came. I saw. I played.

ampos

while len(a$)<8:a$="0"+a$:wend

MrTAToad

Most efficient way is :

PRINT RIGHT$("000000"+score%,len(6)),0,0

Been using that since the C64 :)

ampos

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...

Ian Price

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 came. I saw. I played.

hardyx

I think this sentence is the equivalent in GLBasic:

PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0

Ian Price

Quote
PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0

The winner so far :)
I came. I saw. I played.

Kitty Hello

I think the right$ thing might evaluate a tad faster, though.

Sokurah

#10
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,0

Mine, 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:
Website: Tardis remakes / Mostly remakes of Arcade and ZX Spectrum games. All freeware. :-)
Twitter: Sokurah

Moru

Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)

Sokurah

Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)

LOL, even shorter. I don't think it gets any better than this. ;)
Website: Tardis remakes / Mostly remakes of Arcade and ZX Spectrum games. All freeware. :-)
Twitter: Sokurah

MrTAToad

#13
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 :

Code (glbasic) Select
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)

MrTAToad

Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)
But what if you add or subtract a 0 and forget to update the number 6 ? :)