Many games have some messaging system on their HUDs (player picked up some power-up, have taken some damage, timer is ticking away, etc.). Using just the simple extension of HUD class, you can achieve similar results.
We'll use a simple structure to represent our message:
1: //message struct define
2: struct HUDMessage
3: {
4: var string sMessage;
5: var float fMessageTimer;
6: };
Simple method to display string on HUD (based on great vectorlab tutorial):
All messages are stored in a simple dynamic array and updated/displayed/removed each frame:
1: //========================================================================
2: function DrawString(string text, int X, int Y, int R, int G, int B, int A)
3: {
4: Canvas.SetPos(X, Y);
5: Canvas.SetDrawColor(R, G, B, A);
6: Canvas.Font = class'Engine'.static.GetMediumFont();
7: Canvas.DrawText(text);
8: }
All messages are stored in a simple dynamic array and updated/displayed/removed each frame:
1: var array<HUDMessage> aMessages;
2:
3: //public method to be called from anywhere in game (your actors, etc.)
4: //========================================================================
5: function StringToHUD(string message, float timer = 0)
6: {
7: local HUDMessage newMessage;
8:
9: newMessage.sMessage = message;
10: newMessage.fMessageTimer = -timer; //make it appear longer, optional
11:
12: aMessages.InsertItem(0,newMessage); //insert at the beginning!
13: }
14:
15: //========================================================================
16: simulated event Tick(float DeltaTime)
17: {
18: local int i;
19:
20: //update message array
21: for (i = 0; i < aMessages.length; ++i)
22: {
23: aMessages[i].fMessageTimer += DeltaTime;
24: }
25: }
26:
27: //draw routine to display cascaded messages
28: //========================================================================
29: function DrawCustomMessage()
30: {
31: local HUDMessage message;
32: local offsetY = 200;
33:
34: foreach aMessages(message) //display all messages
35: {
36: //message is still to be displayed
37: if (message.fMessageTimer < 2.0f)
38: {
39: DrawString(message.sMessage,0,offsetY,255,255,255,255,true);
40: offsetY += 20; //update next message position
41: }
42: else //expired, remove
43: {
44: aMessages.RemoveItem(message);
45: }
46: }
47:
48: }
49:
50: //========================================================================
51: function DrawHUD()
52: {
53: super.DrawHUD(); //don't forget to call parent method!
54:
55: DrawCustomMessage(); //our messaging routine
56: }
And here is an example screenshot directly from game:
No comments:
Post a Comment