Weapon system, TSubClassOf

Is it a good idea use TSubClassOf for weapon system ?

Now I have my WeaponActot cpp as a base class and derived blueprint for sword.
In my character cpp class I have next:

	UPROPERTY(VisibleAnywhere, Category = Combat)
	AWeaponActor* RightHandWeapon;
	
	UPROPERTY(EditAnywhere, Category = Combat)
	TSubclassOf<AWeaponActor> RightHandWeaponClass;

	UPROPERTY(VisibleAnywhere, Category = Combat)
	AWeaponActor* LeftHandWeapon;
	
	UPROPERTY(EditAnywhere, Category = Combat)
	TSubclassOf<AWeaponActor> LeftHandWeaponClass;

While I didn’t use TSubClassOf for some reason I couldn’t set my weapons in blueprints.
So I wish to know is it any ways to escape two additional class fields and still be able to set my weapon in blueprints and have access to them in cpp class.

Even batter if I will can see my LeftHandWeapon and RightHandWeapon right in class components.

For you WeaponComponents to show up in the left hand side field (Where the other components are), make sure to have it derive from UActorComponent.

You have to create the Component in the constructor and set the weapon it should represent somewhere else (BeginPlay, PostInitProperties, or any function where you want your weapons to be “shown”). You can use the TSubclassOf for the variables which store the type of weapon that should be represented, which I assume is what you are trying to do already.

It probably isn’t showing your components, because you didn’t initialize them in the constructor:

There’s a function for exactly your use case actually:

RightHandWeapon = CreateDefaultSubobject(FName("RightHandWeapon"), RightHandWeaponClass, RightHandWeaponClass, true, false);

CreateDefaultSubobject

Thanks for you reply. I think I don’t see them because I derived from Actor, not ActorComponent.
In BeginPlay I have:

	if (RightHandWeaponClass)
	{
		RightHandWeapon = GetWorld()->SpawnActor<AWeaponActor>(RightHandWeaponClass, WeaponTransform, SpawnParameters);
		RightHandWeapon->MeshComp->AttachToComponent(GetMesh(), AttachmentTransformRules, "RightHandSocket");
	}
	
	if (LeftHandWeaponClass)
	{
		LeftHandWeapon = GetWorld()->SpawnActor<AWeaponActor>(LeftHandWeaponClass, WeaponTransform, SpawnParameters);
		LeftHandWeapon->MeshComp->AttachToComponent(GetMesh(), AttachmentTransformRules, "LeftHandSocket");
	}

But for me is not clear why I can’t chose weapon here and must add special filed for it:

You are using VisibleAnywhere instead of EditAnywhere.

1 Like

Yep, I know. But if I change to EditAnewhere I still can’t do this.
There is simply no way to select the desired class. For some reason, the map is only in the dropdown list

The problem is you want to set an object (actor) of a subclass of AWeaponActor. You’d have to specify the blueprint class, but I’m not sure how. I think there was a way to solve your problem, I’ll have to dig a bit, but I’ll let you know if I figure it out.

1 Like