Adding your own console commands

Console commands are brilliant way to add commands to game that players can freely utilize. They can vastly change how the game works and looks. Players can type console commands to change game settings or just use them for cheating.  As for a programmer, console commands can be used for example to toggle debugging information. Fortunately, Unreal Engine supports console commands and they are extremely easy to use!


Starting up!

 

To test this, we are simply starting a new C++ project based on the first person template. After the project is loaded, we are going to define our console key. It can be changed from the Project settings -> Input screen. Select the key you want to use as a console key.

 


Dwelving inside the code

First, we are creating a simple console command that allows character to make a massive Jump. We define the console variables outside of the class scopes. So go ahead and open your characters .cpp file and type these lines above the constructor code.

static int32 CheatSuperJump = 0;
FAutoConsoleVariableRef CVARCheatSuperJump(
 TEXT("CHEATS.SuperJump"),
 CheatSuperJump,
 TEXT("Allows user to jump higher"),
 ECVF_SetByConsole);

Above we are specify a console variable that can be set through the console by user. Inside editor and inside game. Next, we are going create our own custom Jump function. Inside character header file we create function that can be either public, protected or private.

void CustomJump();

We declare it in .cpp and modify the jump height depending our console variable

void AMyCustomCharacter::CustomJump()
{
 if (CheatSuperJump == 1)
 GetCharacterMovement()->JumpZVelocity = 1200;
 else if (CheatSuperJump == 0)
 GetCharacterMovement()->JumpZVelocity = 420;
 Jump();
}

Now, for this to work, we also need to add one include and change our imput Jump function

#include "GameFramework/CharacterMovementComponent.h"
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCustomCharacter::CustomJump);

That’s all you need to do to make console command works. Inside game when you toggle console, you can see our new command there! The variable used inside code actually changes depending the value you set in console.

 


Console variables are useful for debugging purposes also. For example, we can set debugging lines behind the console variables, so we don’t always need to comment and uncomment the code when we wan’t to see results. If you dont want player to be able to change the console variable, instead flagging it with ECVF_SetByConsole, you can flag them as

ECVF_Cheat

That way, you can use them inside editor, but not in the packaged game!


Console commands using the exec parameter in UFUNCTION

Another cool way to create console commands is to use the exec parameter in UFUNCTION. This is super simple so let’s dive inside the code. Below is example of how to create a SuperJump with Exec

UFUNCTION(exec)
void SuperJump(bool Activate);

Notice, that I added a bool as parameter. This means, that when you start typing SuperJ… The console will tell you all the possible commands you can use after the SuperJump. In this case it would show like this:

In this case, you are not limited with int32:s, but you can use any variable you desire. After console command is launched, it will run that function with the logic you desire. Of course, you would implement the function normally inside .CPP.

 

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *