Announcement

Collapse
No announcement yet.

Unreal engine keeps crashing when I hit play (no log)

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

    Unreal engine keeps crashing when I hit play (no log)

    I have this code:
    Enemy.cpp
    Code:
    #include "Enemy.h"
    #include "Components/StaticMeshComponent.h"
    #include "Components/PrimitiveComponent.h"
    #include "Components/BoxComponent.h"
    #include "Engine/StaticMesh.h"
    #include "Engine.h"
    
    AEnemy::AEnemy()
    {
        PrimaryActorTick.bCanEverTick = true;
    }
    
    void AEnemy::BeginPlay()
    {
        Super::BeginPlay();
    
        FVector BoxSize = FVector(32.f, 32.f, 32.f);
    
        EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
        EnemyHitBox->SetSimulatePhysics(true);
        EnemyHitBox->SetNotifyRigidBodyCollision(true);
        EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
        EnemyHitBox->SetBoxExtent(BoxSize);
    
        RootComponent = EnemyHitBox;
    
        StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
        StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
        StaticMesh->SetupAttachment(RootComponent);
    }
    void AEnemy::Tick(float DeltaTime)
    {
        Super::Tick(DeltaTime);
    }
    
    void AEnemy::Damage(float Damage)
    {
        Health -= Damage;
    
        if (Health <= 0.f) {
            Destroy();
        }
    }
    void AEnemy::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
    {
        UE_LOG(LogTemp, Error, TEXT("It's a collision!"));
    }
    Enemy.h
    Code:
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "Enemy.generated.h"
    
    UCLASS()
    class THECHOSENONE_API AEnemy : public AActor
    {
        GENERATED_BODY()
    
    public:    
        AEnemy();
    
    protected:
        void BeginPlay() override;
    
    public:
        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        UBoxComponent* EnemyHitBox;
    
        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        UStaticMeshComponent* StaticMesh;
    
        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float Health;
    
        void Tick(float DeltaTime) override;
    
        UFUNCTION(BlueprintCallable)
        void Damage(float Damage);
    
        UFUNCTION()
        void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);
    
    };
    When I compile it, it works. When I play the game the engine just crashes. This is the wrong code, because it is the only code I have written.

    #2
    Issue is you are doing stuff in the wrong place.

    Code:
         FVector BoxSize = FVector(32.f, 32.f, 32.f);      
    EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));    
    EnemyHitBox->SetSimulatePhysics(true);    
    EnemyHitBox->SetNotifyRigidBodyCollision(true);    
    EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);    
    EnemyHitBox->SetBoxExtent(BoxSize);      
    RootComponent = EnemyHitBox;      
    
    StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));    
    StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));    
    StaticMesh->SetupAttachment(RootComponent);
    This should be in your constructor
    Code:
     AEnemy::AEnemy()
    not in BeginPlay.

    Also i would advise against binding a delegate in constructor,

    this line
    Code:
         EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
    should be in BeginPlay

    so your code should look like this

    Code:
    #include "Enemy.h"
    #include "Components/StaticMeshComponent.h"
    #include "Components/PrimitiveComponent.h"
    #include "Components/BoxComponent.h"
    #include "Engine/StaticMesh.h"
    
    AEnemy::AEnemy()
    {
        PrimaryActorTick.bCanEverTick = true;
    
        EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
        EnemyHitBox->SetSimulatePhysics(true);
        EnemyHitBox->SetNotifyRigidBodyCollision(true);
        EnemyHitBox->SetBoxExtent(FVector(32.f, 32.f, 32.f));
    
        RootComponent = EnemyHitBox;
    
        StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
        StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
        StaticMesh->SetupAttachment(RootComponent);
    }
    
    void AEnemy::BeginPlay()
    {
        Super::BeginPlay();
    
        EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
    
    }
    void AEnemy::Tick(float DeltaTime)
    {
        Super::Tick(DeltaTime);
    }
    
    void AEnemy::Damage(float Damage)
    {
        Health -= Damage;
    
        if (Health <= 0.f) {
            Destroy();
        }
    }
    void AEnemy::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
    {
        UE_LOG(LogTemp, Error, TEXT("It's a collision!"));
    }
    Last edited by TheKaosSpectrum; 12-09-2019, 11:14 PM.
    Game Programmer - Working for a Indie Studio.

    Visit my blog: TheGamesDev

    InterKaos Games

    PC Specs:

    Comment


      #3
      Yes thank you. But I ve got new problem. Can you help me to fix this too please?
      Click image for larger version

