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"

Editor crashes when creating UMaterialInstanceDynamic

The snippet of code below comes mainly from the procedural mesh generation wiki article, but I've attempted to assign a very simple default material to it, which instantly crashes the editor (when I recompile, and when I load the editor). No error message or information is presented.

The exact line it crashes at is 18, when the new instance is created.

The code I've been working with (entire class, based on default actor template):

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "MeshTestGame.h"
 #include "GeneratedCubeActor.h"
 #include "ProceduralMeshComponent.h"
 
 
 // Sets default values
 AGeneratedCubeActor::AGeneratedCubeActor()
 {
      // 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;
 
     // Find mat
     static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/wood_mat.wood_mat'"));
     UE_LOG(LogTemp, Warning, TEXT("Material pointer: 0x%X"), Material.Object); // Prints 0x43507580
 
     auto matinstance = UMaterialInstanceDynamic::Create(Material.Object, this); // At this point the Editor crashes without any message or info.
 
     USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
     RootComponent = SphereComponent;
 
     UProceduralMeshComponent* mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
 
     TArray<FVector> vertices;
     vertices.Add(FVector(0, 0, 0));
     vertices.Add(FVector(0, 100, 0));
     vertices.Add(FVector(0, 100, 100));
     vertices.Add(FVector(0, 0, 100));
 
     TArray<int32> Triangles;
     Triangles.Add(0);
     Triangles.Add(1);
     Triangles.Add(2);
 
     Triangles.Add(0);
     Triangles.Add(2);
     Triangles.Add(3);
 
     TArray<FVector> normals;
     normals.Add(FVector(1, 0, 0));
     normals.Add(FVector(1, 0, 0));
     normals.Add(FVector(1, 0, 0));
     normals.Add(FVector(1, 0, 0));
 
     TArray<FVector2D> UV0;
     UV0.Add(FVector2D(0, 0));
     UV0.Add(FVector2D(0, 1));
     UV0.Add(FVector2D(1, 1));
     UV0.Add(FVector2D(1, 0));
 
     TArray<FColor> vertexColors;
     vertexColors.Add(FColor(100, 100, 100, 100));
     vertexColors.Add(FColor(100, 100, 100, 100));
     vertexColors.Add(FColor(100, 100, 100, 100));
     vertexColors.Add(FColor(100, 100, 100, 100));
 
     TArray<FProcMeshTangent> tangents;
     tangents.Add(FProcMeshTangent(1, 1, 1));
     tangents.Add(FProcMeshTangent(1, 1, 1));
     tangents.Add(FProcMeshTangent(1, 1, 1));
     tangents.Add(FProcMeshTangent(1, 1, 1));
 
 
     mesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
     mesh->CreateAndSetMaterialInstanceDynamicFromMaterial(0, matinstance);
 
     mesh->AttachTo(RootComponent);
 }
 
 // Called when the game starts or when spawned
 void AGeneratedCubeActor::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void AGeneratedCubeActor::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 }
 
Product Version: UE 4.11
Tags:
more ▼

asked Apr 01 '16 at 03:32 PM in C++ Programming

avatar image

Bart Pelle
46 1 4 8

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

2 answers: sort voted first

I managed to fix the issue by changing line 18 to auto matinstance = UMaterialInstanceDynamic::Create(Material.Object, Material.Object);, but it still doesn't render the material sadly.

more ▼

answered Apr 01 '16 at 04:28 PM

avatar image

Bart Pelle
46 1 4 8

avatar image Bart Pelle Apr 01 '16 at 05:10 PM

Nevermind, changing the material index from 0 to 1 fixed it.. :)

avatar image Matthew J Apr 01 '16 at 07:22 PM

Hello Bart,

I'm glad to hear that you were able to solve the issue. If you run into any more problems involving this, please let me know.

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

You can also use (Material.Object, this) as you did in the beginning, if you call that outside of the constructor. the constructor you define in C++ pretty much runs for the CDO (class default object), while other instances just use copy-constructor and get the CDO as feed for copy.

It pretty much means that you need to create dynamic material instances outside of constructor, for example in PostInitProperties(), OnConstruction(), BeginPlay()... All of these run for each instance. I really advice to check out this link for better explanation: https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Actors/ActorLifecycle

more ▼

answered Mar 19 '18 at 05:27 PM

avatar image

Arty-McLabin
915 20 43 63

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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