How to avoid name collisions in C ++ class names

What steps are you taking to avoid name collisions in class names?

I myself use a unique method for class names.

The ultimate solution is to use namespaces,
Namespaces are not available in Unreal C ++.

So, as I said earlier, the class name
I can only think of a way to give it a unique name.

Let’s say I want to have five children, and I really like the name “Tom.” But what if I have more than one son? How do I avoid naming both my children “Tom?”

1 Like

In c++ class name collisions can be easily avoided using includes.
If they are exposed to the Blueprint system as UClass however, that’s a different story as the UAssets and c++ types will all show up on a big pile within the editor.

First thing is using a naming system. In Blueprint we use a naming system like this one:

In c++ you will notice many engine source files do not use prefixes like the above naming system but suffixes, like “ActorComponent”, “Subsystem”, “Widget”, “Character”.

Let’s create a human character, deriving from the UCharacter class.
This class, “UHumanCharacter”, has a property “Title” which is this human’s name.
We shouldn’t create another subclass for Tom from UHumanCharacter.
Tom is just an implementation of UHumanCharacter itself.
We can create a new UHumanCharacter object by spawning it.
Then, we can set the name of this UHumanCharacter to “Tom”. Now we can have hundreds of Toms (instances) running around which are very different actors with the same name, of the same class.

That said it is unlikely you will ever run into name collisions even when all your classes show up at once in the Blueprint Editor as long as you use a naming system for Blueprint Assets like the one I posted and if you use a suffix for c++ files / classes similar to what the engine source files are called.

That was a joke! I was jokingly making the point “give names that aren’t re-used!”

1 Like

“These are all humans, but that one, is Dave. Dave is not like the others” :rofl:

#pragma once

#include "HumanCharacter.h"

#include "DaveCharacter.generated.h"


UCLASS(BlueprintType)
class GAMECORE_API ADaveCharacter : public AHumanCharacter {
    GENERATED_BODY()

private:

	UPROPERTY()
		bool bIsSpecial = true;

public:
	
	ADaveCharacter() {
		Title = "VeryMuchDave";
	}

};


1 Like

To explain my situation, I created Plugins and in it
I have created a C ++ Class that inherits from PawnMovementComponent.

The inherited class name is
“PhysicsMovementComponent”
I named it.

The created Class is published to Blueprint as UClass.
When I completed the implementation and packaged it with UE 4.27, there was no problem.

However, when I packaged with UE 5.0, an error occurred and Package could not be performed if there was a duplicate class name.

Probably UE 5.0 added some Classes on the engine side.

When you actually check the engine code, in UE5
“PhysicsMovement.h”
“PhysicsMovement.cpp”
Class is added.

Error messages
“Error: Duplicate class name”
It is that.

However, such an error does not occur when creating a Class, but it occurs at Package.

I used the class name to avoid errors
“PhysicsMovementComponent”
from
“PhysicsBasedMovement”
When I changed it to, I was able to package normally

1 Like

Renaming like you did is probably best in that situation. I tend to put most functionality in plugins, then prefix the class name with an abbreviated plugin name ie: ACGLCharacter for a plugin CoolGameLogic. You ran into an unfortunate situation where you coincidentally named a class they introduced. Doesn’t happen often, I’d think.

You may want to read up on redirectors, not necessarily a fix but related:

1 Like

Oh, thank you. A better solution is to add a preflex such as Plugins.

I also try to take that approach.

I’m a little worried about the long name, but at the moment it seems to be the best solution.

It would be nice if there was a standard way to avoid class name collisions, such as namespace support …