Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ health bar not displaying right

health bar not displaying right

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Joeyfogas   (41 posts)  Bio
Date Wed 21 Mar 2018 05:47 AM (UTC)

Amended on Wed 21 Mar 2018 05:49 AM (UTC) by Joeyfogas

Message
can someone help out and tell me why this code doesn't display right?

it is supposed to be a healthbar based on health percentage



send_to_char("[",ch);
int value;
value = ch->hit/ch->max_hit ;
for (start = 1; start <= 100; start++ )

{
if (start <= value * 100)
send_to_char("|",ch);
else
send_to_char(" ",ch);
}
send_to_char("] \r",ch);

it seems to only display blank unless I am at 100% health.

output at 50% should resemble:
[||||||||||||||| ]
Top

Posted by Joeyfogas   (41 posts)  Bio
Date Reply #1 on Wed 21 Mar 2018 05:49 AM (UTC)
Message

output at 50% should resemble:
[|||||||||||||||               ]


i guess the forum didnt like all the spaces... won't let me edit it seems either
Top

Posted by Joeyfogas   (41 posts)  Bio
Date Reply #2 on Wed 21 Mar 2018 06:10 AM (UTC)

Amended on Wed 21 Mar 2018 06:39 AM (UTC) by Nick Gammon

Message
i realize the written code was ugly so I cleaned it up.. it now reads:


send_to_char("[",ch);
int value = 100 * (ch->hit/ch->max_hit) ;
   for (start = 1; start <= 100; start++ )
     {
     if (start <= value )
       send_to_char("|",ch);
     else
       send_to_char(" ",ch);
    }
   send_to_char("]  \r",ch);



but same issue
Top

Posted by Nick Gammon   Australia  (23,169 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 21 Mar 2018 06:42 AM (UTC)
Message
An integer only holds whole numbers, so for example if hit is 50 and max_hit is 100, then hit/max_hit will be zero (until hit is the same as max_hit).

You might want to work with floats here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Joeyfogas   (41 posts)  Bio
Date Reply #4 on Wed 21 Mar 2018 07:13 AM (UTC)
Message

send_to_char("[",ch);
float value = 100 * (ch->hit/ch->max_hit) ;
                          for (start = 1; start <= 100; start++ )

                                {
                          if (start <= value )
                                        send_to_char("|",ch);
                                  else
                                        send_to_char(" ",ch);
                                }
                    send_to_char("]  \r",ch);



still gives same outcome:

----------------------------------------------------------------------------
You are a level 65 Elf Adventurer from O'ran Thalore.
Your TIER rank is 0.

STR : 10 Attack: 0 Health: 300/600
[
]
AGI : 180 Defense: 412
INT : 180 M. Def: 0
LCK : 180



(disregard the obvious need for a length of output fix.. more focused on getting it working first)
Top

Posted by Nick Gammon   Australia  (23,169 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 21 Mar 2018 07:38 AM (UTC)
Message
See this: http://www.gammon.com.au/forum/?id=12146

Making it float is too late.

Try changing 100 to 100.0

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Joeyfogas   (41 posts)  Bio
Date Reply #6 on Wed 21 Mar 2018 07:47 AM (UTC)
Message
nope :(

didn't work
Top

Posted by Nick Gammon   Australia  (23,169 posts)  Bio   Forum Administrator
Date Reply #7 on Wed 21 Mar 2018 08:27 AM (UTC)

Amended on Wed 21 Mar 2018 08:28 AM (UTC) by Nick Gammon

Message
OK, the brackets didn't help. Try:


float value = 100.0 * ((float) ch->hit / (float) ch->max_hit) ;

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Joeyfogas   (41 posts)  Bio
Date Reply #8 on Wed 21 Mar 2018 08:40 AM (UTC)
Message
woohoo! that got it. THANK YOU!!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,149 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.