x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to make a function to change material?

How to make function to change certain actor material ? I have actor class with main code like this:

 ADresserBuilding::ADresserBuilding()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
 
     Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Dresser_building"));
     RootComponent = Mesh;
 
     static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT("StaticMesh'/Game/Models/Dresser.Dresser'")); 
     UStaticMesh* Asset = MeshToUse.Object;
     Mesh->SetStaticMesh(Asset);
 
     SetOpacityMaterial();
 }
 
 void ADresserBuilding::SetNormalMaterial()
 {
     static ConstructorHelpers::FObjectFinder<UMaterial> NormalMaterial(TEXT("Material'/Game/Models/eb_dresser_01.eb_dresser_01'"));
     Mesh->SetMaterial(0, NormalMaterial.Object);
 } 
 
 
 void ADresserBuilding::SetOpacityMaterial()
 {
     static ConstructorHelpers::FObjectFinder<UMaterial> OpacityMaterial(TEXT("Material'/Game/Models/eb_dresser_01.eb_dresser_01'"));
     Mesh->SetMaterial(0, OpacityMaterial.Object);
 }

And next one that tries to call function but it crush editor when I try like this.

 AStrategy_PlayerController::AStrategy_PlayerController()
 {
     bShowMouseCursor = true;
     DefaultMouseCursor = EMouseCursor::Crosshairs;
 }
 
 
 void AStrategy_PlayerController::BeginPlay()
 {
     Super::BeginPlay();
 
     bActivateDresser = false;
     
     HUDPtr = Cast<AStrategy_HUD>(GetHUD());
 
     SpawnedActorsNumber = 0;
 } 
 
 void AStrategy_PlayerController::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
     
     if (bActivateDresser == true)
     {
         SpawnedActors[SpawnedActorsNumber]->SetActorLocation(GetMousePos(), false, 0, ETeleportType::None);
     }
 }
 
 
 void AStrategy_PlayerController::SetupInputComponent()
 {
     Super::SetupInputComponent();
 
     InputComponent->BindAction("LeftMouseClick", IE_Released, this, &AStrategy_PlayerController::SetBuilding);
     InputComponent->BindAction("ActivateDres", IE_Pressed, this, &AStrategy_PlayerController::ActivateDresser);
 } 
 
 void AStrategy_PlayerController::SetBuilding()
 {
     if (bActivateDresser == true)
     {
         SpawnedActors[SpawnedActorsNumber]->SetActorLocation(GetMousePos(), false, 0, ETeleportType::None);
         SpawnedActors[SpawnedActorsNumber]->SetNormalMaterial(); **//this one cause problem**
            SpawnedActorsNumber++;
         bActivateDresser = false;
     }
 }
 
 void AStrategy_PlayerController::ActivateDresser()
 {
     bActivateDresser = true;
     FVector Location(0.0f, 0.0f, 0.0f);
     FRotator Rotation(0.0f, 0.0f, 0.0f);
     ADresserBuilding* NewActor = GetWorld()->SpawnActor<ADresserBuilding>(GetMousePos(), Rotation);
     SpawnedActors.Add(NewActor);
 
 }
 
 FVector AStrategy_PlayerController::GetMousePos()
 {
     FHitResult Hit;
     GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, false, Hit);
 
     return Hit.Location;
 }

Thank you for your response.

Product Version: UE 4.21
Tags:
more ▼

asked Jan 11 '19 at 05:57 PM in C++ Programming

avatar image

Proggi
3 1 2

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Your crash is probably being caused by a null pointer (nullptr) exception, or perhaps by the use of the ConstructorHelpers library outside of a class constructor.

In general, to avoid null pointer exceptions, you want to make sure your pointers are 'valid' before attempting to de-reference them with the -> operator. Even a simple if (MyObjectPointer) would do the trick, though you'd have to handle what to do in case the pointer is actually not pointing to anything.

On the subject of loading project assets during runtime, I recommend you look at this article from the official docs: Referencing Assets. It shows some examples of the correct ways of doing so in C++.

Finally, have you considered setting references to your materials and meshes in the editor, using a blueprint derived from your ADresserBuilding class? In other words, if you make some UPROPERTY variables on your ADresserBuilding class for the materials and meshes you need, you'll be able to view and set these variables from within the editor. This way, you won't need to use hard asset references in C++, and it also allows you (or other artists) to change out these assets without having to recompile the code. Just a suggestion.

more ▼

answered Jan 11 '19 at 08:27 PM

avatar image

the_batch
1.1k 4 5 8

avatar image Proggi Jan 11 '19 at 09:20 PM

Thank you for ur answer. I already found the problem: FObjectFinder should be used inside actor`s constructor because ""FObjectFinders can't be used outside of constructors". I`m sure that pointers okey, but thank you one more time, I`ll add check before use it. I made all UPROPERTY variables to use it with blueprint. However I`m studying c++ and wanna see how it works if to make project by code only where it`s possible to create plugin in future.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question