I know some basics of c ++ but "CreateDefaultSubobject<USceneComponent>(TEXT("muzzel location"));" i don't understand this syntax like what is "USeceneComponent"?

i know some basics of c ++ but “CreateDefaultSubobject(TEXT(“muzzel location”));” i can’t understand this syntax like what is “USeceneComponent” ? is it class ? and if it is then what is “CreateDefaultSubobject” ?

please Help me ik i am so dumb :slightly_smiling_face:

CreateDefaultSubobject creates a component. Like when in Actor Blueprint you press Add and select a component class.

USceneComponent is a class, you’re right. Basically, SceneComponent is a component to which you can set Location, Rotation, and Scale.

So if you want to add a component to your actor, you declare it in .h:
USceneComponent* MyScene;
and then assign value to it in the constructor in .cpp:
MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("My Component Name"));

2 Likes

CreateDefaultSubobject is a template function. The argument in the angles is the type argument required by that template function.

You can open up the definition of that function in the editor – select it and press F12. Then you can see how it’s implemented.

	template<class TReturnType>
	TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
	{
		UClass* ReturnType = TReturnType::StaticClass();
		return static_cast<TReturnType*>(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, bTransient));
	}

You will note that it gets the UClass that goes with the type you want, and pass that along to a non-template overload of CreateDefaultSubobject(), and then casts the returned value to the type you requested.

Googling for “C++ template function” and “function overload” will hopefully find a variety of references you can use to read more about this. It’s used all over, so pretty important to learn!

Got It Thanks :hugs:

Template function ! …. Got It I Don’t know Anything about Template function
Thank You For Your explanation