Help with casting[Reference for getting a mesh]

Here are the first two examples, to get a basic understanding of what im trying to do, at the bottom, you´ll find a function written in bold, so that you can easily see where my problem is. Hope everything is clear.


void ATest::WeaponCollected_Implementation()

{

	
	AHumanCharacter* MyCharacter = Cast<AHumanCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
	
	
	if (MyCharacter)

	{
		

		
		
		WeaponM3->AttachTo(MyCharacter->GetMesh(), "RightHand", EAttachLocation::SnapToTarget, EAttachLocation::KeepWorldPosition);

		bHasNade = true; 

		

		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "WeaponM3 pickedup");

	}
	

	
}

Now, in the AHumanCharacter.cpp, i call the function with overlappingactor:


void AHumanCharacter::TakePickup()
{

	TArray <AActor*> CollectedActors;
	SphereCollider->GetOverlappingActors(CollectedActors);
	float CollectedHealth = 0;
	for (int32 Collected = 0; Collected < CollectedActors.Num(); ++Collected)


        	ATest* const Weapon = Cast<ATest>(CollectedActors[Collected]);

                if (Weapon && !Weapon->IsPendingKill())
				{

					Weapon->WeaponCollected();
					
					bHasWeapon = true;


					

				}


so far so good.

In the AHumanCharacter.cpp i want now to call the Mesh WeaponM3 from the Test.cpp.


void AInHumanCharacter::Tick(float DeltaTime)
{

	Super::Tick(DeltaTime);

	if (MouseButtonDown && bHasWeapon)
	{

		
		AddForceNade();

		
	}

}

**void AInHumanCharacter::AddForceNade()
{

	ATest* WeaponM3Mesh = Cast<ATest>(GetMesh());
	if (WeaponM3Mesh)
	{
		WeaponM3Mesh->GetMesh()->SetSimulatePhysics(true)   // Just an example
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, "Weapon Function Kill being called");
	}
}


**

But GetMesh() doesnt work. I can´t get access to the mesh or atleast the DebugMessage doesnt get called. Any Ideas how i can call or get the mesh or what im doing wrong?

I’m not sure what your GetMesh( ) function returns but I’m guessing it’ll be a pointer to a USkeletalMeshComponent or UStaticMeshComponent. You are trying to cast the class type ATest into a USkeletalMeshComponent/UStaticMeshComponent type, which won’t work.

If you want to get the mesh for your ATest class, it would be something like:



ATest* WeaponM3 = Cast<ATest>( GetWeapon( ) );
if( WeaponM3 )
{
     USkeletalMeshComponent *M3Mesh = WeaponM3->GetMesh( );
     if( M3Mesh )
     {
          M3Mesh->SetSimulatedPhysics( true );
     }
}


GetWeapon( ) would be a function that would return the current weapon reference to the weapon being held by your Pawn/Character class. Also, you don’t necessarily need to cast the weapon if you have a direct reference to the weapon type you are looking for. As in, if GetWeapon returns a pointer to the type ATest, you do not need to cast a local reference to that as ATest, as it is already that type. You can just do GetWeapon( )->GetMesh( ) …

Hey Vawx! Aren´t you the guy who also does some Blueprint tutorials on youtube? I think i´ve already heard your name somewhere.

Anyways, could you explain or show at an easily example how the GetWeapon function would look like?

right now, i´ve tried to return the WeaponM3 Mesh, which didn´t work.

Here is an example:

MyGameMode.cpp



/**
	https://forums.unrealengine.com/showthread.php?113708-Help-with-casting-Reference-for-getting-a-mesh&p=546838#post546838
*/

#include "T.h"
#include "TGameMode.h"

#include "MyCharacter.h"

ATGameMode::ATGameMode( )
{
	DefaultPawnClass = AMyCharacter::StaticClass( );
}



MyCharacter.h




/**
	https://forums.unrealengine.com/showthread.php?113708-Help-with-casting-Reference-for-getting-a-mesh&p=546838#post546838
*/
#pragma once

#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"

UCLASS()
class T_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	AMyCharacter();
	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	/** returns current weapon held */
	FORCEINLINE class AMyWeapon* GetCurrentWeapon( ) { return CurrentWeapon; }

	/** returns socket name for weapon attachment */
	FORCEINLINE FName GetWeaponAttachSocketName( ) { return WeaponAttachmentSocketName; }

protected:

	// Camera
	UCameraComponent *Camera;
	USpringArmComponent *CameraArm;

	// create and attach weapon
	virtual void GiveInitialWeapon( );

	// -Help-with-casting-Reference-for-getting-a-mesh
	virtual void AddForceNade( );

	// attachment name and weapon
	FName WeaponAttachmentSocketName;
	class AMyWeapon *CurrentWeapon;
};



MyCharacter.cpp



