Announcement

Collapse
No announcement yet.

Displaying Something on the HUD When a Trigger is Touched

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Displaying Something on the HUD When a Trigger is Touched

    There are a number of ways you could display something on the player's HUD when they touch a trigger in the level. Using Kismet only is one possible approach, but not really the best choice.

    One way I would approach it would be to create a custom trigger object in UnrealScript (which you can place in the level) that fires off a function in the touching player's PlayerController class. This function would in turn fire off a function in the HUD class, which will then update the player's HUD.

    You'll need to make a new class that extends Trigger (C:\UDK\UDK-2011-09\Development\Src\Engine\Classes\Trigger.uc)

    Here's an example implementation:

    Trigger Class

    Code:
    class MyTrigger extends Trigger
    	placeable
    	ClassGroup(Common);
    
    event Touch(Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal)
    {
        local Pawn TouchingPawn;
        local MyPlayerController PC;
    	
        TouchingPawn = Pawn(Other);
        PC = MyPlayerController(TouchingPawn.Controller);
    
        `log(Self$" was touched!");
        `log("Touched by: " @ Other); // logs the pawn that touched it
        `log(Other @ " is possessed by: " @ PC); // logs the player controller that possesses the pawn.
    
        PC.TriggerTouched(); // this executes the TriggerTouched() function in the PlayerController class.
    }
    MyPlayerController should be replaced with the name of your PlayerController class.




    PlayerController Class

    Your PlayerController next fires off a function in the HUD. There are two ways to do this.

    Version 1

    In version 1, simply make a direct call to the HUD's functions with this format:

    Code:
    function TriggerTouched()
    {
        MyHUDWrapper(myHUD).HudMovie.DoSomething();
    }
    MyHUDWrapper should be replaced with the name of your HUD Wrapper class (which extends UTHUDBase)
    HudMovie should be replaced with the name of the instantiated HUD, as created by the HUD Wrapper.


    Version 2

    In version 2, we first add a function to register the HUD in the PlayerController:

    Code:
    var MyHudClass MyHud;
    
    function registerHUD(MyHudClass hud)
    {
        RegisteredHud = hud;
    }
    Now, you can add the function to call in your PlayerController, when the trigger is touched:

    Code:
    function TriggerTouched()
    {
        RegisteredHud.DoSomething();
    }



    HUD Class

    (Version 2 only) In your HUD class, call the function registerHUD() in the Init()/Start() function:

    Code:
    var MyPlayerController PC;
    
    PC = MyPlayerController(GetPC());
    PC.registerHUD(self);
    And finally, your HUD would have the function to do something visibly:

    Code:
    function DoSomething()
    {
        \\ do something in the HUD...
    }

    #2
    Are you registering the GFxMovie or the wrapper class in Version 2? The RegisterHud() function in PC takes the wrapper class while you pass in a GFxMovie into this function in your HUD class example (GetPC() doesn't exist in the wrapper)

    Comment


      #3
      Hello, can this be used to display a warning message in game for example?

      Comment


        #4
        @Snufkin
        Yes, it can be used for pretty much anything.

        Comment


          #5
          Well, a more straight forward method may be to just do it all in this Trigger class.

          Code:
          class MyTrigger extends Trigger
          	placeable
          	ClassGroup(Common);
          
          event Touch(Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal)
          {
              local Pawn TouchingPawn;
              local MyPlayerController PC;
              local MyHUD HUD;
          	
              TouchingPawn = Pawn(Other);
              if (TouchingPawn != None)
              {
                  PC = MyPlayerController(TouchingPawn.Controller);
                  if (PC != None)
                  {
                      HUD = MyHUD(PC.MyHUD);
                      if (HUD != None && HUD.HUDMovie != None)
                      {
                          HUD.HUDMovie.DoSomething();
                      }
                  }
              }
          }

          Comment


            #6
            No doubt there are many ways it could be done. Thanks for the addition.

            Comment


              #7
              I am fairly new to UnrealScript so I apologize in advance for the basic question. How can I determine which trigger is being touched in order to update the HUD appropriately?

              Thanks in advance!

              Comment


                #8
                This custom trigger you place in the map just like any other trigger in the Actor Browser, if a pawn touches the trigger it will launch
                a function in the pawns HUD. Hope this answer helps out

                Comment


                  #9
                  Quick question:

                  I tried something different from code below, but my log gives me an error in DoSomething () from type Canvas (I used Canvas to draw a basic Message on my Screen).

                  So the question is: Is it possible to call the Canvas class in my DoSometthing() function without any call in DrawHUD or is it only possible to use this class in PostRender() and DrawHUD()?

                  Comment

                  Working...
                  X