Announcement

Collapse
No announcement yet.

Cast problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Cast problem

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

    AProjectile class :
    Code:
    #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 :
    Code:
    AProjectile* Projectile = Cast<AProjectile>(UGameplayStatics::BeginSpawningActorFromClass(this, MagicConfig.ProjectileClass, SpawnTM));

    Error :
    Code:
    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 :/

    #2
    Are you sure you are including the AProjectile.h?

    Edit: Yes you are including it.

    Can you post AProjectile.h?
    Raise the Black Flag!

    - [TUT]: HTTP GET Request in C++
    - [Tool]: Game Site Tracker website for your project
    - [Website]: Website for my game Leviathan

    Comment


      #3
      Here is the AProjectile.h :

      Code:
      #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;
      
      	
      };

      Comment


        #4
        I'm still stuck on this :/

        Here is the class where i use the Cast :

        Code:
        #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);
        	}*/
        }

        Comment


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

          Comment


            #6
            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 :

            Code:
            UCLASS(Abstract, Blueprintable)
            class AShooterProjectile : public AActor


            and then within ShooterWeapon_Projectile.ccp :

            Code:
            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);
            	}
            }

            Comment


              #7
              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.
              Animation and Physics Programmer, UE4, Epic Games

              Comment


                #8
                Originally posted by Exelcior View Post
                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 !

                Comment

                Working...
                X