Doubt anyone will undestand, but screw it.
I wanted to separate both Player and Enemy Character blueprints, and be parented to Master Character that holds my Custom Locomotion System. Thing is, I’m not sure, what should I do for separate Animation Blueprints as well.
Or is there better way? And can someone point out what am I doing wrong?
I’m on the brink of losing my mind over, and my script’s in ruins by pseudo-experts. Not even Copilot can help me.
Blueprints:
CharacterMaster.h
#include "CoreMinimal.h"
#include "ProjectName/GameData/Character/ch/GD_Character_Base.h" // Enums, Structures and Data Assets storage
#include "Character_Master.generated.h" // Header Top
// Use forward class declaration to avoid circular dependency
class UCharacter_CustomLocomotionSystem;
public: // Variables
// Component Declaration: Custom Locomotion System
UCharacter_CustomLocomotionSystem* ACO_CLS;
CharacterMaster.cpp
// Create Subobjects
// Create and attach the subobject: Custom Locomotion System
ACO_ELS = CreateDefaultSubobject<UCharacter_CustomLocomotionSystem>(TEXT("ACO_CLS"));
CharacterPlayer.h
#include "CoreMinimal.h"
#include "ProjectName/Character/ch/Character_Master.h" // Parent
// Enums, Structures and Data Assets storage
#include "ProjectName/GameData/Character/ch/GD_Character_Player.h"
#include "ProjectName/GameData/Character/wp/GD_Weapon.h"
#include "ProjectName/GameData/Player/Input/GD_Input.h"
// Header Top
#include "Character_Player.generated.h"
// Use forward class declaration to avoid circular dependency
class UAnimInstance_Player;
class UCharacter_CustomLocomotionSystem;
UCLASS()
class PROJECTNAME_API ACharacter_Player : public ACharacter_Master
{
public: // Variables
ACharacter_Master* Character_Master;
ACharacter_Player* Character_Player;
UAnimInstance_Player* AnimBP_PlayerCharacter;
}
Character_Player.cpp
#include "ProjectName/Character/ch/Character_Player.h"
#include "ProjectName/Components/Character/CustomLocomotionSys.h"
void ACharacter_Player::BeginPlay()
{
Super::BeginPlay();
// Get Owner of Master Character
Character_Master = Cast<ACharacter_Master>(GetOwner());
// Get Owner of Master Character
Character_Master = Cast<ACharacter_Master>(GetOwner());
if (Character_Player)
{
// Access Locomotion System
ACO_CLS = Character_Player->ACO_CLS;
// Get Player Animation Blueprint
AnimBP_PlayerCharacter = Cast<UAnimInstance_Player>(Character_Player->GetMesh()->GetAnimInstance());
// Send Animation Blueprint to Locomotion System
ACO_CLS->AnimInstance_Player = AnimBP_PlayerCharacter;
}
}
CustomLocomotionSystem.h
#include "ProjectName/Character/ch/Character_Master.h"
#include "ProjectName/GameData/Components/Character/GD_CustomLocomotionSys.h" // Enums, Structures and Data Assets storage
// Use forward class declaration to avoid circular dependency
class ACharacter_Master;
class UAnimInstance_Player;
class UAnimInstance_Enemy;
public: // Variables
ACharacter_Master* CharacterMaster;
UAnimInstance_Player* AnimInstance_Player;
UAnimInstance_Enemy* AnimInstance_Enemy;
CustomLocomotionSystem.cpp
#include "ProjectName/Components/Character/CustomLocomotionSys.h"
#include "Components/CapsuleComponent.h"
#include "Components/BoxComponent.h"
#include "DrawDebugHelpers.h"
void UCharacter_CustomLocomotionSystem::BPF_Fetch_AnimInstance_OfChild()
{
// Attempt to cast to child character types
AActor* Owner = GetOwner();
if (Owner)
{
CharacterMaster = Cast<ACharacter_Master>(Owner);
if (CharacterMaster)
{
USkeletalMeshComponent* Mesh = CharacterMaster->GetMesh();
if (Mesh)
{
// Try to cast to both Player and Enemy Anim Instances
AnimInstance_Player = Cast<UAnimInstance_Player>(Mesh->GetAnimInstance());
if (!AnimInstance_Player)
{
AnimInstance_Enemy = Cast<UAnimInstance_Enemy>(Mesh->GetAnimInstance());
}
}
}
}
}
AnimInstance_Player.h
UCLASS()
class PROJECTNAME_API UAnimInstance_Player : public UAnimInstance
{
GENERATED_BODY()
// Master Character Parent holding Custom Locomotion component
ACharacter_Master* OWN_CharacterMaster;
ACharacter_Player* OWN_CharacterPlayer;
UCharacter_CustomLocomotionSystem* CO_CLS;
}
AnimInstance_Player.cpp
// Get the owning actor and try to cast it to ACharacter_Player
AActor* Owner = GetOwningActor();
if (Owner)
{
OWN_CharacterPlayer = Cast<ACharacter_Player>(Owner);
if (OWN_CharacterPlayer)
{
OWN_CharacterMaster = Cast<ACharacter_Master>(Owner);
// Access the locomotion system (ACO_CLS) from the master character
if (OWN_CharacterMaster)
{
CO_CLS = OWN_CharacterMaster->ACO_CLS;
}
}
}