Game Maker 8 Tutorial: Draw HUD Elements in 3D

James Cloud
Ninety nine percent of all games, in all platforms and in all genres, draw a 2D heads up display (also know as a HUD). These graphics and text typically display vital information about the player health, ammunition, score, etc. When using the Game Maker 8 game development system, however, 2D drawing is disabled when 3D becomes enabled. Follow the steps given below to learn how to overcome this annoyance by using some simple scripts.

Step One - Draw Sprite and Draw Sprite Ext

Right click on the scripts folder in the left side of the Game Maker window and select create new script. Name the new script d3d_draw_sprite. Now, either copy and paste or type in the following GML source code,

var sprite, subimage, x_coord, y_coord;
sprite = argument0;
subimage = argument1;
x_coord = argument2;
y_coord = argument3;

d3d_set_hidden(false);
d3d_set_lighting(false);
d3d_set_projection_ortho(0,0,view_wview[view_current],view_hview[view_current],0);

draw_sprite(sprite, subimage, x_coord, y_coord);

d3d_set_hidden(true);
d3d_set_lighting(true);

This script will take the exact same arguments that the default draw_sprite function takes, drawing a sprite at the given coordinates.

After saving the new script, create another one called d3d_draw_sprite_ext and insert this code into the editor,

var sprite, subimage, x_coord, y_coord, x_scale, y_scale, rotation, color, alpha;
sprite = argument0;
subimage = argument1;
x_coord = argument2;
y_coord = argument2;
x_scale = argument3;
y_scale = argument4;
rotation = argument5;
color = argument6;
alpha = argument7;

d3d_set_hidden(false);
d3d_set_lighting(false);
d3d_set_projection_ortho(0,0,view_wview[view_current],view_hview[view_current],0);

draw_sprite_ext(sprite, subimage, x_coord, y_coord, x_scale, y_scale, rotation, color, alpha)

d3d_set_hidden(true);
d3d_set_lighting(true);

This script is the same as draw_sprite_ext and takes the same arguments. Draw_Sprite_Ext gives more options and control over the drawing of an individual sprite. Don't forget to save this script as well.

Step Two - Draw Text

The final script to create is called d3d_draw_text. It takes the same arguments as Game Maker's draw_text built in function. After creating a new script using that name, place this source code into the new script,

var x_coord, y_coord, str;
x_coord = argument0;
y_coord = argument1;
str = argument2;

d3d_set_hidden(false);
d3d_set_lighting(false);
d3d_set_projection_ortho(0,0,view_wview[view_current],view_hview[view_current],0);

draw_text(x_coord, y_coord, str);

d3d_set_hidden(true);
d3d_set_lighting(true);

Save the script and that's it!

Conclusion

With the proper scripts the Game Maker engine can determine when and how to draw 2D images and text and when to render 3D objects. All that is required is the calling of newly created scripts, d3d_draw_sprite for regular sprites, d3d_draw_sprite_ext for extended spriting options, and d3d_draw_text for drawing text on screen.

NOTE:
It is typical in the Game Maker community to ask for credit, however these scripts are provided absolutely free and require no credit what so ever.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.