Casting Variables that derive from Actor?

Hello!

I am having issues trying to get variables from another class. when I activate the ability I wish to execute, it crashes because of how I am attempting to cast it. Can anyone help me?

	if (bIsCharging)
	{
		ChargeAmount += DeltaTime;
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("CHARGING"));
		if (bIsNature)
		{
			ANatureAbility *ManaStuff = Cast<ANatureAbility>(GetActorClass());  //// NEED HELP HERE :(
			ManaStuff->ManaCost += DeltaTime;
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("NATURE_CHARGING"));
			Charge_Nature->SetRelativeScale3D(FVector(ChargeAmount, ChargeAmount, ChargeAmount));
			if (ChargeAmount > 3.0f)
			{
				ManaStuff->ManaCost = 50.f;
				Mana -= ManaStuff->ManaCost;
				ChargeAmount = 3.0f;
			}
		}

GetActorClass() returns UClass pointer which is class reference of your class which works like class identification, so it crash because cast dors not fit (As i suspect ANatureAbility is inherent to AActor which is in different class tree) . Why you actually doing that? Why are you not using varbales in actor direcly or use atleast use “this” pointer?

Because I’m not getting variables directly from actor.
Actually I should re explain myself :/. ANatureAbility-> AAbility-> AActor
That is tthe correct inheritance I have :/. I am trying to get a variable that is an int32, but I’m unsure how to cast to a class like this. I know how to with pawns and controllers… just not actors :confused:

Here’s ability.cpp

#include "SideScrollerConcept.h"
#include "Ability.h"


AAbility::AAbility(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	CollisionComp = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
	CollisionComp->InitSphereRadius(15.0f);
	RootComponent = CollisionComp;

	ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComp"));
	ProjectileMovement->InitialSpeed = 3000.0f;
	ProjectileMovement->UpdatedComponent = CollisionComp;
	ProjectileMovement->MaxSpeed = 3000.0f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;
	ProjectileMovement->Bounciness = 0.3f;

	ManaCost = 10;
}

here’s NatureAbility.cpp

#include "SideScrollerConcept.h"
#include "NatureAbility.h"
#include "ParticleDefinitions.h"


ANatureAbility::ANatureAbility(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

	NatureScale = PCIP.CreateDefaultSubobject<UParticleSystemComponent>(this, TEXT("NatureScale"));
	NatureScale->AttachTo(RootComponent);


}

I want the variable called ManaCost from Ability, Which NatureAbility is derived from.

this is not forum, use add comment to comment on answer, insted creating new anwser to your own quastion:p i think you can convert it back comment in arow menu

You really doing this weird way, you don’t need to cast anything aspecially in class it self, just add this variable to AAbility class (considering all abilities will have ManaCost) and refere it directly inside the class. Declare variable in header file (.h) in side the class like this.

 int32 ManaCost;

Or else you are trying to do call ManaCost from other class but then i don’t know why you using GetActorClass() which will return UClass ponter of class you currently writing in, which is useless to use any variables… use of UClass is pointless eitherway. So essential question where is the code piece you posted first, in NatureAbility class?

here, let me clarify a bit better. I’ll post all of my code. yes i have a int32 ManaCost in my header otherwise i wouldn’t be asking that sort of question. I am actually trying to cast it in my Character class. I apologize ahead of time for still being unclear :(.

Ability.h

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#pragma once

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

/**
 * 
 */
UCLASS()
class AAbility : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
	TSubobjectPtr<class USphereComponent> CollisionComp;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
	TSubobjectPtr<class UProjectileMovementComponent> ProjectileMovement;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mana)
	int32 ManaCost;
	
};

Ability.cpp

enter code here// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "SideScrollerConcept.h"
#include "Ability.h"


AAbility::AAbility(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	CollisionComp = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
	CollisionComp->InitSphereRadius(15.0f);
	RootComponent = CollisionComp;

	ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComp"));
	ProjectileMovement->InitialSpeed = 3000.0f;
	ProjectileMovement->UpdatedComponent = CollisionComp;
	ProjectileMovement->MaxSpeed = 3000.0f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;
	ProjectileMovement->Bounciness = 0.3f;

	ManaCost = 10;
}

NatureAbility.h

#pragma once

#include "Ability.h"

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

/**
 * 
 */
UCLASS()
class ANatureAbility : public AAbility
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Particle)
	TSubobjectPtr<UParticleSystemComponent> NatureScale;
	
};

NatureAbility.cpp

#include "SideScrollerConcept.h"
#include "NatureAbility.h"
#include "ParticleDefinitions.h"


ANatureAbility::ANatureAbility(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

	NatureScale = PCIP.CreateDefaultSubobject<UParticleSystemComponent>(this, TEXT("NatureScale"));
	NatureScale->AttachTo(RootComponent);


}

I cant fit everything in one comment, forgive me for the extra comments.
I am really trying to tell you that I want to cast it in my character code. but I am unsure how to cast it. I have everything needed to be included for the .h files, just need to know how to cast it.

Character = (AMyCharacter*)FunctionFromWhereYouGetCharacter();

You can get controlled pawn from PlayerController GetPawn();

Your problem is not casting, but what are you trying to cast from, or rether your design thinking. You tried to cast from GetActorClass(), i don’t know where you get that idea from, but you doing it wrong, it does not return your ability but UClass pointer of Character (as you saying you call from that class) :stuck_out_tongue: You need refence to your abilities, which previously you should spawn with ()->SpawnActor(ANatureAbility::StataicClass());, that function return pointer to your spawned actor, contain that in some TArray named abilities and then from that array get your abilities and call functions or get variables.

You should do set of functions in your character or player controller which manage AAbility array and use of it. You ability should have functions which controls abilities, not Character class.

I’m not trying to get my character type casted in my abilities. I’m trying to go my Nature ability class casted in my character class.

I do have it spawning once it’s done charging. Should I execute the mana costs when it’s spawned? ? Or should I make mana and mana cost be properties of the character instead?? Looking at the big picture it would be a better and easier idea?

Well Ability is a like a Weapon right? You learn it from something and you can have it or not and you can have more abilities which you select one and use it right? :slight_smile: So you need to think i more like inventory system but for abilities.