No announcement yet.

Displaying Map Name & Gameplay Time in the HUD

  • Filter
  • Time
  • Show
Clear All
new posts

    Displaying Map Name & Gameplay Time in the HUD

    Hi UDK folks. Just a very short, beginner level tutorial today to demonstrate how to easily display the name of a level/map and the total gameplay time in the HUD.

    NOTE: This tutorial assumes you already have a working Scaleform HUD, including Flash file and UnrealScript files.

    Step 1 - Flash Setup

    Add a standard Flash textfield to the stage, and give it an instance name of mapname_tf.
    Add another standard Flash textfield to the stage, and give this one an instance name of gametime_tf.
    Publish your HUD SWF and reimport into UDK.

    Step 2 - UnrealScript

    First declare the variables that will be used to cache a reference to the Flash textfields:

    var GFxObject MapNameTF, GameTimeTF;
    We'll also need to keep track of the last known time, so add that variable next:

    var int LastTime;
    You will also need to get a reference to the WorldInfo:

    var WorldInfo    ThisWorld;
    Now, get the worldinfo by adding this line to your Init()/Start() function:

    ThisWorld = GetPC().WorldInfo;
    Then, cache the references to your Flash textfields in your Init()/Start() function:

    MapNameTF = GetVariableObject("_root.mapname_tf");
    GameTimeTF = GetVariableObject("_root.gametime_tf");
    Next, initialize LastTime to zero:

    LastTime = 0;
    To populate the map name, simply add this code to the Init()/Start() function now:

    // Display the current map name (sans map prefix) on the HUD.
    MapNameTF.SetString("text", ThisWorld.GetMapName(false));
    Now, to add the running game time, which updates as you play, you need to add this code to your TickHud() function:

    local int CurrentTime;
    CurrentTime = ThisWorld.TimeSeconds;
    // Only update the textfield if the current time is different than the last known time.
    if (CurrentTime != LastTime)
        GameTimeTF.SetString("text", FormatTime(CurrentTime));
    NOTE: Alternatively, replace ThisWorld.TimeSeconds with ThisWorld.RealTimeSeconds instead if you don't want the total play time to pause when the game is paused.

    Last of all, add a helper function to format the time into hh:mm:ss format:
    static function string FormatTime(int Seconds)
    	local int Hours, Mins;
    	local string NewTimeString;
    	Hours = Seconds / 3600;
    	Seconds -= Hours * 3600;
    	Mins = Seconds / 60;
    	Seconds -= Mins * 60;
    	if (Hours > 0)
    		NewTimeString = ( Hours > 9 ? String(Hours) : "0"$String(Hours)) $ ":";
    	NewTimeString = NewTimeString $ ( Mins > 9 ? String(Mins) : "0"$String(Mins)) $ ":";
    	NewTimeString = NewTimeString $ ( Seconds > 9 ? String(Seconds) : "0"$String(Seconds));
    	return NewTimeString;

    Thanks for that, but what do I have to change to have like this format?



      I am interested in knowing how to do that to.

      I have been playing around with numbers and variables for quite sometime and not managed to get the milli/centiseconds to work correctly.


        Change "local int CurrentTime;" into "local float CurrentTime;"


        static function string FormatTime(int Seconds)


        static function string FormatTime(float Seconds)

        Now, just add the calculations for miliseconds.