Download

Cast problem

I am having trouble with this part of code which i did while following the Shooter example :

AProjectile class :



#pragma once

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

/**
 * 
 */
UCLASS(Abstract, Blueprintable)
class ARENA_API AProjectile : public AActor
{
	GENERATED_UCLASS_BODY()

	...
}


Line of code in another class :


AProjectile* Projectile = Cast<AProjectile>(UGameplayStatics::BeginSpawningActorFromClass(this, MagicConfig.ProjectileClass, SpawnTM));

Error :


1>E:\Google Drive\UE4\Projects\Arena\Source\Arena\Private\Magics\Magic.cpp(30): error C2275: 'AProjectile' : illegal use of this type as an expression
1>          E:\Google Drive\UE4\Projects\Arena\Source\Arena\Classes/Projectiles/Projectile.h(13) : see declaration of 'AProjectile'

It’s very similar to the shooter example but i don’t understand the error :confused:

Are you sure you are including the AProjectile.h?

Edit: Yes you are including it.

Can you post AProjectile.h?

Here is the AProjectile.h :



#pragma once

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

/**
 * 
 */
UCLASS(Abstract, Blueprintable)
class ARENA_API AProjectile : public AActor
{
	GENERATED_UCLASS_BODY()

	/** initial setup */
	virtual void PostInitializeComponents() override;

	/** setup velocity */
	void InitVelocity(FVector& ShootDirection);

	/** handle hit */
	UFUNCTION()
	void OnImpact(const FHitResult& HitResult);


protected:

	/** movement component */
	UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
	TSubobjectPtr<UProjectileMovementComponent> MovementComp;

	/** collisions */
	UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
		TSubobjectPtr<USphereComponent> CollisionComp;

	UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
	TSubobjectPtr<UParticleSystemComponent> ParticleComp;



	/** life time */
	UPROPERTY(EditDefaultsOnly, Category = ProjectileStat)
	float ProjectileLife;

	/** damage at impact point */
	UPROPERTY(EditDefaultsOnly, Category = ProjectileStat)
	int32 ExplosionDamage;

	/** radius of damage */
	UPROPERTY(EditDefaultsOnly, Category = ProjectileStat)
	float ExplosionRadius;

	/** type of damage */
	UPROPERTY(EditDefaultsOnly, Category = WeaponStat)
	TSubclassOf<UDamageType> DamageType;


	/** effects for explosion */
	//UPROPERTY(EditDefaultsOnly, Category = Effects)
	TSubclassOf<class AExplosionEffect> ExplosionTemplate;

	/** controller that fired me (cache for damage calculations) */
	TWeakObjectPtr<AController> MyController;

	/** did it explode? */
	UPROPERTY(Transient, ReplicatedUsing = OnRep_Exploded)
	bool bExploded;

	/** [client] explosion happened */
	UFUNCTION()
	void OnRep_Exploded();

	/** trigger explosion */
	void Explode(const FHitResult& Impact);

	/** shutdown projectile and prepare for destruction */
	void DisableAndDestroy();

	/** update velocity on client */
	virtual void PostNetReceiveVelocity(const FVector& NewVelocity) override;

	
};

I’m still stuck on this :confused:

Here is the class where i use the Cast :



#include "Arena.h"


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


}


void AMagic::Cast()
{

}



bool AMagic::ServerCast_Validate(FVector Origin, FVector_NetQuantizeNormal ShootDir)
{
	return true;
}

void AMagic::ServerCast_Implementation(FVector Origin, FVector_NetQuantizeNormal ShootDir)
{
	FTransform SpawnTM(ShootDir.Rotation(), Origin);
	AProjectile* Projectile = Cast<AProjectile>(UGameplayStatics::BeginSpawningActorFromClass(this, MagicConfig.ProjectileClass, SpawnTM));
	/*if (Projectile)
	{
		Projectile->Instigator = Instigator;
		Projectile->SetOwner(this);
		Projectile->InitVelocity(ShootDir);

		UGameplayStatics::FinishSpawningActor(Projectile, SpawnTM);
	}*/
}


Are you trying to spawn an instance of AProjectile, or a subclass derived from it? AProjectile is marked as abstract in the UCLASS properties.

As it is now it would be an Instance of AProjectile. I tried to remove the Abstract but i have the same error.

I simply tried to follow the way it was done in the shooter example :

AShooterProjectile.h :



UCLASS(Abstract, Blueprintable)
class AShooterProjectile : public AActor


and then within ShooterWeapon_Projectile.ccp :



void AShooterWeapon_Projectile::ServerFireProjectile_Implementation(FVector Origin, FVector_NetQuantizeNormal ShootDir)
{
	FTransform SpawnTM(ShootDir.Rotation(), Origin);
	AShooterProjectile* Projectile = Cast<AShooterProjectile>(UGameplayStatics::BeginSpawningActorFromClass(this, ProjectileConfig.ProjectileClass, SpawnTM));
	if (Projectile)
	{
		Projectile->Instigator = Instigator;
		Projectile->SetOwner(this);
		Projectile->InitVelocity(ShootDir);

		UGameplayStatics::FinishSpawningActor(Projectile, SpawnTM);
	}
}


Where you use the cast (inside the class AMagic) You have also defined void AMagic::Cast(). This is probably where the error is coming from, removing or renaming that method should allow it to compile.

Yes! seems like that was it. I didnt even pay attention of that, i guess i will just change the name. Thanks !