簡體   English   中英

虛幻引擎 - 如何通過 C++ 代碼獲取 AxisMappings

[英]Unreal Engine - How to get the AxisMappings via C++ code

我是虛幻引擎和 C++ 的新手...

我正在嘗試獲取軸的分配鍵。 我發現這些信息存儲在 DefaultInput.ini 文件中,但我如何以編程方式訪問這些數據?

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

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

我究竟做錯了什么? 我將衷心感謝您的幫助。

我不確定,因為我大部分時間都使用藍圖,但獲取值的一種方法是將其綁定到方法。

(AFPSCharacter 模板示例)

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

然后在方法中使用它

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

這也是我的第一個想法,但不幸的是您無法獲得密鑰,您只需將方法綁定到軸名稱即可。 MoveForward 的“值”參數保存比例值,但不保存鍵值。 例如,按“W”時為 1.0f,按“S”時為 -1.0f。

我已經為我的問題找到了解決方案。 GetAxisValue(FName) 是用於此目的的錯誤方法。

相反,我找到了包含名為 GetAxisMappingByName(FName, TArray) 的方法的 UInputSettings 類

這是一個代碼片段,它是如何工作的:

// 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++ 專家。 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM