Hi there, I am currently a game dev student working with procedural and runtime meshes for the first time and I am running into a easy issue that I am at a stump to fix. The assignment is to make a interactive procedural generated mesh. My plan is to make a square and have it change from red to green with dynamic material every time the player shoots it. I am currently on step one, trying to make it. I have the code mostly done, but CreateMeshSection() does not work. And every example of this code I have seen online has that or CreateMeshSection_LinearColor(). Any help is appreciated. Here is the code.
Header file
#pragma once
include “CoreMinimal.h”
include “GameFramework/Actor.h”
include “ProceduralMeshComponent.h”
include “Square.generated.h”
UCLASS()
class PAINTBALLMICHAELM_API ASquare : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
ASquare();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
//makes squate float value
UPROPERTY(EditAnywhere)
float SquareSize;
//variable for square mesh
UPROPERTY(EditAnywhere)
UStaticMeshComponent* SquareMesh;
//generates the square
UFUNCTION()
void GenerateSquare();
//variable to set square size
UFUNCTION(BlueprintCallable)
void SetSquareSize(float NewSize);
};
cpp file
void ASquare::GenerateSquare()
{
// Creates a new square mesh
UStaticMesh* NewMesh = NewObject(this);
// Verticies for the square
TArray<FVector> Vertices;
Vertices.Add(FVector(-SquareSize / 2, -SquareSize / 2, 0.0f));
Vertices.Add(FVector(SquareSize / 2, -SquareSize / 2, 0.0f));
Vertices.Add(FVector(SquareSize / 2, SquareSize / 2, 0.0f));
Vertices.Add(FVector(-SquareSize / 2, SquareSize / 2, 0.0f));
// Triangles for the square
TArray<int32> Triangles;
Triangles.Add(0);
Triangles.Add(1);
Triangles.Add(2);
Triangles.Add(0);
Triangles.Add(2);
Triangles.Add(3);
// Combines the vertices and triangles for the mesh use
NewMesh->CreateMeshSection(0, Vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), true);
// Sets new mesh for square
SquareMesh->SetStaticMesh(NewMesh);
}
//sets square size
void ASquare::SetSquareSize(float NewSize)
{
SquareSize = NewSize;
// Generates square with new size
GenerateSquare();
}