2
\$\begingroup\$

I created SM_Door in my Editor with a custom Actor class named "Door" to fire some events like open and close.

The problem is, when I open the UE4 Editor, it loads nearly to 75% loading and then crashes, and I really don't know why!

enter image description here

The code of the Door Header file is:

#pragma once
 #include "CoreMinimal.h"
 #include "Components/ActorComponent.h"
 #include "GameFramework/Actor.h"
 #include "Door.generated.h"

 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
 class MAZE_API UDoor : public UActorComponent
 {
     GENERATED_BODY()

 public:    
     // Sets default values for this component's properties
     UDoor();

     void Open();
     void Close();
     bool IsOverlapping(AActor*);

 protected:
     // Called when the game starts
     virtual void BeginPlay() override;

 public:    
     // Called every frame
     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

 };

Then I created Runner class representing the default player:

enter image description here

The full code of the Runner class is:

#pragma once

 #include "CoreMinimal.h"
 #include "Components/ActorComponent.h"
 #include "Engine/World.h"
 #include "Door.h"
 #include "GameFramework/Actor.h"
 #include "Runner.generated.h"


 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
 class MAZE_API URunner : public UActorComponent
 {
     GENERATED_BODY()

 public:    
     // Sets default values for this component's properties
     URunner(); 


 protected:
     // Called when the game starts
     virtual void BeginPlay() override;

 public:    
     // Called every frame
     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

     void IsNearToDoor();
     void IsNearToLampKey();

 private:
     void SetupComponents();

     UDoor Door;
     AActor* Runner = nullptr;
 };
\$\endgroup\$
  • \$\begingroup\$ Do you mean "custom"? "Costume" means something different. \$\endgroup\$ – Engineer Mar 16 at 7:46
  • \$\begingroup\$ ohhhh, that's right , sorry :) \$\endgroup\$ – Mohammed K. Abushawish Mar 16 at 19:21
0
\$\begingroup\$

The problem could be with this line in Runner.h:

UDoor Door;

In this case, the constructor, UDoor(), from Door.h is being called to initialize the default value of the private variable, Door in Runner.h. However, Runner.h may not have access to all of the includes and variables required by the UDoor() constructor. I can't be sure if this is the case because you haven't posted the constructor code from your Runner.cpp file.

Try making the variable Door in Runner.h a pointer instead, like this:

UDoor* Door;

To further clean up the code, you might use a sort of forward declaration by using the class keyword like this:

class UDoor* Door;

If you use forward declaration like this, you could remove #include Door.h from your Runner.h file and add it to your Runner.cpp file instead.

\$\endgroup\$
  • \$\begingroup\$ Thanks @DerpKat for your response.. That's right, i noticed that the problem is in UDoor Door; I will try using UDoor as pointer and tell you the results .. keeping in touch :) \$\endgroup\$ – Mohammed K. Abushawish Mar 16 at 19:27
  • \$\begingroup\$ But how i can instantiate the object using pointer (UDoor* Door) ? \$\endgroup\$ – Mohammed K. Abushawish Mar 16 at 19:34
  • \$\begingroup\$ Try this in your constructor in the Runnner.cpp file: Door = CreateDefaultSubobject<UDoor>(TEXT("NameYourDoorHere")); \$\endgroup\$ – DerpKat Mar 16 at 19:40
  • \$\begingroup\$ Thanks a lot DerpKat :) Nice code brother \$\endgroup\$ – Mohammed K. Abushawish Mar 16 at 20:39
  • \$\begingroup\$ I want to ask you another question because i'm new c++ coder :) Can i use NewObject rather than CreateDefaultSubobject? \$\endgroup\$ – Mohammed K. Abushawish Mar 16 at 20:41

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.