/**
	https://forums.unrealengine.com/showthread.php?113708-Help-with-casting-Reference-for-getting-a-mesh&p=546838#post546838
*/
#include "T.h"
#include "MyCharacter.h"

#include "MyWeapon.h"

AMyCharacter::AMyCharacter()
{
	PrimaryActorTick.bCanEverTick = true;

	// Default mesh asset
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshAsset( TEXT( "SkeletalMesh'/Engine/Tutorial/SubEditors/TutorialAssets/Character/TutorialTPP.TutorialTPP'" ) );
	if( MeshAsset.Succeeded( ) )
	{
		// Set mesh asset
		GetMesh( )->SetSkeletalMesh( MeshAsset.Object );

		// Orient mesh to face correct direction at correct height within capsule
		GetMesh( )->SetRelativeLocation( FVector( 0.f, 0.f, -90.f ) );
		GetMesh( )->SetRelativeRotation( FRotator( 0.f, -90.f, 0.f ) );
	}

	// Create camera spring arm
	CameraArm = CreateDefaultSubobject<USpringArmComponent>( TEXT( "SPRING_ARM" ) );
	if( CameraArm )
	{
		// Attach to and set distance away from player
		CameraArm->AttachTo( RootComponent );
		CameraArm->TargetArmLength = 300.f;

		// create camera
		Camera = CreateDefaultSubobject<UCameraComponent>( TEXT( "CAMERA" ) );
		if( Camera )
		{
			// Attach camera to arm
			Camera->AttachTo( CameraArm );	
		}	
	}

	// Attachment location on default mesh
	WeaponAttachmentSocketName = FName( "hand_r" );
}

void AMyCharacter::BeginPlay()
{
	Super::BeginPlay();

	// Give weapon at start
	GiveInitialWeapon( );
}

// Temporary to show giving a weapon to a character 
// This should be generalized to "GiveWeapon( WeaponType ) 
void AMyCharacter::GiveInitialWeapon( )
{
	CurrentWeapon = GetWorld( )->SpawnActor<AMyWeapon>( );
	if( CurrentWeapon )
	{
		CurrentWeapon->AttachToCharacter( this );
	}
}

**// Call this when you want to...
void AMyCharacter::AddForceNade( )
{
	if( CurrentWeapon )
	{
		CurrentWeapon->GetMesh( )->SetSimulatePhysics( true );
		GEngine->AddOnScreenDebugMessage( -1, 15.f, FColor::Yellow, "Weapon Function Kill being called" );
	}
}**

// Not used in example
void AMyCharacter::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}

// Not used in example
void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

}




MyWeapon.h



/**
	https://forums.unrealengine.com/showthread.php?113708-Help-with-casting-Reference-for-getting-a-mesh&p=546838#post546838
*/
#pragma once

#include "GameFramework/Actor.h"
#include "MyWeapon.generated.h"

UCLASS()
class T_API AMyWeapon : public AActor
{
	GENERATED_BODY()
	
public:	
	AMyWeapon();
	virtual void BeginPlay() override;

	/** Attach this weapon to a character */
	virtual void AttachToCharacter( ACharacter *Character );

	/** Returns SkeletalMeshComp for Weapon */
	FORCEINLINE USkeletalMeshComponent *GetMesh( ) { return Mesh; }

protected:

	USkeletalMeshComponent *Mesh;
	
};



MyWeapon.cpp



/**
	https://forums.unrealengine.com/showthread.php?113708-Help-with-casting-Reference-for-getting-a-mesh&p=546838#post546838
*/
#include "T.h"
#include "MyWeapon.h"

#include "MyCharacter.h"

AMyWeapon::AMyWeapon()
{
	// Doesnt need to tick 
	PrimaryActorTick.bCanEverTick = false;

	// Create mesh for weapon
	Mesh = CreateDefaultSubobject<USkeletalMeshComponent>( TEXT( "MESH" ) );
	if( Mesh )
	{
		static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshAsset( TEXT("SkeletalMesh'/Engine/EngineMeshes/SkeletalCube.SkeletalCube'") );
		if( MeshAsset.Succeeded( ) )
		{
			// Assign asset from content folder to mesh
			Mesh->SetSkeletalMesh( MeshAsset.Object );
		}
	}
}

// Attach mesh to character
void AMyWeapon::AttachToCharacter( ACharacter *Character )
{
	// Cast to "our" character
	AMyCharacter *AttachCharacter = Cast<AMyCharacter>( Character );
	if( AttachCharacter )
	{
		// Attach weapon mesh to character, using character WeaponAttachmentSocketName
		GetMesh( )->AttachTo( AttachCharacter->GetMesh( ), AttachCharacter->GetWeaponAttachSocketName( ), EAttachLocation::SnapToTarget );
	}
}

// Not used in example
void AMyWeapon::BeginPlay()
{
	Super::BeginPlay();	
}



I don’t make any YouTube videos. Must be somebody else!