V3: "Null image reference" error
Displaying 1-10 of 10 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Almostpeaceful74

So I'm a total n00b and aware that this may be a stupid question, but I'm trying to create a HUD with a healthbar that changes with the player's health, but when I try to Tblit() the healthbar I get an error that says:

ImageForHandle() - Null image reference (0), probably an uninitialized image handle:

drawHUD - system.vc(22)

I've looked through the FAQ and the forums but I can't find anything, could someone please help a noob in trouble?

Posted on 2010-02-18 12:38:09

ustor

This is telling you that the "image" you're trying to give it doesn't exist. Make sure you're loading an image like:

int myimage;
myimage = LoadImage("image.pcx");

Then myimage would contain an integer handle which you need to pass to TBlit like so:

TBlit(0, 0, myimage, screen);

Make sure the parameters are in the right order too. Hopefully that helps, if not let me know and we'll try another approach.

Posted on 2010-02-18 17:24:46

Almostpeaceful74

thanks,
I put in a different function to assign a value to the integer (a different healthbar depending on what the player's health is)
and that seems to have fixed it, except now the game is really laggy. Any further suggestions?

this is the code for the whole thing, including the image integer check function:


int HUD = 1;
int imageHUD = LoadImage( "HUD.bmp" );
int healthbar;


void Autoexec()
{
SetAppName("EARTH");
Titlescreen("EARTH: A PROLOGUE");
Map("village.map");
showHUD();
}

void showHUD()
{
HookRetrace("drawHUD");
}

void drawHUD()
{
if(HUD = 1)
{
TBlit( 0,0, imageHUD, screen );
checkhealthbar();
TBlit( 0,0, healthbar, screen );
}
}

void checkhealthbar()
{
if (playerhealth >= 100)
{
healthbar = LoadImage( "hp100.bmp" );
}
else if (playerhealth >= 90 and playerhealth <100)
{
healthbar = LoadImage( "hp90.bmp" );
}
else if (playerhealth >= 80 and playerhealth <90)
{
healthbar = LoadImage( "hp80.bmp" );
}
else if (playerhealth >= 70 and playerhealth <80)
{
healthbar = LoadImage( "hp70.bmp" );
}
else if (playerhealth >= 60 and playerhealth <70)
{
healthbar = LoadImage( "hp60.bmp" );
}
else if (playerhealth >= 50 and playerhealth <60)
{
healthbar = LoadImage( "hp50.bmp" );
}
else if (playerhealth >= 40 and playerhealth <50)
{
healthbar = LoadImage( "hp40.bmp" );
}
else if (playerhealth >= 30 and playerhealth <40)
{
healthbar = LoadImage( "hp30.bmp" );
}
else if (playerhealth >= 20 and playerhealth <30)
{
healthbar = LoadImage( "hp20.bmp" );
}
else if (playerhealth >= 10 and playerhealth <20)
{
healthbar = LoadImage( "hp10.bmp" );
}
else if (playerhealth <10)
{
healthbar = LoadImage( "hp00.bmp" );
}
}

Posted on 2010-02-18 19:39:58 (last edited on 2010-02-18 22:40:00)

Overkill

Your checkhealthbar is going to keep loading images everytime the drawHUD function is called, and it never frees them either. So you're leaking memory and reading an entire file each time you call checkhealthbar currently.

What I would do possibly is pre-load all the different health images. I tried to follow your naming convention as much as possible (a bit weird looking to me though):

int HUD = 1;
int imageHUD = LoadImage( "HUD.bmp" );
int imagehealth00 = LoadImage( "hp00.bmp" );
int imagehealth10 = LoadImage( "hp10.bmp" );
int imagehealth20 = LoadImage( "hp20.bmp" );
int imagehealth30 = LoadImage( "hp30.bmp" );
int imagehealth40 = LoadImage( "hp40.bmp" );
int imagehealth50 = LoadImage( "hp50.bmp" );
int imagehealth60 = LoadImage( "hp60.bmp" );
int imagehealth70 = LoadImage( "hp70.bmp" );
int imagehealth80 = LoadImage( "hp80.bmp" );
int imagehealth90 = LoadImage( "hp90.bmp" );
int imagehealth100 = LoadImage( "hp100.bmp" );
int healthbar;


void Autoexec()
{
SetAppName("EARTH");
Titlescreen("EARTH: A PROLOGUE");
Map("village.map");
showHUD();
}

void showHUD()
{
HookRetrace("drawHUD");
}

void drawHUD()
{
if(HUD = 1)
{
TBlit( 0,0, imageHUD, screen );
checkhealthbar();
TBlit( 0,0, healthbar, screen );
}
}

void checkhealthbar()
{
if (playerhealth >= 100)
{
healthbar = imagehealth100;
}
else if (playerhealth >= 90 and playerhealth <100)
{
healthbar = imagehealth90;
}
else if (playerhealth >= 80 and playerhealth <90)
{
healthbar = imagehealth80;
}
else if (playerhealth >= 70 and playerhealth <80)
{
healthbar = imagehealth70;
}
else if (playerhealth >= 60 and playerhealth <70)
{
healthbar = imagehealth60;
}
else if (playerhealth >= 50 and playerhealth <60)
{
healthbar = imagehealth50;
}
else if (playerhealth >= 40 and playerhealth <50)
{
healthbar = imagehealth40;
}
else if (playerhealth >= 30 and playerhealth <40)
{
healthbar = imagehealth30;
}
else if (playerhealth >= 20 and playerhealth <30)
{
healthbar = imagehealth20;
}
else if (playerhealth >= 10 and playerhealth <20)
{
healthbar = imagehealth10;
}
else if (playerhealth <10)
{
healthbar = imagehealth00;
}
}


Of course there are more elegant ways than this to do a health-gauge too. You could really save on having to draw images and have a lot smoother gauge if you just drew your gauge with rectangles and stuff, that change size (and color even) based on playerhealth.

Posted on 2010-02-19 13:05:16 (last edited on 2010-02-19 13:05:59)

Almostpeaceful74

Thanks, it seems to have worked for now, but I think I will take your advice and just draw it with rectangles when I get some time later on, I think that would work better.

thanks again

Posted on 2010-02-19 15:41:33

ustor

Not looking to steal Overkill's thunder here, but I thought I'd offer something cleaner without fundamentally changing anything. It uses a for loop and int array to load and store the image handles. That allows you to use a bit of math to pick the right image instead of a bunch of if/else if. Ultimately, you're still better off drawing it with rectangles, but this might be more palatable in the meantime.

int HUD = 1;
int imageHUD = LoadImage( "HUD.bmp" );
int i = 0;
int imagehealth[11];
for (i = 0; i < 11; i++)
{
imagehealth[i] = LoadImage("hp" + i + "0.bmp");
}
int healthbar;


void Autoexec()
{
SetAppName("EARTH");
Titlescreen("EARTH: A PROLOGUE");
Map("village.map");
showHUD();
}

void showHUD()
{
HookRetrace("drawHUD");
}

void drawHUD()
{
if(HUD = 1)
{
TBlit( 0,0, imageHUD, screen );
checkhealthbar();
TBlit( 0,0, healthbar, screen );
}
}

void checkhealthbar()
{
healthbar = imagehealth[playerhealth/10];
}

Posted on 2010-02-22 13:36:38

Overkill

Nah, you're more than welcome to post more elegant solutions. I just went for the least effort, quick-for-beginners application. But ustor, your current code won't work. For loops can't be used outside of a function. Also integers need to be converted to strings to be concatinated.

Here's a fix, which also does bounds checking:

int HUD = 1;
int imageHUD = LoadImage("HUD.bmp");
int imageHealth[11];
int healthBar;

void AutoExec()
{
int i;
for (i = 0; i < 11; i++)
{
imageHealth[i] = LoadImage("hp" + str(i) + "0.bmp");
}

SetAppName("EARTH");
Titlescreen("EARTH: A PROLOGUE");
Map("village.map");
ShowHUD();
}

void ShowHUD()
{
HookRetrace("DrawHUD");
}

void DrawHUD()
{
if(HUD = 1)
{
TBlit(0, 0, imageHUD, screen);
CheckHealthBar();
TBlit(0, 0, healthBar, screen);
}
}

void CheckHealthBar()
{
// Make sure to check the player health variable isn't outside the array.
if(playerhealth > 100)
{
healthBar = imageHealth[10];
}
else if(playerhealth < 0)
{
healthBar = imageHealth[0];
}
else
{
healthBar = imageHealth[playerhealth / 10];
}
// Alternatively, instead of three cases, just do this:
// healthBar = imageHealth[min(max(playerhealth, 0), 100) / 10]
}

Posted on 2010-02-24 02:24:56 (last edited on 2010-02-24 10:25:25)

zonker6666

heres a better suggestion for what youre trying to do ...


u need

int health;
int maxhealth;

void drawHealthBar()
{
int width=(health/maxhealth)*300;

RectFill(0,0, width, 30, rgb(200,0,0),screen);
Rect(0,0,300,30,rgb(255.255.255),screen);

}

Posted on 2010-03-17 20:54:59

Almostpeaceful74

wow, thanks everyone!

in the end, I did end up using the rectangle method(thanks zonker6666)
but still thank you for all the other stuff, I think it'll still come in handy for other HUD- related things (like # of lives, etc) and other things too.

Posted on 2010-03-17 21:04:21

zonker6666

youre quite welcome almostpeaceful - glad i could help out :)

now concerning icons as you mentioned --- i noticed that in your code (for the original health bar) youre loading a shitload of images to do it ---- it leads me to think youll probably be doing the same thing for the icons. ...

instead of having each item as a separate image file that gets loaded into an array of images or into a bunch of individual image pointers you may want to try the following:

say you got 10 icons in your game that are 32x32 pixels in size ---- make them into a strip ( a single image that has all the icons one over the other (32x320 in this case)

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

save that file as icons.png (or whatever :))

the use something like this in ur code ..

#define ICON_1UP 0
#define ICON_HEART 1
#define ICON_WHATEVER 2
/// define all 10 icons :)

int icons=LoadImage("icons.png");

void drawIcon(int iconID,int atx, int aty, int dest)
{
GrabRegion(0,32*iconID,32,(32*iconID)+32, atx,aty, icons, dest);
}

//// then ... wherever it is youre doing all your blitting ...

drawIcon(ICON_HEART, 10, 10, screen);
drawIcon(ICON_1UP, 110, 10, screen);

// and so on :)






Anyway --- again I hope this is helpful to you -- and good luck with your game -- cheers

Posted on 2010-03-18 18:39:37


Displaying 1-10 of 10 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.