Unreal Engine Tip #2 – Expand Enumerations as Execs

NOTE: This tip requires basic C++ knowledge

Whenever i find something useful, i want to share it with others. Something recently that caught my eye was the ability to give blueprint nodes multiple outputs. Which output is executed is specified by your logic. This is done via the “ExpandEnumAsExecs” meta specifier for UFUNCTIONs.

Here is an example to get things started. The example we are going to use features an NPC and their dog. After the mission has been activated, the NPC will give the user a “find dog” mission when they speak to the player.

Logically speaking, When the NPC talks to the player; the blueprint node will check the current mission status. Afterwards it will execute a specific set of blueprint nodes based on the status of the mission.

NOTE: I also added some extra functions (Speak to Player, Activate Find Dog Mission, Give Player Rewards, and Mark Mission as Complete) to the screenshot to assist in understanding real world application of the mission.

Here you can see the NPC will say something different for each status:

  • Before the mission is active (aka No Mission) – Hello There! This is my dog ruffles.
  • When the mission is active, but not accepted – I lost my dog! Can you find it for me?
  • While the mission is active – Have you found my dog yet?
  • Once you’ve completed the mission’s requirements (found her dog) – Ruffles! Thank you. Take this as a reward.
  • After you have received your reward – Thank you for finding my dog!

Why would i want to do this?

This tip isn’t required for game development in unreal. It’s purpose is to help assist other developers. A developer would want to do this to simplify blueprint logic. Keeping a blueprint clean and readable is important when creating games using them.

Setting up the code

First you need an enumeration. This enumeration must be created in a c++ class.

// ANPCSurvivorCharacter.H
UENUM(BlueprintType)
enum class ENPCMissionStatus : uint8
{
	No_Mission,
	New_Mission,
	Active_Mission,
	Mission_Requirements_Completed,
	Mission_Completed
};

After your enumeration is complete, you need to declare a ‘BlueprintCallable’ UFUNCTION. This UFUNCTION should take in an out reference to the previously created function . In the meta data for this function, the “ExpandEnumAsExecs” specifier should be set. It’s value should be the name of the associated parameter.

// ANPCSurvivorCharacter.H
UCLASS()
class SOMESURVIVALGAME_API ANPCSurvivorCharacter : public ACharacter
{
	GENERATED_BODY()
	
public:

	UFUNCTION(BlueprintCallable, Category = "NPC|Missions", Meta = (ExpandEnumAsExecs = "MissionStatus"))
		void GetMissionStatus(ENPCMissionStatus & MissionStatus);
};

The above, once defiend properly will look like such:

ExpandEnumAsExecs = “MissionStatus”

Finally, you need to define the the previously declared function. In the function definition, you need to set the value of the passed in enumeration parameter.

// ANPCSurvivorCharacter.cpp
void ANPCSurvivorCharacter::GetMissionStatus(ENPCMissionStatus & MissionStatus)
{

	bool HasMission = false; // ... Implement your logic here
	bool IsMissionAccepted = false; // ... Implement your logic here
	bool HasCompletedMissionRequirements = false; // ... Implement your logic here
	bool IsMissionComplete = false; // ... Implement your logic here

	if (!HasMission) {

		MissionStatus = ENPCMissionStatus::No_Mission;
	}
	else if(!IsMissionAccepted){

		MissionStatus = ENPCMissionStatus::New_Mission;
	}
	else if (!IsMissionComplete) {

		MissionStatus = ENPCMissionStatus::Active_Mission;
	}
	else if (HasCompletedMissionRequirements) {

		MissionStatus = ENPCMissionStatus::Mission_Requirements_Completed;
	}else {

		MissionStatus = ENPCMissionStatus::Mission_Completed;
	}
}

Thats it. The example chosen should be pretty easy to understand, but this can be applied to anything your game needs.

Unreal Engine Tip#1 – Disable Automatically Compiling New C++ Classes

If you’re developing a game in Unreal Engine and you are using the C++ API, this tip should save you some time. In the Engine’s editor preferences, you can disable automatically compilation when new c++ classes are added.

NOTE: This tip is for those using c++ projects. The tip also doesn’t require much technical knowledge, so blueprint users should be able to understand it as well.

Type “compile” in the search box to find the setting, then make sure the checkbox is unchecked.

Why would i do this?

I do this to avoid having to wait unnecessarily when creating multiple classes. Compilation after creating new classes doesn’t take an abnormally long amount of time, but when you are in a groove it can be annoying. In addition, with the option unchecked; you can actually edit your newly created class before compilation. Being able to edit classes before manually compiling saves time as well.

Here’s an example to explain what i mean, and the gains of disabling this setting.

Example – Adding two classes.

Let’s say you want to add two more c++ classes to your Unreal project. For the sake of the example, let’s assume it takes your engine 1 minute to re-compile after creating a new c++ class, and it will take you 5 minutes to actually code the new classes.

With the setting enabled, here is how things would go:

  1. You create a new c++ class
  2. The engine re-compiles (1 minute)
  3. You create a second new c++ class
  4. The engine re-compiles again. (1 minute)
  5. You edit the created classes. (5 minutes)
  6. You manually re-compile. (1 minute)

If you disable the setting, you would cut out the first 2 re-compilations. The overall time difference would be as follows:

  • Automatic Compilation Enabled: 8 minutes
  • Automatic Compilation Disabled: 6 minutes

Wow! A WHOLE 2 MINUTES!! Yeah…. i know, not a big deal. But if you want to create 3 classes that turns into a 3 minute difference. Also, depending on your machine; what if re-compilation takes 2 minutes? Then that 8 minutes turns into 11 minutes, whereas the 6 minutes only turns into 7 minutes. If recompilation for your takes 2 minutes, there is a 4 minute difference.

It’s definitely not required for game development with Unreal Engine. I just thought i’d let other people know it’s there. Automatic compilation after creating c++ classes was very annoying for me for a while. I now do this at the start of every project.

T-th-th-Tha-That’s all folks. Thanks for reading, i hope it helps someone.