Home > UDK > UDK tutorial : Mouse cursor with basic HUD

UDK tutorial : Mouse cursor with basic HUD

November 24th, 2009 Leave a comment Go to comments

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'

    ...

}
Update me when site is updated
Share and Enjoy:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • Turn this article into a PDF!
  • Twitter
Categories: UDK
  1. Bixel
    November 29th, 2009 at 19:06 | #1

    This is exactly where I want to go with the UDK. However I decided to skip your Isometric fixed Camera, I also made > SetBehindView(false); in PlayerController so I have a kind of Oblivion/Morrowind action going on with a independent mouse cursor/picker. However my look camera view still follows the mouse. I was wondering if you knew how to make it so that camera will follow only during the right mouse button held. Once the right mouse button is released camera is fixed.

  2. December 1st, 2009 at 19:28 | #2

    nice

  3. Hotdot
    December 2nd, 2009 at 00:02 | #3

    Bixel :

    This is exactly where I want to go with the UDK. However I decided to skip your Isometric fixed Camera, I also made > SetBehindView(false);in PlayerController so I have a kind of Oblivion/Morrowind action going on with a independent mouse cursor/picker. However my look camera view still follows the mouse. I was wondering if you knew how to make it so that camera will follow only during the right mouse button held. Once the right mouse button is released camera is fixed.

    You have to check the functions in the controller I would think that you would have to set a class variable to true while the user press the mouse key and in the tick function every frame or inside the set rotation function change the rotation. I Suggest you take a look at the UTGame controller for mouse functions.

  4. gate
    January 11th, 2010 at 19:55 | #4

    I’m working on a side scroller and have the camera and chr moving properly , using this tutorial I was able to get the mouse on the screen (thanks) how ever I was wondering how to go about to lock as a aiming pointer for my weapon.. so if I angle up to shoot it moves also.. the cross hair would be X feet away and follow the weapon, or the weapon follows the cross hair :)

  5. Hotdot
    January 12th, 2010 at 20:24 | #5

    gate :

    I’m working on a side scroller and have the camera and chr moving properly , using this tutorial I was able to get the mouse on the screen (thanks) how ever I was wondering how to go about to lock as a aiming pointer for my weapon.. so if I angle up to shoot it moves also.. the cross hair would be X feet away and follow the weapon, or the weapon follows the cross hair :)

    Do you mean like Duke Nukem Manathan project ? see http://www.youtube.com/watch?v=n5wk5wv4a_k but with a weapon crosshair ?

  6. Moriteza
    May 21st, 2010 at 17:27 | #6

    Hello and thnx for ur great Tutorials.
    I have a question.
    how can i play an animation for my player after click????

  7. Roychr
    May 27th, 2010 at 07:31 | #7
  8. Grim
    June 5th, 2010 at 02:17 | #8

    I have a top down game going, I did everything except draw the crosshair for aiming. I figured I’de use this tutorial for that and I failed at following it apparently. The new gametype uses the new hud, there is no crosshair drawn in game though, there is also no debug message drawn in game. My original attempt to accomplish this I was just telling the crosshair on each weapon to display at the current mouse location. Problem is I couldn’t figure out a way to typecast the mouseX and mouseY.

  1. December 7th, 2009 at 12:53 | #1