Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

Custom HUD|Canvas is none

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

    Custom HUD|Canvas is none

    Hello everyone! I am quite new to unrealscript and I am trying to make a game where you watch the player from above and he shoots wherever your mouse is(like in Of Guards and Thieves).

    The problem is that I am always getting that my canvas is none in my custom HUD class right under the line "// Ensure that we have a valid canvas and player owner". Here's the code:

    This is my custom utgame class:
    Code:
    class AwesomeGame extends UTDeathmatch;
    
    defaultproperties
    {
          DefaultInventory(0)=None
          bScoreDeaths=false
          PlayerControllerClass=class'AwesomeGame.AwesomePlayerController'
          HUDType=class'AwesomeHUD'
          DefaultPawnClass=class'AwesomeGame.AwesomePawn'
    }
    This is my custom PlayerInput class:
    Code:
    class AwesomePlayerInput extends PlayerInput;
    
    // Stored mouse position.
    var public IntPoint MousePosition;
    
    event PlayerInput(float DeltaTime)
    {
      // 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, 1920);
        // Add the aMouseY to the mouse position and clamp it within the viewport height
        MousePosition.Y = Clamp(MousePosition.Y - aMouseY, 0, 1080);
      }
    
      Super.PlayerInput(DeltaTime);
    }
    
    defaultproperties
    {
    }
    This is my custom PlayerController class:
    Code:
    class AwesomePlayerController extends UTPlayerController;
    
    var vector PlayerViewOffset;
    var vector CurrentCameraLocation, DesiredCameraLocation;
    var rotator CurrentCameraRotation;
    var rotator PawnCurrentRotation;
    var AwesomeHUD AwesomeHUD;
    var vector TestMouseCoords;
    
    simulated function PostBeginPlay()
    {
      super.PostBeginPlay();
      bNoCrosshair = true;
    }
    
    reliable client function ClientSetHUD(class<HUD> newHUDType)
    {
        if(myHUD != none)
        myHUD.Destroy();
        myHUD = spawn(class'AwesomeHUD', self);
    }
    
    
    
    simulated event GetPlayerViewPoint(out vector out_Location, out Rotator out_Rotation)
     {
          super.GetPlayerViewPoint(out_Location, out_Rotation);
    
           if(Pawn != none)
               {
                    out_Location = CurrentCameraLocation;
                    out_Rotation = rotator((out_Location * vect(1,1,0)) - out_Location);
               }
               CurrentCameraRotation = out_Rotation;
     }
    
     function Rotator GetAdjustedAimFor(Weapon W, vector StartFireLoc )
      {
          return Pawn.Rotation;
      }
      
    function PlayerTick(float DeltaTime)
    {
            super.PlayerTick(DeltaTime);
    
            if(Pawn != none)
            {
                TestMouseCoords = AwesomeHUD(myHUD).GetMouseWorldLocation();
                PawnCurrentRotation = rotator(TestMouseCoords * vect(1,1,0) - Pawn.Location * vect(1,1,0));
                SetRotation(PawnCurrentRotation);
    
                DesiredCameraLocation = Pawn.Location + (PlayerViewOffset >> Pawn.Rotation);
                CurrentCameraLocation += (DesiredCameraLocation - CurrentCameraLocation) * DeltaTime * 6;
            }
    }
    
    state PlayerWalking
    {
        function ProcessMove( float DeltaTime, vector newAccel, eDoubleClickDir DoubleClickMove, rotator DeltaRot)
        {
            local vector X, Y, Z, AltAccel;
            GetAxes(CurrentCameraRotation, X, Y, Z);
            AltAccel = PlayerInput.aForward * Z + PlayerInput.aStrafe * Y;
            AltAccel.Z = 0;
            AltAccel = Pawn.AccelRate * Normal(AltAccel);
            super.ProcessMove(DeltaTime, AltAccel, DoubleClickMove, DeltaRot);
        }
    }
    
    defaultproperties
    {
    PlayerViewOffset=(X=0,Y=0,Z=512)
    InputClass=class'AwesomePlayerInput'
    }
    And my custom HUD class is:
    Code:
    class AwesomeHUD extends HUD;
    
    function Vector GetMouseWorldLocation()
    {
      local AwesomePlayerInput AwesomePlayerInput;
      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
      AwesomePlayerInput = AwesomePlayerInput(PlayerOwner.PlayerInput);
    
      // Ensure that the player input is valid
      if (AwesomePlayerInput == None)
      {
        return Vect(0, 0, 0);
      }
    
      // We stored the mouse position as an IntPoint, but it's needed as a Vector2D
      MousePosition.X = AwesomePlayerInput.MousePosition.X;
      MousePosition.Y = AwesomePlayerInput.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
    {
    }
    Any help would be appreciated.

    #2
    Got it! I changed my custom hud to extend not HUD, but UTGFxHUDWrapper and got the canvas from PostRender. But now I have another problem. The GetMouseWorldLocation function should return the mouse's world coordinates but it always returns 0 0 0.

    Comment


      #3
      This kind of question should probably be asked in the dedicated subforum https://forums.epicgames.com/forums/...d-Unrealscript
      I can remember a few occasions with similar questions.

      Your function will return 0,0,0 in any of your special cases, Canvas being none, PlayerInput == none or the cast of the PlayerInput to AwesomePlayerInput failed. For your original question it depends where GetMouseWorldLocation is called from. The Canvas available to your HUD is only valid (!= none) during the PostRender event (and functions called therein). You could compute the result in PostRender and store it in a variable in your HUD, GetMouseWorldLocation would only serve as a getter to that value then. Alternatively you can deproject using the LocalPlayer.

      Comment

      Working...
      X