How can I make UFUNCTION(exec) work inside #if !UE_BUILD_SHIPPING?

Hello!–

I would like to have a debug function as follows:

#if !UE_BUILD_SHIPPING

	bool DebugEnabled;

    /* Enables Debug */
    UFUNCTION(Exec)
	void ShowMyDebug(bool param);

 #endif

However, it seems that the Exec does not persist when written in this way. The function and variable are created and may be called upon at runtime. If I comment out the preprocesser lines, the function does properly show up in the console.

I am doing this to create very simple debug functionality that can be run in the editor or in a normal development runtime build that is not “SHIP”

Any tips or suggestions to solve this would be much appreciated!

Thanks,
J

To understand this problem you need to understand that U-macros (UFUNCTION, UCLASS etc.) are only used by Unreal Header Tool (UHT) which based on those generates header files which allows engine to see class structure and those macros are ignored during compile time.

I heard that UHT has problems parsing other C++ preprocessing calls so that might be the cause :stuck_out_tongue:

Ahh, thanks for the insight!
I assumed it was something like this.

Any known workaround or fix?

Maybe you could just do the same thing but inside the .cpp so the function altho it still exec, only does nothing when shipping

void MyClass::ShowMyDebug(bool param)
{
   #if !UE_BUILD_SHIPPING
    //Code here
   #endif
}

We had the same problem on our project.
I found that using the existing UCheatManager system makes adding debug functions rather easy.

First, extend UCheatManager in your own class:

UCLASS()
class UMyCheatManager : public UCheatManager
{
	GENERATED_BODY()
public:

	UFUNCTION(exec)
	void MyDebugFunction();
};

Then, implement it in the constructor of your player controller class:

AMyPlayerController::AMyPlayerController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	CheatClass = UMyCheatManager::StaticClass();
	...
}

The APlayerController class has the code for enabling and disabling cheats (debug functions) based on build type and gamemode settings.