UDK tutorial : Mouse cursor with basic HUD
In my last UDK tutorial. We saw how to get basic isometric camera up and running. To get things into motion for full RTS or Isometric game, you will need persistent mouse cursor so you can actually click somewhere usefull inside the game. This tutorial will not yet cover how the mouse click is handled for movement but I will give you a basic setup so you can do some research on your own (Nothing like it if you really want to get some UnrealScript coding skills).
First of all you will add a new script file in visual studio inside your Classes folder for your project. Again if you are unsure how to do this, look into this post, or this thread. Now name your new script file with the name appended name “Hud” for clarity ex : “MyModHud”.
You should have the following code :
class MyModHud extends Object;
DefaultProperties
{
}
Now you will change the extends of the class to reflect something like :
class MyModHud extends GameHUD config(Game);
Now the function you will want to have to get basic mouse coordinate is the following (put it between class declaration and DefaultProperties.
function vector2D GetMouseCoordinates()
{
local Vector2D mousePos;
local UIInteraction UIController;
local GameUISceneClient GameSceneClient;
UIController = PlayerOwner.GetUIController();
if ( UIController != None)
{
GameSceneClient = UIController.SceneClient;
if ( GameSceneClient != None )
{
mousePos.X = GameSceneClient.MousePosition.X;
mousePos.Y = GameSceneClient.MousePosition.Y;
}
}
return mousePos;
}
What it does is basically get the User Interface Controller and set the mouse X and Y. Now to get a proper Heads Up Display running in UDK, you will need to override the PostRender function. In the PostRender function you will prepare all the display position and work before rendering it to screen. It will be PostRender that will call DrawHud (Where the rendering to screen actually go). DrawHud is considered as the main drawing pump. The variable Canvas will only be valid during PostRender and that is why we call the DrawHud function from it.
Before we actually add those two functions, we need a couple of variables on top of our class :
var Vector WorldOrigin; var Vector WorldDirection; var FontRenderInfo TextRenderInfo; var Texture2D CursorTexture; /** Various colors */ var const color GoldColor; var Vector2D MousePosition;
WorldOrigin will hold where our mouseclick occured in world unit.
WorldDirection will hold the direction of the ray that is cast to the world for de-projection
TextRenderInfo will hold extra information on drawing text on screen for debug purpose.
CursorTexture will hold a reference to the cursor texture will will plot to screen.
GoldColor will hold a sample gold color for rendered text on screen
MousePosition will hold where the mouse is hovering during play.
With those variables on top we need to set the default properties for some of them and so go inside the DefaultProperties brackets and put the following :
DefaultProperties
{
CursorTexture=Texture2D'UI_HUD.HUD.UTCrossHairs'
GoldColor=(R=255,G=183,B=11,A=255)
TextRenderInfo=(bClipText=true)
}
Now you will put the rest inside the class just before the DefaultProperties :
//Canvas is only valid during PostRender phase
event PostRender()
{
MousePosition = GetMouseCoordinates();
//Deproject the mouse from screen coordinate to world coordinate and store World Origin and Dir.
Canvas.DeProject(MousePosition, WorldOrigin, WorldDirection);
//now you have the world origin and direction vector of the mouse deprojection
//so you can do with it whatever you'd like, such as do a Trace with it to see what the mouse is pointing at
DrawHUD();
}
/**
* This is the main drawing pump. It will determine which hud we need to draw (Game or PostGame). Any drawing that should occur
* regardless of the game state should go here.
*/
function DrawHUD()
{
local string StringMessage;
StringMessage = "MouseX" @ MousePosition.X @ "MouseY" @ MousePosition.Y @ "World" @ WorldOrigin @ "WorldDir" @ WorldDirection;
// now draw string with GoldColor color
Canvas.DrawColor = GoldColor;
Canvas.SetPos( 10, 10 );
Canvas.DrawText( StringMessage, false, , , TextRenderInfo );
//Set position for mouse and plot the 2d texture.
Canvas.SetPos(MousePosition.X, MousePosition.Y);
Canvas.DrawTile(CursorTexture, 26 , 26, 380, 320, 26,26);
}
The code is pretty explicit in itself, you can get the final file here.
The last operation to do, to make your hud be actually used by your game is to set it properly inside your gameInfo class.
defaultproperties
{
...
HUDType=class'MyMod.MyModHud'
...
}
Most of us did think it was in the working. Its not real until you see it in action
Former Quebec Premier Bernard Landry has dubbed tax credits implemented for multimedia development companies in the Canadian province as an enormous success,