【问题标题】:Unreal Engine - How to get the AxisMappings via C++ code虚幻引擎 - 如何通过 C++ 代码获取 AxisMappings
【发布时间】:2019-12-04 19:02:19
【问题描述】:

我是 Unreal Engine 和 C++ 的新手...

我正在尝试获取分配的轴键。我发现信息存储在 DefaultInput.ini 文件中,但我如何以编程方式访问这些数据?

有一个 GetAxisValue(const FName) 方法,但它不返回任何内容。

FString AxisName = "MoveForward";
auto value = PlayerInputComponent->GetAxisValue(FName(*AxisName));

我做错了什么?非常感谢您的帮助。

【问题讨论】:

    标签: c++ unreal-engine4


    【解决方案1】:

    我不确定,因为我大部分时间都为此使用蓝图,但获取值的一种方法是将其绑定到方法。

    (AFPSCharacter 模板示例)

    PlayerInputComponent->BindAxis("MoveForward", this, &APawn::MoveForward);
    

    然后在方法中使用

    void AFPSCharacter::MoveForward(float Value){
    //for example print the val
     UE_LOG(LogTemp, Warning, TEXT("%f"), Value);
    }
    

    【讨论】:

      【解决方案2】:

      这也是我的第一个想法,但不幸的是您无法获得键,您只需将方法绑定到轴名称。 MoveForward 的“Value”参数保存比例值,但不保存 Key 值。例如,当您按“W”时为 1.0f,当您按“S”时为 -1.0f。

      我已经为我的问题找到了解决方案。 GetAxisValue(FName) 是用于此目的的错误方法。

      我发现 UInputSettings 类包含一个名为 GetAxisMappingByName(FName, TArray) 的方法

      这里有一个代码 sn-p 它是如何工作的:

      // Get the instance of the InputSettings
      UInputSettings* InputSettings = UInputSettings::GetInputSettings();
      
      // AxisMappings with all the information will be stored here
      TArray<FInputAxisKeyMapping> VerticalKeys;
      TArray<FInputAxisKeyMapping> HorizontalKeys;
      
      // Load the AxisMappings
      InputSettings->GetAxisMappingByName(FName("MoveForward"), VerticalKeys);
      InputSettings->GetAxisMappingByName(FName("MoveRight"), HorizontalKeys);
      
      // Each MovementKey gets its own variable
      FKey ForwardKey;
      FKey BackKey;
      FKey LeftKey;
      FKey RightKey;
      
      // Assign each key to the correct direction
      for (FInputAxisKeyMapping verticalKey : VerticalKeys)
      {
          if (verticalKey.Scale == 1.0f)
              ForwardKey = verticalKey.Key;
          else if (verticalKey.Scale == -1.0f)
              BackKey = verticalKey.Key;
      }
      
      for (FInputAxisKeyMapping horizontalKey : HorizontalKeys)
      {
          if (horizontalKey.Scale == 1.0f)
              RightKey = horizontalKey.Key;
          else if (horizontalKey.Scale == -1.0f)
              LeftKey = horizontalKey.Key;
      }
      

      有没有比使用 for 循环更好的分配键的解决方案?请随意更正我的代码,因为我并不是真正的 C++ 专家。 ;-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-21
        • 2016-08-18
        • 2021-08-24
        • 1970-01-01
        相关资源
        最近更新 更多