Bennu Game Development

English Forums => Helpdesk => Topic started by: cmunoz on June 25, 2014, 10:56:32 PM

Title: writing variable as 01 instead of 1
Post by: cmunoz on June 25, 2014, 10:56:32 PM
Hello everybody. This seems like a simple problem, but I haven't been able to get it to work. Basically what I want to do is write a variable to the screen so that if the number is less than two digits it displays a 0 in the tens place.


int myvariable=3;

write(0,160,120,4,(myvariable));


This results in the number "3" printed on screen. But what I want is for the number "03" to be printed on screen. I've tried various things such as myvariable+00 but I haven't had much luck.


I suppose I could do it like this....



if(myvariable<10)

write(0,160,120,4,"0"+(myvariable));
else
write(0,160,120,4,(myvariable));
end


But I have the feeling there's a much tidier way of going about it since that would get a bit sloppy the more variables are added.
Any ideas?
Title: Re:writing variable as 01 instead of 1
Post by: Breadcaster on May 18, 2016, 12:31:10 AM
Yeah I actually have a similar issue, I've got an ingame timer which is written on screen and I'd ideally like it to be written as ##:## (e.g. "03:25" rather than "3:25"), it just looks sorta... well, it serves its function but it just doesn't LOOK right. Is there any way to change/fix this?
Title: Re:writing variable as 01 instead of 1
Post by: Drumpi on May 18, 2016, 01:34:29 AM
As far as I know, no, that's the best way I think you can do it.
For a timer, you can write the numbers from right to left, and use three WRITE functions (remember to save the write ID to use with DELETE_TEXT)... or you can use a FPG with 10 graphs with every digit, and four process.
Title: Re:writing variable as 01 instead of 1
Post by: Breadcaster on June 15, 2016, 03:52:32 PM
Best way on this, I guess, is to have a statement like:

if (timer[0]>60);
    seconds++;
    timer[0]=0;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end

??
Title: Re:writing variable as 01 instead of 1
Post by: Drumpi on June 15, 2016, 07:14:24 PM
By the way, just check if timer is working OK. Last time I checked with a VERY OLD version of Bennu, timers aren't as accurate as a manual counter.
And maybe your code must be:

if (timer[0]>60);
    seconds++;
    timer[0] -= 60;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end


Or else you will be deprecating some hundredths seconds every 60 (timer is in hundredts of seconds, not seconds). Check the doc:
http://wiki.bennugd.org/index.php?title=Timer
Title: Re:writing variable as 01 instead of 1
Post by: SplinterGU on June 15, 2016, 09:21:48 PM
take a look at this


import "mod_say"
import "mod_string"

global
    int
    a;
begin

    a = 1;
    say(substr("0"+a, -2, 2));

    a = 4;
    say(substr("0"+a, -2, 2));

    a = 5;
    say(substr("0"+a, -2, 2));

    a = 12;
    say(substr("0"+a, -2, 2));

end
Title: Re:writing variable as 01 instead of 1
Post by: Breadcaster on July 03, 2016, 03:53:05 PM
Quote from: Drumpi on June 15, 2016, 07:14:24 PM
By the way, just check if timer is working OK. Last time I checked with a VERY OLD version of Bennu, timers aren't as accurate as a manual counter.
And maybe your code must be:

if (timer[0]>60);
    seconds++;
    timer[0] -= 60;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end


Or else you will be deprecating some hundredths seconds every 60 (timer is in hundredts of seconds, not seconds). Check the doc:
http://wiki.bennugd.org/index.php?title=Timer

Thank you so much Drumpi, this actually fixed a bug I've been having for quite a while!! I'm using a newer version of Bennu but this still proved useful.
Title: Re:writing variable as 01 instead of 1
Post by: Drumpi on July 05, 2016, 12:57:04 PM
Ok.
But be carefull, don't use write in one line and write_int in the next, jus t pick one (write is the best option, because you're gonna use delete_text in the next frame).