I do not understand why the user input gets saved to the Input variable

Hello,
I am absolutely loving the course so far, but do not fully understand what’s going on in the below code:

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter

I know this lesson is specifically made to explain this, but I still do not understand. What is it about this code, that specifically tells the computer to save the user input to the const Input ? Is this OnInput function written somewhere else, and we are just accessing it from outside? I cannot find it in the Unreal documentation
Would really appreciate an explanation,
thanks in advance!

Read This File Source/BullCowGame/Console/Terminal.cpp

// When You Press Any Key Then UTerminal::OnKeyDown Is Executing
void UTerminal::ActivateTerminal()
{
    FInputKeyBinding PressedBinding(EKeys::AnyKey, EInputEvent::IE_Pressed);
    PressedBinding.KeyDelegate.BindDelegate(this, &UTerminal::OnKeyDown); 
}

// Here If You Pressed A Button Which Is Not "Enter Button" Then The Key Will Be Added To InputLine As FString 
//And If You Press Enter Then AcceptInputLine() Will Execute
void UTerminal::OnKeyDown(FKey Key)
{
	if (Key == EKeys::Enter)
	{
		AcceptInputLine();
	}

	if (Key == EKeys::BackSpace)
	{
		Backspace();
	}

    const FString KeyString = GetKeyString(Key);
    const FModifierKeysState KeyState = FSlateApplication::Get().GetModifierKeys();
	if (KeyState.IsShiftDown() || KeyState.AreCapsLocked())
	{
		InputLine += KeyString.ToUpper(); //InputLine Is Storing What You Type
	}
	else
	{
		InputLine += KeyString.ToLower();
	}

	UpdateText();
}

// Finally AcceptInputLine() Will Call The Function OnInput(const FString& Input) Of UBullCowCartridge 
void UTerminal::AcceptInputLine()
{
	Buffer.Emplace(GPrompt + InputLine);
	auto Cartridge = GetOwner()->FindComponentByClass<UCartridge>();
	if (Cartridge != nullptr)
	{
		Cartridge->OnInput(InputLine); // This Is The Line Where You Write BullCow Code [void UBullCowCartridge::OnInput(const FString& Input)]
	}
	InputLine = TEXT("");
}
1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms