Autor Tema: writing variable as 01 instead of 1  (Leído 3020 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado cmunoz

  • Newbie
  • *
  • Mensajes: 42
  • Karma: 0
  • Sexo: Masculino
    • Ver Perfil
writing variable as 01 instead of 1
« en: Junio 25, 2014, 02: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?
@thistof

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 40
  • Karma: 2
    • Ver Perfil
Re:writing variable as 01 instead of 1
« Respuesta #1 en: Mayo 17, 2016, 04:31:10 pm »
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?

Desconectado Drumpi

  • Hero Member
  • *****
  • Mensajes: 6139
  • Karma: 157
  • Sexo: Masculino
  • Odio el periodo "entre proyectos"
    • Ver Perfil
    • La web de Drumpi
Re:writing variable as 01 instead of 1
« Respuesta #2 en: Mayo 17, 2016, 05:34:29 pm »
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.
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 40
  • Karma: 2
    • Ver Perfil
Re:writing variable as 01 instead of 1
« Respuesta #3 en: Junio 15, 2016, 07:52:32 am »
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

??

Desconectado Drumpi

  • Hero Member
  • *****
  • Mensajes: 6139
  • Karma: 157
  • Sexo: Masculino
  • Odio el periodo "entre proyectos"
    • Ver Perfil
    • La web de Drumpi
Re:writing variable as 01 instead of 1
« Respuesta #4 en: Junio 15, 2016, 11:14:24 am »
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:

Código: [Seleccionar]
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
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

Conectado SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12515
  • Karma: 369
  • Sexo: Masculino
    • Ver Perfil
Re:writing variable as 01 instead of 1
« Respuesta #5 en: Junio 15, 2016, 01:21:48 pm »
take a look at this

Código: [Seleccionar]
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
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 40
  • Karma: 2
    • Ver Perfil
Re:writing variable as 01 instead of 1
« Respuesta #6 en: Julio 03, 2016, 07:53:05 am »
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:

Código: [Seleccionar]
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.

Desconectado Drumpi

  • Hero Member
  • *****
  • Mensajes: 6139
  • Karma: 157
  • Sexo: Masculino
  • Odio el periodo "entre proyectos"
    • Ver Perfil
    • La web de Drumpi
Re:writing variable as 01 instead of 1
« Respuesta #7 en: Julio 05, 2016, 04:57:04 am »
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).
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)