2

I'm new in using c++ and UE. I tried some simple programming but the editor crash.

#include "NewActorComponent.h"
#include "Runtime/Engine/Classes/GameFramework/Actor.h"

 UNewActorComponent::UNewActorComponent()
 {

     PrimaryComponentTick.bCanEverTick = true;
     GetOwner()->GetName();
 }

I knew maybe the output is null so it is crashed,but idk how to expect the error without any crash.

lalap
  • 35
  • 1
  • 8
  • You should inspect the value returned by `GetOwner()` and check that it is not null before calling some methods from it. – user7860670 Sep 18 '17 at 10:46
  • idk how can I do this. – lalap Sep 18 '17 at 10:59
  • Then you should get more familiar with C++ by [reading some books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Programming of UE requires considerable level of C++ comprehension. – user7860670 Sep 18 '17 at 11:04

2 Answers2

1

It's possible the actor component is created but not initialized or attached to an object. You should gate these kind of checks behind IF statements, or use the assert/check macros.

Also, you may want to use the BeginPlay() function instead of the constructor. BeginPlay requires the component to be registered and initialized so it should have an owner.

JonS
  • 401
  • 2
  • 5
  • thank you very much. it works in BeginPlay(). but how can I check if the component is initialized or not ? – lalap Sep 18 '17 at 13:53
  • Use "if (GetOwner()!=NULL)" or "if (GetOwner()->IsValidLowLevel())". However if there's ever a chance that the owner could be invalid at BeginPlay, you could put this stuff in Tick and gate its execution with a bool so that it doesn't run every frame. – JonS Sep 18 '17 at 15:10
1

GetName();

To find the owner of the component. Ex: When inserting a component in the chair, a reference to the chair will be returned.

From Unreal Engine API Reference:

UObjectBaseUtility::GetName

Syntax: FString GetName()

Remarks Returns the name of this object (with no path information)

OK, Follow this steps:

1) File -> New Project -> C++ -> Basic Code -> With Starter Content

2) Inside MinimalDefault Map select one chair and pick Add Component Button.

3) Choose New C++ Component

4) Choose Actor Component Class and click em Next Button

5) In Visual Studio inside NewActorComponent.cpp insert code below in BeginPlay() function

UNewActorComponent::UNewActorComponent()

{

 PrimaryComponentTick.bCanEverTick = true;
 FString ObjectName = GetOwner()->GetName();
 UE_LOG(LogTemp, Warning, TEXT("ObjetctName: %s"), *Objectname);  

}

6) Show Log Window in Unreal Engine 4 Log Windows

7) Compile!

8) See Results in Log Window logwindowresults

Below Complete Code. It Works! Enjoy!

#include "NewActorComponent.h"
#include "Runtime/Engine/Classes/GameFramework/Actor.h"

UNewActorComponent::UNewActorComponent()
{
    PrimaryComponentTick.bCanEverTick = true;

}

void UNewActorComponent::BeginPlay()
{
    Super::BeginPlay();
    FString NameOfObject = GetOwner()->GetName();
    UE_LOG(LogTemp, Warning, TEXT("Name is: %s"), *NameOfObject)

}


void UNewActorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);


}
  • but it crashes if the code in UNewActorComponent not BeginPlay. so step 5 will crash but the Complete Code you insert will run normally – lalap Sep 18 '17 at 15:31