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"

Setting dynamic material on constructor

Hi all, I'm a UE newbie so I'm still in the phase where 90% of my time working with Unreal is spent on online tutorial. That's my problem: when I recompile the code, I had to replace every instance of my class already present in the scene with a new instance, or the game will crash while starting playing (furthermore on recompiling i can see the material applied to the mesh disappear in the editor). I have done some trials and as far as I have understood, this is caused by instantiating the Dynamic material in the constructor. Now I have two question: why this happens? And what's should be initializated in the constructor and what in other method (like onConstruction(), beginPlay(), etc.)? I have searched a lot, but I have found nothing to clarify this doubts. Below there is the code of my constructor. // Sets default values MyClass::MyClass() { // 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;

     // Collision component.
     USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
     SphereComponent->InitSphereRadius(40);
     RootComponent = SphereComponent;
 
     // Mesh component.
     SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Visual Representation"));
     SphereVisual->SetupAttachment(RootComponent);
     static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
     if (SphereVisualAsset.Succeeded()) {
         SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
         SphereVisual->SetWorldScale3D(FVector(0.8f));
     }
 
     // Instance Material.
     static ConstructorHelpers::FObjectFinder<UMaterial> FoundMaterial(TEXT("/Game/Material/DummyMaterial.DummyMaterial"));
     if (FoundMaterial.Succeeded()) {
         UE_LOG(EditorLog, Log, TEXT("Material found."));
         DynamicMaterial = UMaterialInstanceDynamic::Create(FoundMaterial.Object, SphereVisual);
 
         SphereVisual->SetMaterial(0, DynamicMaterial);  
     }
 } 
Product Version: UE 4.18
Tags:
more ▼

asked Mar 08 '18 at 05:19 PM in C++ Programming

avatar image

giulioaur
6 1 5

avatar image giulioaur Mar 09 '18 at 11:41 AM

I solve the problem creating a class attribute that store FoundMaterial.Object, and moving the DynamicMaterial construction to the BeginPlay() function, but I have no idea about the reason of this strange behaviour, it seems the in-game object could not be correctly re-inizialized.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
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