Please Delete this post, my problem was solved

Started by
13 comments, last by Morpheus011 17 years, 8 months ago
Im adding a heathmeter to this fps im working on. I know for sure that this is the only part that the compiler doesnt like. float health = m_playerManager->GetLocalPlayer()->GetHealth(); char healthcasted = (char) health; m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700); I know render takes char, so i casted it and it says error C2664: 'Font::Render' : cannot convert parameter l from 'char' to 'char *' I'm a noob so im sorry. Thankyou for any responses. [Edited by - PhilipNguyen on August 18, 2006 7:13:25 PM]
Advertisement
not really a rendering problem, the function you're calling is expecting the first parameter to be a char *, and you're passing it a char. you can either pass the address of the variable using the address of operator (&) or you can pass it a true character pointer.
Quote:Original post by PhilipNguyen
Im adding a heathmeter to this fps im working on. I know for sure that this is the only part that the compiler doesnt like.

float health = m_playerManager->GetLocalPlayer()->GetHealth();
char healthcasted = (char) health;

m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700);

I know render takes char, so i casted it and it says
error C2664: 'Font::Render' : cannot convert parameter l from 'char' to 'char *'

I'm a noob so im sorry. Thankyou for any responses.


Even then, casting a float to a char doesn't really give you any information. If healthcasted needs to be a string, ie. char* you better use sprintf.

untested:
float health = m_playerManager->GetLocalPlayer()->GetHealth();char healthcasted[6] // expecting 100.0 at most//now put the float into the string using format 3.1 => 0.0 to 100.0sprintf(healthcasted, "%3.1f", health);m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700);


hope that works for you.
Morpheus, I changed this line of code
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);
The screen displayed a bunch of characters, like BQBS3

Limitz, I changed the program like you said and it compiles but crashes when i get into the game state. It says the heathcasted is corrupt

Thankyou for responding
I meant to say that the healthmeter says a bunch of characters like BQBS44, not 100.
&healthcasted is a char**

#include <sstream>std::stringstream health<<m_playerManager->GetLocalPlayer()->GetHealth();m_scoreBoardFont->Render(health.str(), 30, 0, 0xFFFF7700);[edit]it may require std::stringstream health;health<<m_playerManager->GetLocalPlayer()->GetHealth();
Quote:Original post by PhilipNguyen
Morpheus, I changed this line of code
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);
The screen displayed a bunch of characters, like BQBS3

Limitz, I changed the program like you said and it compiles but crashes when i get into the game state. It says the heathcasted is corrupt

Thankyou for responding


are you sure you removed the & before healthcasted in the Render function using my method? It should do the same as dmail's code...

Greetings

EDIT: ofcourse you did, you said it compiled... silly me :)
EDIT: is that your own renderfunction? if it sais it is corrupt because it gets its input in the format 100.0 and you need 100 you may replace %3.1f by %3.0f.
Render() seems to take a null-terminated string of characters, and draws those. If you don't know what a null-terminated string is, you should probably go learn (check out a basic C tutorial, or the function strcpy(), for example).

The reason you get lots of characters when passing the pointer to the character, is that there is no null after the character to terminate the string. Further, the character itself is a single ASCII character with the value you're setting it to -- not the characters that make up the number you want to render. For that, you need sprintf().

Thus, your code should look like this:

float health = m_playerManager->GetLocalPlayer()->GetHealth();char buf[30];sprintf(buf, "%.1f", health);m_scoreBoardFont->Render(buf, 30, 0, 0xFFFF7700);


This will print health with one decimal of precision.
enum Bool { True, False, FileNotFound };
God bless everyone,

thank you hplus because that got my health bar running. I'll be sure to take your advice.

Thankyou everyone
Quote:Original post by dmail
&healthcasted is a char**

*** Source Snippet Removed ***


He's creating a char called healthcasted. &healthcasted is therefore the address of the variable healthcasted, i.e. a char *, not a double pointer. If healthcasted was a char *, then you'd be right.

This topic is closed to new replies.

Advertisement