Name:	DSLG6KQ8TEW56p2LLDCxIQ.png
Views:	113
Size:	236.1 KB
ID:	1696558

      Comment


        #4
        This can be a number of things, either your collision profile is not set to physics actor, or you need to open the mesh and assure use complex as simple is set. These are the most common issues.
        Game Programmer - Working for a Indie Studio.

        Visit my blog: TheGamesDev

        InterKaos Games

        PC Specs:

        Comment


          #5
          Thank you, but I cant fix it.
          Even this line of code:
          Code:
           StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
          Is not working..
          The cube is like 5644845664865456 bigger than the Hitbox which is
          Code:
           FVector(32.f, 32.f, 32.f)
          And it still calls this error from previous message..
          I will switch to unity, where I can get some help. Because here I will get nowhere..
          You see, I dont see any error in my code.. I am setting the size to be smaller than the box, and the cube is still almost 64x bigger than the small hitbox. Even when I resize it, it just deletes itself from the scene and becomes invisible..
          Attached Files
          Last edited by jimmo22; 12-11-2019, 02:51 PM.

          Comment


            #6

            Try: StaticMesh->SetWorldScale3D(FVector(0.9f, 0.9f, 0.9f)); It is scaling relative to the hitbox when you attach it hence it is so large. Or you can Attach and pass the flag to ignore scale.
            SolidGasGames

            Comment


              #7
              the size remains same nomatter what for numbers I type there.

              Comment


                #8
                the StaticMesh->SetWorldScale3D is not working at all. even with that flag.. it remains the same in size.

                Comment


                  #9
                  Originally posted by TheKaosSpectrum View Post
                  Issue is you are doing stuff in the wrong place.

                  Code:
                  FVector BoxSize = FVector(32.f, 32.f, 32.f);
                  EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
                  EnemyHitBox->SetSimulatePhysics(true);
                  EnemyHitBox->SetNotifyRigidBodyCollision(true);
                  EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
                  EnemyHitBox->SetBoxExtent(BoxSize);
                  RootComponent = EnemyHitBox;
                  
                  StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
                  StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
                  StaticMesh->SetupAttachment(RootComponent);
                  This should be in your constructor
                  Code:
                  AEnemy::AEnemy()
                  not in BeginPlay.

                  Also i would advise against binding a delegate in constructor,

                  this line
                  Code:
                  EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
                  should be in BeginPlay

                  so your code should look like this

                  Code:
                  #include "Enemy.h"
                  #include "Components/StaticMeshComponent.h"
                  #include "Components/PrimitiveComponent.h"
                  #include "Components/BoxComponent.h"
                  #include "Engine/StaticMesh.h"
                  
                  AEnemy::AEnemy()
                  {
                  PrimaryActorTick.bCanEverTick = true;
                  
                  EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
                  EnemyHitBox->SetSimulatePhysics(true);
                  EnemyHitBox->SetNotifyRigidBodyCollision(true);
                  EnemyHitBox->SetBoxExtent(FVector(32.f, 32.f, 32.f));
                  
                  RootComponent = EnemyHitBox;
                  
                  StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
                  StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
                  StaticMesh->SetupAttachment(RootComponent);
                  }
                  
                  void AEnemy::BeginPlay()
                  {
                  Super::BeginPlay();
                  
                  EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
                  
                  }
                  void AEnemy::Tick(float DeltaTime)
                  {
                  Super::Tick(DeltaTime);
                  }
                  
                  void AEnemy::Damage(float Damage)
                  {
                  Health -= Damage;
                  
                  if (Health <= 0.f) {
                  Destroy();
                  }
                  }
                  void AEnemy::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
                  {
                  UE_LOG(LogTemp, Error, TEXT("It's a collision!"));
                  }
                  Good day!
                  Why is it better to addDynamic in BeginPlay not in constructor? because the object could not exist? Detailed explanation is much appreciated!

                  Because when i changed it from constructor to BeginPlay I've encountered this error::

                   
                  Spoiler


                  Thank you!
                  Last edited by EminentEIement; 02-15-2020, 06:29 AM.

                  Comment

                  Working...
                  X