Custom MatchState in child class of GameMode

I have a class inheriting from default GameMode. I would like to add additional MatchStates to my own CustomGameMode but doing it like default GameMode does not work.

CustomGameMode.h
namespace MatchState  
{ 	
   extern ENGINE_API const FName CustomState; 
}
    
CustomGameMode.cpp
namespace MatchState
{
   const FName CustomState = FName(TEXT("CustomState"));
}

It fails to compile with “inconsistent dll linkage”.
Is there any way to implement custom match state so it works(for blueprint) the same way as the included ones by default.
.

Default GameMode.h
namespace MatchState
{
	extern ENGINE_API const FName EnteringMap;			// We are entering this map, actors are not yet ticking
	extern ENGINE_API const FName WaitingToStart;		// Actors are ticking, but the match has not yet started
	extern ENGINE_API const FName InProgress;			// Normal gameplay is occurring. Specific games will have their own state machine inside this state
	extern ENGINE_API const FName WaitingPostMatch;		// Match has ended so we aren't accepting new players, but actors are still ticking
	extern ENGINE_API const FName LeavingMap;			// We are transitioning out of the map to another location
	extern ENGINE_API const FName Aborted;				// Match has failed due to network issues or other problems, cannot continue

	// If a game needs to add additional states, you may need to override HasMatchStarted and HasMatchEnded to deal with the new states
	// Do not add any states before WaitingToStart or after WaitingPostMatch
}

Default GameMode.cpp
namespace MatchState
{
	const FName EnteringMap = FName(TEXT("EnteringMap"));
	const FName WaitingToStart = FName(TEXT("WaitingToStart"));
	const FName InProgress = FName(TEXT("InProgress"));
	const FName WaitingPostMatch = FName(TEXT("WaitingPostMatch"));
	const FName LeavingMap = FName(TEXT("LeavingMap"));
	const FName Aborted = FName(TEXT("Aborted"));
}

extern ENGINE_API

this is probably the issue, first of all ENGINE_API can be used only by engine (a name of) module, each module have macro on it own, your module name is the name of folder in source folder it inherent project name so it will look like this PROJECTNAME_API. 2nd issue is function of that macro… it places extern :stuck_out_tongue: when engine does modular build non-monolithic, when engine is build monolithic it does not need extern in that place, so it place nothing. In other owrds you don’t need extern at beginning because macro already place it when it needed, in fact as result you will have extern extern in that place.

Overall… i think you don’t need extern ENGINE_API at all, you only need this if you going to use it outside of your code module and majority of people (and i susspect including you assuming you making game project) have no idea about UE4 module system at all, lot of people writing C++ in UE4 don’t have idea they making module. Or else you writing a plugin, in majority of cases you don’t need this.

Also making custom states won’t magically make them work with blueprints as default once, you most likely will need to make some extra custom nodes to make it work same way.

Also just declare those states in only header file with values same as original GameMode.h does, those are const variables so compiler won’t generate anything from this when you include header file. Compiler only acknowledges const valuable value and paste in that value when variable is used else where, if not used there is no trace of it in dll, so it safe to fully declere it in header file.

1 Like