Announcement

Collapse
No announcement yet.

Adding the mouse cursor to HUD?

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

    Adding the mouse cursor to HUD?

    Hello,

    I'm new to UScript and a friend of mine pointed me to a tutorial on the UDN that shows how to get the mouse cursor to show up and interact with a level in UDK. I'm slowly going through this tutorial, but I seem to be having a couple of issues. I'm getting 3 errors that I'm not sure how to fix. I don't want to open UDK until these can be fixed. I don't want to mess up my project or UDK in general. I've already had to reinstall UDK multiple times due to various issues.

    Any help would be greatly appreciated. Thank you!!

    Errors:


    MouseInterfaceInteractionInterface.uc
    Code:
    class MouseInterfaceInteractionInterface extends Object;
    
    interface MouseInterfaceInteractionInterface
    
    // Called when the left mouse button is pressed
    function MouseLeftPressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal);
    
    // Called when the left mouse button is released
    function MouseLeftReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the right mouse button is pressed
    function MouseRightPressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal);
    
    // Called when the right mouse button is released
    function MouseRightReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the middle mouse button is pressed
    function MouseMiddlePressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal);
    
    // Called when the middle mouse button is released
    function MouseMiddleReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the middle mouse button is scrolled up
    function MouseScrollUp(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the middle mouse button is scrolled down
    function MouseScrollDown(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the mouse is moved over the actor
    function MouseOver(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Called when the mouse is moved out from the actor (when it was previously over it)
    function MouseOut(Vector MouseWorldOrigin, Vector MouseWorldDirection);
    
    // Returns the hit location of the mouse trace
    function Vector GetHitLocation();
    
    // Returns the hit normal of the mouse trace
    function Vector GetHitNormal();
    
    // Returns the mouse world origin calculated by the deprojection within the canvas
    function Vector GetMouseWorldOrigin();
    
    // Returns the mouse world direction calculated by the deprojection within the canvas
    function Vector GetMouseWorldDirection();
    
    DefaultProperties
    {
    }
    MouseInterfaceKActor.uc
    Code:
    class MouseInterfaceKActor extends KActor
    Implements(MouseInterfaceInteractionInterface);
    
    var Vector CachedMouseHitLocation;
    var Vector CachedMouseHitNormal;
    var Vector CachedMouseWorldOrigin;
    var Vector CachedMouseWorldDirection;
    
    // ===
    // MouseInterfaceInteractionInterface implementation
    // ===
    function MouseLeftPressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = HitLocation;
      CachedMouseHitNormal = HitNormal;
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 0);
    }
    
    function MouseLeftReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 1);
    }
    
    function MouseRightPressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = HitLocation;
      CachedMouseHitNormal = HitNormal;
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 2);
    }
    
    function MouseRightReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 3);
    }
    
    function MouseMiddlePressed(Vector MouseWorldOrigin, Vector MouseWorldDirection, Vector HitLocation, Vector HitNormal)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = HitLocation;
      CachedMouseHitNormal = HitNormal;
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 4);
    }
    
    function MouseMiddleReleased(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 5);
    }
    
    function MouseScrollUp(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 6);
    }
    
    function MouseScrollDown(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 7);
    }
    
    function MouseOver(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 8);
    }
    
    function MouseOut(Vector MouseWorldOrigin, Vector MouseWorldDirection)
    {
      CachedMouseWorldOrigin = MouseWorldOrigin;
      CachedMouseWorldDirection = MouseWorldDirection;
      CachedMouseHitLocation = Vect(0.f, 0.f, 0.f);
      CachedMouseHitNormal = Vect(0.f, 0.f, 0.f);
      TriggerEventClass(class'SeqEvent_MouseInput', Self, 9);
    }
    
    function Vector GetHitLocation()
    {
      return CachedMouseHitLocation;
    }
    
    function Vector GetHitNormal()
    {
      return CachedMouseHitNormal;
    }
    
    function Vector GetMouseWorldOrigin()
    {
      return CachedMouseWorldOrigin;
    }
    
    function Vector GetMouseWorldDirection()
    {
      return CachedMouseWorldDirection;
    }
    
    defaultproperties
    {
      SupportedEvents(4)=class'SeqEvent_MouseInput'
    }
    MouseInterfacePlayerInput.uc
    Code:
    class MouseInterfacePlayerInput extends PlayerInput;
    
    // Stored mouse position. Set to private write as we don't want other classes to modify it, but still allow other classes to access it.
    var IntPoint MousePosition; 
    
    event PlayerInput(float DeltaTime)
    {
      // Handle mouse 
      // Ensure we have a valid HUD
      if (myHUD != None) 
      {
        // Add the aMouseX to the mouse position and clamp it within the viewport width
        MousePosition.X = Clamp(MousePosition.X + aMouseX, 0, myHUD.SizeX); 
        // Add the aMouseY to the mouse position and clamp it within the viewport height
        MousePosition.Y = Clamp(MousePosition.Y - aMouseY, 0, myHUD.SizeY); 
      }
    
      Super.PlayerInput(DeltaTime);
    }
    
    defaultproperties
    {
    }
    MouseInterfacePlayerController.uc
    Code:
    class MouseInterfacePlayerController extends PlayerController;
    
    // Null this function
    function UpdateRotation(float DeltaTime);
    
    defaultproperties
    {   
      // Set the input class to the mouse interface player input
      InputClass=class'MouseInterfacePlayerInput'
    }
    MouseInterfaceHUD.uc
    Code:
    class MouseInterfaceHUD extends HUD;
    
    // The texture which represents the cursor on the screen
    var const Texture2D CursorTexture;
    // The color of the cursor
    var const Color CursorColor;
    // Pending left mouse button pressed event
    var bool PendingLeftPressed;
    // Pending left mouse button released event
    var bool PendingLeftReleased;
    // Pending right mouse button pressed event
    var bool PendingRightPressed;
    // Pending right mouse button released event
    var bool PendingRightReleased;
    // Pending middle mouse button pressed event
    var bool PendingMiddlePressed;
    // Pending middle mouse button released event
    var bool PendingMiddleReleased;
    // Pending mouse wheel scroll up event
    var bool PendingScrollUp;
    // Pending mouse wheel scroll down event
    var bool PendingScrollDown;
    // Cached mouse world origin
    var Vector CachedMouseWorldOrigin;
    // Cached mouse world direction
    var Vector CachedMouseWorldDirection;
    // Last mouse interaction interface
    var MouseInterfaceInteractionInterface LastMouseInteractionInterface;
    
    event PostRender()
    {
      local MouseInterfacePlayerInput MouseInterfacePlayerInput;
      local MouseInterfaceInteractionInterface MouseInteractionInterface;
      local Vector HitLocation, HitNormal;
    
      Super.PostRender();
    
      // Ensure that we aren't using ScaleForm and that we have a valid cursor
      if (!UsingScaleForm && CursorTexture != None)
      {
        // Ensure that we have a valid PlayerOwner
        if (PlayerOwner != None)
        {
          // Cast to get the MouseInterfacePlayerInput
          MouseInterfacePlayerInput = MouseInterfacePlayerInput(PlayerOwner.PlayerInput);
    
          // If we're not using scale form and we have a valid cursor texture, render it
          if (MouseInterfacePlayerInput != None)
          {
            // Set the canvas position to the mouse position
            Canvas.SetPos(MouseInterfacePlayerInput.MousePosition.X, MouseInterfacePlayerInput.MousePosition.Y);
            // Set the cursor color
            Canvas.DrawColor = CursorColor;
            // Draw the texture on the screen
            Canvas.DrawTile(CursorTexture, CursorTexture.SizeX, CursorTexture.SizeY, 0.f, 0.f, CursorTexture.SizeX, CursorTexture.SizeY,, true);
          }
        }
      }
    
      // Get the current mouse interaction interface
      MouseInteractionInterface = GetMouseActor(HitLocation, HitNormal);
    
      // Handle mouse over and mouse out
      // Did we previously had a mouse interaction interface?
      if (LastMouseInteractionInterface != None)
      {
        // If the last mouse interaction interface differs to the current mouse interaction
        if (LastMouseInteractionInterface != MouseInteractionInterface)
        {
          // Call the mouse out function
          LastMouseInteractionInterface.MouseOut(CachedMouseWorldOrigin, CachedMouseWorldDirection);
          // Assign the new mouse interaction interface
          LastMouseInteractionInterface = MouseInteractionInterface; 
    
          // If the last mouse interaction interface is not none
          if (LastMouseInteractionInterface != None)
          {
            // Call the mouse over function
            LastMouseInteractionInterface.MouseOver(CachedMouseWorldOrigin, CachedMouseWorldDirection); // Call mouse over
          }
        }
      }
      else if (MouseInteractionInterface != None)
      {
        // Assign the new mouse interaction interface
        LastMouseInteractionInterface = MouseInteractionInterface; 
        // Call the mouse over function
        LastMouseInteractionInterface.MouseOver(CachedMouseWorldOrigin, CachedMouseWorldDirection); 
      }
    
      if (LastMouseInteractionInterface != None)
      {
        // Handle left mouse button
        if (PendingLeftPressed)
        {
          if (PendingLeftReleased)
          {
            // This is a left click, so discard
            PendingLeftPressed = false;
            PendingLeftReleased = false;
          }
          else
          {
            // Left is pressed
            PendingLeftPressed = false;
            LastMouseInteractionInterface.MouseLeftPressed(CachedMouseWorldOrigin, CachedMouseWorldDirection, HitLocation, HitNormal);
          }
        }
        else if (PendingLeftReleased)
        {
          // Left is released
          PendingLeftReleased = false;
          LastMouseInteractionInterface.MouseLeftReleased(CachedMouseWorldOrigin, CachedMouseWorldDirection);
        }
    
        // Handle right mouse button
        if (PendingRightPressed)
        {
          if (PendingRightReleased)
          {
            // This is a right click, so discard
            PendingRightPressed = false;
            PendingRightReleased = false;
          }
          else
          {
            // Right is pressed
            PendingRightPressed = false;
            LastMouseInteractionInterface.MouseRightPressed(CachedMouseWorldOrigin, CachedMouseWorldDirection, HitLocation, HitNormal);
          }
        }
        else if (PendingRightReleased)
        {
          // Right is released
          PendingRightReleased = false;
          LastMouseInteractionInterface.MouseRightReleased(CachedMouseWorldOrigin, CachedMouseWorldDirection);
        }
    
        // Handle middle mouse button
        if (PendingMiddlePressed)
        {
          if (PendingMiddleReleased)
          {
            // This is a middle click, so discard 
            PendingMiddlePressed = false;
            PendingMiddleReleased = false;
          }
          else
          {
            // Middle is pressed
            PendingMiddlePressed = false;
            LastMouseInteractionInterface.MouseMiddlePressed(CachedMouseWorldOrigin, CachedMouseWorldDirection, HitLocation, HitNormal);
          }
        }
        else if (PendingMiddleReleased)
        {
          PendingMiddleReleased = false;
          LastMouseInteractionInterface.MouseMiddleReleased(CachedMouseWorldOrigin, CachedMouseWorldDirection);
        }
    
        // Handle middle mouse button scroll up
        if (PendingScrollUp)
        {
          PendingScrollUp = false;
          LastMouseInteractionInterface.MouseScrollUp(CachedMouseWorldOrigin, CachedMouseWorldDirection);
        }
    
        // Handle middle mouse button scroll down
        if (PendingScrollDown)
        {
          PendingScrollDown = false;
          LastMouseInteractionInterface.MouseScrollDown(CachedMouseWorldOrigin, CachedMouseWorldDirection);
        }
      }
    }
    
    function MouseInterfaceInteractionInterface GetMouseActor(optional out Vector HitLocation, optional out Vector HitNormal)
    {
      local MouseInterfaceInteractionInterface MouseInteractionInterface;
      local MouseInterfacePlayerInput MouseInterfacePlayerInput;
      local Vector2D MousePosition;
      local Actor HitActor;
    
      // Ensure that we have a valid canvas and player owner
      if (Canvas == None || PlayerOwner == None)
      {
        return None;
      }
    
      // Type cast to get the new player input
      MouseInterfacePlayerInput = MouseInterfacePlayerInput(PlayerOwner.PlayerInput);
    
      // Ensure that the player input is valid
      if (MouseInterfacePlayerInput == None)
      {
        return None;
      }
    
      // We stored the mouse position as an IntPoint, but it's needed as a Vector2D
      MousePosition.X = MouseInterfacePlayerInput.MousePosition.X;
      MousePosition.Y = MouseInterfacePlayerInput.MousePosition.Y;
      // Deproject the mouse position and store it in the cached vectors
      Canvas.DeProject(MousePosition, CachedMouseWorldOrigin, CachedMouseWorldDirection);
    
      // Perform a trace actor interator. An interator is used so that we get the top most mouse interaction
      // interface. This covers cases when other traceable objects (such as static meshes) are above mouse
      // interaction interfaces.
      ForEach TraceActors(class'Actor', HitActor, HitLocation, HitNormal, CachedMouseWorldOrigin + CachedMouseWorldDirection * 65536.f,         CachedMouseWorldOrigin,,, TRACEFLAG_Bullet)
      {
        // Type cast to see if the HitActor implements that mouse interaction interface
        MouseInteractionInterface = MouseInterfaceInteractionInterface(HitActor);
        if (MouseInteractionInterface != None)
        {
          return MouseInteractionInterface;
        }
      }
    
      return None;
    }
    
    function Vector GetMouseWorldLocation()
    {
      local MouseInterfacePlayerInput MouseInterfacePlayerInput;
      local Vector2D MousePosition;
      local Vector MouseWorldOrigin, MouseWorldDirection, HitLocation, HitNormal;
    
      // Ensure that we have a valid canvas and player owner
      if (Canvas == None || PlayerOwner == None)
      {
        return Vect(0, 0, 0);
      }
    
      // Type cast to get the new player input
      MouseInterfacePlayerInput = MouseInterfacePlayerInput(PlayerOwner.PlayerInput);
    
      // Ensure that the player input is valid
      if (MouseInterfacePlayerInput == None)
      {
        return Vect(0, 0, 0);
      }
    
      // We stored the mouse position as an IntPoint, but it's needed as a Vector2D
      MousePosition.X = MouseInterfacePlayerInput.MousePosition.X;
      MousePosition.Y = MouseInterfacePlayerInput.MousePosition.Y;
      // Deproject the mouse position and store it in the cached vectors
      Canvas.DeProject(MousePosition, MouseWorldOrigin, MouseWorldDirection);
    
      // Perform a trace to get the actual mouse world location.
      Trace(HitLocation, HitNormal, MouseWorldOrigin + MouseWorldDirection * 65536.f, MouseWorldOrigin , true,,, TRACEFLAG_Bullet);
      return HitLocation;
    }
    
    defaultproperties
    {
      CursorColor=(R=255,G=255,B=255,A=255)
      CursorTexture=Texture2D'EngineResources.Cursors.Arrow'
    }

    #2
    class MouseInterfaceInteractionInterface extends Object;

    try removing that.

    Comment


      #3
      Thank you! I thought you were always supposed to have that (or something like that) at the top of every class file. I wish I wasn't so dumb with UDK. ^^;; It's taking me a long time to figure out (and use). I also want to remove all references to Scaleform and GFx (I have my own custom HUD). The line:

      Code:
      // Ensure that we aren't using ScaleForm and that we have a valid cursor
        if (!UsingScaleForm && CursorTexture != None)
        {
          // Ensure that we have a valid PlayerOwner
          if (PlayerOwner != None)
          {
            // Cast to get the MouseInterfacePlayerInput
            MouseInterfacePlayerInput = MouseInterfacePlayerInput(PlayerOwner.PlayerInput);
      
            // If we're not using scale form and we have a valid cursor texture, render it
            if (MouseInterfacePlayerInput != None)
            {
              // Set the canvas position to the mouse position
              Canvas.SetPos(MouseInterfacePlayerInput.MousePosition.X, MouseInterfacePlayerInput.MousePosition.Y);
              // Set the cursor color
              Canvas.DrawColor = CursorColor;
              // Draw the texture on the screen
              Canvas.DrawTile(CursorTexture, CursorTexture.SizeX, CursorTexture.SizeY, 0.f, 0.f, CursorTexture.SizeX, CursorTexture.SizeY,, true);
            }
      is bringing up an error: "Error 1 Bad or missing expression after '!': 'UsingScaleForm'". How do I rid myself of this without screwing up the rest of the if statement? Should I just scrap the whole statement? Any help would be greatly appreciated. Thank you!

      Comment


        #4
        if you are using MouseInterfaceHUD, there should be no scaleform, since you are extending from HUD. You should make sure that your hud is being used.

        and this might help clarify something about the previous error

        http://udn.epicgames.com/Three/Unrea...nterfaces.html

        Comment


          #5
          So I should just scrap the first if statement or the whole thing?

          Comment


            #6
            I give you the entire code for getting a mouse cursor on screen in this tutorial



            Extensive actual code supplied to you for your use



            Third Person and Free Roam Cameras and Mouse Drag Select



            Rama

            Comment


              #7
              I'm not sure if I can use this, but I'll take a look at it. Thank you! ^_^

              Comment


                #8
                the whole code is provided, yes, and no scaleform is required.

                its very simple

                you get the mouse movements from the player and you generate a cursor in the HUD and position it using mouse movements

                tutorial explains everything

                Rama

                Comment


                  #9
                  Okay. Thank you. I was told to use the tutorial on the UDN site and get rid of all of the scaleform/GFx code. As long as the file names and code are the same I should be okay. Am I correct in assuming this? (Sorry for the dumb questions. I'm just really frustrated. UDK is very hard for me to use in a physical sense and the UScript isn't helping.)

                  I have to make sure that the following is done:
                  Mouse Cursor Displayed
                  an interactable Mouse Actor (that has to be used in game play)
                  Mouse Input Kismet event used in game play

                  Any help would be greatly appreciated evernewjoy. I thank you for your help already!

                  Comment


                    #10
                    "an interactable Mouse Actor (that has to be used in game play)
                    Mouse Input Kismet event used in game play"

                    these wont be hard

                    not sure exactly what u mean by interactable mouse actor, you will have to explain further for all of us to assist you.

                    ~~~

                    mouse input in kismet


                    Mouse input is easy in kismet

                    right click-> new event -> input -> key/button pressed

                    click on it and make sure Window->Properties is active/open

                    add a key name "LeftMouseButton" (click green plus arrow)



                    Rama

                    PS: to connect your kismet mouse event to your custom class functions

                    Making Custom Kismet Nodes

                    it would be better to just keybind the mouse button via your defaultInput.ini

                    see here for more details
                    http://forums.epicgames.com/threads/...7#post31030597

                    Comment

                    Working...
                    X