While our game may be running without any issues in the editor or even in…
Creating console commands
In this post we’re going to create our own custom console commands. Unreal Engine 4 provides a specifier named Exec, for the UFUNCTION macro which declares that the following function can be executed through the console window of the engine (to open up the console window press the ~ key in your keyboard). However, there is a catch – currently only specific classes execute the said functions. According to UE4 answershub, these classes are:
- Pawn
- PlayerController
- CheatManager
- GameMode
- PlayerInput and
- HUD
Creating console commands
For this post, I’ve created a blank C++ project. Then, I opened up the header file of my game mode and added the following functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public: /*Function with no parameters*/ UFUNCTION(Exec, Category = ExecFunctions) void DoSomething(); /*Function with one parameter*/ UFUNCTION(Exec, Category = ExecFunctions) void DoSomethingElse(float param); /*Function with two parameters*/ UFUNCTION(Exec, Category = ExecFunctions) void DoubleParamFunction(float param1, int32 param2); |
Here is their implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void AConsoleExecsGameMode::DoSomething() { GLog->Log("doing something"); } void AConsoleExecsGameMode::DoSomethingElse(float param) { GLog->Log("Param: " + FString::SanitizeFloat(param)); } void AConsoleExecsGameMode::DoubleParamFunction(float param1, int32 param2) { GLog->Log("Param1: "+FString::SanitizeFloat(param1)+" - Param2: " +FString::FromInt(param2)); } |
Save and compile your code, then in the default map in the World Settings make sure to specify your c++ (or the derived BP) game mode in the game mode override option:
Then, when you press play if you open up your console you can execute the functions we’ve created above by typing their name and pressing enter.
In order to pass multiple parameters in the console window, type the name of your function and space your pamateres like so:
DoubleParamFunction 123.4323 5000Here is my output after calling each function:
This Post Has 0 Comments