Code for procedual mesh actor does not work, help!

Hello :slight_smile:

I have some issues to finish the following code. With this I’d like to be able to create meshes from blueprint.



#pragma once

#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "CustomMeshBridge.generated.h"


/**
 * 
 */
UCLASS()
class SSS2_API ACustomMeshBridge : public AActor
{
	GENERATED_UCLASS_BODY()
public:
	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "New Vertex", Keywords = "New Vertex"), Category = ProceduralMesh)
		static FProceduralMeshVertex CreateVertex(FVector position, FColor col, float u, float v);

	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "New Triangle", Keywords = "New Triangle"), Category = ProceduralMesh)
		static FProceduralMeshTriangle CreateTriangle(FProceduralMeshVertex vertex0, FProceduralMeshVertex vertex1, FProceduralMeshVertex vertex2);

	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Create Mesh Class Instance", Keywords = "Create Mesh Class Instance"), Category = ProceduralMesh)
		static ACustomMeshBridge* CreateMesh(TArray<FProceduralMeshTriangle>& OutTriangles, UMaterial* Material);

	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Create Triangle Array", Keywords = "Create Triangle Array"), Category = ProceduralMesh)
		static TArray<FProceduralMeshTriangle> CreateTriangleArray();

	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Add Triangle To Triangle Array", Keywords = "Add Triangle To Triangle Array"), Category = ProceduralMesh)
		static TArray<FProceduralMeshTriangle> AddTriangleToTriangleArray(FProceduralMeshTriangle triangle,TArray<FProceduralMeshTriangle>& OutTriangles);

private:
	TArray<FProceduralMeshTriangle> triangles;
	ACustomMeshBridge(TArray<FProceduralMeshTriangle>&, UMaterial*);
};


and



#include "SSS2.h"
#include "CustomMeshBridge.h"
#include "ConstructorHelpers.h"


ACustomMeshBridge::ACustomMeshBridge(const class FPostConstructInitializeProperties& PCIP, TArray<FProceduralMeshTriangle>& OutTriangles, UMaterial* Material)
	: Super(PCIP)
{
	TSubobjectPtr<UProceduralMeshComponent> mesh;
	mesh = PCIP.CreateDefaultSubobject<UProceduralMeshComponent>(this, TEXT("BPGeneratedMesh"));
	// TODO Apply a real material with textures, using UVs
	 //static ConstructorHelpers::FObjectFinder<UMaterialInterface> Material(TEXT("Material'/Game/Materials/M_Concrete_Poured.M_Concrete_Poured'"));
	mesh->SetMaterial(0, &static_cast<UMaterialInterface>(*Material));
	// Generate a single triangle
	mesh->SetProceduralMeshTriangles(triangles);
	this->RootComponent = mesh;
}


FProceduralMeshVertex ACustomMeshBridge::CreateVertex(FVector position, FColor col, float u, float v)
{
	FProceduralMeshVertex vert;
	vert.Position = position;
	vert.Color = col;
	vert.U = u;
	vert.V = v;
	return vert;
}

FProceduralMeshTriangle ACustomMeshBridge::CreateTriangle(FProceduralMeshVertex vertex0, FProceduralMeshVertex vertex1, FProceduralMeshVertex vertex2)
{
	FProceduralMeshTriangle tri;
	tri.Vertex0 = vertex0;
	tri.Vertex1 = vertex1;
	tri.Vertex2 = vertex2;
	return tri;
}

TArray<FProceduralMeshTriangle> ACustomMeshBridge::CreateTriangleArray()
{
	TArray<FProceduralMeshTriangle> a;
	return a;
}

TArray<FProceduralMeshTriangle> ACustomMeshBridge::AddTriangleToTriangleArray(FProceduralMeshTriangle triangle, TArray<FProceduralMeshTriangle>& OutTriangles)
{
	OutTriangles.Add(triangle);
	return OutTriangles;
}

ACustomMeshBridge* ACustomMeshBridge::CreateMesh(TArray<FProceduralMeshTriangle>& OutTriangles, UMaterial* Material)
{
	ACustomMeshBridge b = ACustomMeshBridge(OutTriangles, Material);
	return &b;
}


I turn arround for hours. I am not verry experimented in c++ and i cannot work with pointers.
This project keeps throwing errors at me. Please help me to fix those. the custom mesh class is the one found in https://github.com/SRombauts/UE4ProceduralMesh and the class here is inspired by the triangle class.

Hope you can fix it :wink:

Wicpar

It would help knowing what errors you are getting.

here are the errors:


Error	14	error : Failed to produce item: C:\Users\\Documents\Unreal Projects\SSS2\Binaries\Win64\UE4Editor-SSS2-Win64-DebugGame.pdb	C:\Users\\Documents\Unreal Projects\SSS2\Intermediate\ProjectFiles\ERROR	SSS2
Error	7	error C2511: 'ACustomMeshBridge::ACustomMeshBridge(const FPostConstructInitializeProperties &,TArray<FProceduralMeshTriangle,FDefaultAllocator> &,UMaterial *)' : overloaded member function not found in 'ACustomMeshBridge'	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	9	1	SSS2
Error	8	error C2550: 'ACustomMeshBridge::{ctor}' : constructor initializer lists are only allowed on constructor definitions	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	10	1	SSS2
Error	12	error C2597: illegal reference to non-static member 'AActor::RootComponent'	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	18	1	SSS2
Error	11	error C2597: illegal reference to non-static member 'ACustomMeshBridge::triangles'	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	17	1	SSS2
Error	9	error C2671: 'ACustomMeshBridge::{ctor}' : static member functions do not have 'this' pointers	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	12	1	SSS2
Error	13	error C4172: returning address of local variable or temporary	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	56	1	SSS2
Error	10	error C4238: nonstandard extension used : class rvalue used as lvalue	C:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	15	1	SSS2
Error	15	error MSB3073: The command ""C:\Program Files\Unreal Engine\4.4\Engine\Build\BatchFiles\Build.bat" SSS2Editor Win64 DebugGame "C:\Users\\Documents\Unreal Projects\SSS2\SSS2.uproject" -rocket" exited with code -1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	38	5	SSS2
	24	IntelliSense: declaration is incompatible with "ACustomMeshBridge *ACustomMeshBridge::CreateMesh(TArray<FProceduralMeshTriangle, FDefaultAllocator> &OutTriangles, UMaterial *Material)" (declared at line 25 of "c:\users\\documents\unreal projects\sss2\source\sss2\CustomMeshBridge.h")	c:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	53	39	SSS2
	23	IntelliSense: expected a '{'	c:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	9	2	SSS2
	20	IntelliSense: identifier "FMeshBatchElement" is undefined	c:\Program Files\Unreal Engine\4.4\Engine\Source\Runtime\ShaderCore\Public\VertexFactory.h	488	131	SSS2
	17	IntelliSense: identifier "FOnSelectedLevelsChangedEvent" is undefined	c:\Program Files\Unreal Engine\4.4\Engine\Source\Runtime\Engine\Classes\Engine\World.h	1929	2	SSS2
	16	IntelliSense: identifier "FWindSourceSceneProxy" is undefined	c:\Program Files\Unreal Engine\4.4\Engine\Source\Runtime\Engine\Classes\Components\WindDirectionalSourceComponent.h	20	2	SSS2
	18	IntelliSense: no instance of constructor "FReadSurfaceDataFlags::FReadSurfaceDataFlags" matches the argument list	c:\Program Files\Unreal Engine\4.4\Engine\Source\Runtime\Engine\Public\UnrealClient.h	55	92	SSS2
	19	IntelliSense: no instance of constructor "FReadSurfaceDataFlags::FReadSurfaceDataFlags" matches the argument list	c:\Program Files\Unreal Engine\4.4\Engine\Source\Runtime\Engine\Public\UnrealClient.h	63	87	SSS2
	21	IntelliSense: no instance of overloaded function "ACustomMeshBridge::ACustomMeshBridge" matches the specified type	c:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	8	20	SSS2
	22	IntelliSense: variable "UMaterial" is not a type name	c:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	8	139	SSS2
	25	IntelliSense: variable "UMaterial" is not a type name	c:\Users\\Documents\Unreal Projects\SSS2\Source\SSS2\CustomMeshBridge.cpp	53	97	SSS2
Warning	2	The element 'ItemGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'NMakeNativeOutput' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Item, Reference, COMReference, COMFileReference, Xdcmake, Bscmake, ClCompile, ClInclude, Midl, ResourceCompile, PreLinkEvent, CustomBuildStep, Manifest, ProjectConfiguration, ProjectCapability, NativeReference, ProjectReference, Compile, EmbeddedResource, Content, Page, Resource, ApplicationDefinition, None, BaseApplicationManifest, Folder, Import, Service, WebReferences, WebReferenceUrl, FileAssociation, BootstrapperFile, PublishFile, PRIResource, AppxSystemBinary, AppxReservedFileName, AppxManifestFileNameQuery, AppxManifest, StoreAssociationFile, CodeAnalysisDependentAssemblyPaths, CodeAnalysisDictionary, CodeAnalysisImport, Link, ResourceCompile, PreBuildEvent, PostBuildEvent' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	61	8	Miscellaneous Files
Warning	3	The element 'ItemGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'NMakeNativeOutput' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Item, Reference, COMReference, COMFileReference, Xdcmake, Bscmake, ClCompile, ClInclude, Midl, ResourceCompile, PreLinkEvent, CustomBuildStep, Manifest, ProjectConfiguration, ProjectCapability, NativeReference, ProjectReference, Compile, EmbeddedResource, Content, Page, Resource, ApplicationDefinition, None, BaseApplicationManifest, Folder, Import, Service, WebReferences, WebReferenceUrl, FileAssociation, BootstrapperFile, PublishFile, PRIResource, AppxSystemBinary, AppxReservedFileName, AppxManifestFileNameQuery, AppxManifest, StoreAssociationFile, CodeAnalysisDependentAssemblyPaths, CodeAnalysisDictionary, CodeAnalysisImport, Link, ResourceCompile, PreBuildEvent, PostBuildEvent' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	64	8	Miscellaneous Files
Warning	5	The element 'ItemGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'PropertyPageSchema' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Item, Reference, COMReference, COMFileReference, Xdcmake, Bscmake, ClCompile, ClInclude, Midl, ResourceCompile, PreLinkEvent, CustomBuildStep, Manifest, ProjectConfiguration, ProjectCapability, NativeReference, ProjectReference, Compile, EmbeddedResource, Content, Page, Resource, ApplicationDefinition, None, BaseApplicationManifest, Folder, Import, Service, WebReferences, WebReferenceUrl, FileAssociation, BootstrapperFile, PublishFile, PRIResource, AppxSystemBinary, AppxReservedFileName, AppxManifestFileNameQuery, AppxManifest, StoreAssociationFile, CodeAnalysisDependentAssemblyPaths, CodeAnalysisDictionary, CodeAnalysisImport, Link, ResourceCompile, PreBuildEvent, PostBuildEvent' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	98	6	Miscellaneous Files
Warning	4	The element 'PropertyGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'NMakeManagedOutput' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Property, AllowUnsafeBlocks, AppConfigForCompiler, ApplicationIcon, ApplicationRevision, ApplicationVersion, AppDesignerFolder, AspNetConfiguration, AssemblyKeyContainerName, AssemblyKeyProviderName, AssemblyName, AssemblyOriginatorKeyFile, AssemblyOriginatorKeyFileType, AssemblyOriginatorKeyMode, AssemblyType, AutoGenerateBindingRedirects, AutorunEnabled, BaseAddress, BootstrapperComponentsLocation, BootstrapperComponentsUrl, BootstrapperEnabled, CharacterSet, CheckForOverflowUnderflow, CLRSupport, UseDebugLibraries, CodePage, Configuration, ConfigurationName, ConfigurationOverrideFile, CreateDesktopShortcut, CreateWebPageOnPublish, CurrentSolutionConfigurationContents, DebugSecurityZoneURL, DebugSymbols, DebugType, DefaultClientScript, DefaultHTMLPageLayout, DefaultTargetSchema, DefineConstants, DefineDebug, DefineTrace, DelaySign, DisableLangXtns, DisallowUrlActivation, CodeAnalysisAdditionalOptions, CodeAnalysisApplyLogFileXsl, CodeAnalysisConsoleXsl, CodeAnalysisCulture, CodeAnalysisFailOnMissingRule....	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	75	8	Miscellaneous Files
Warning	1	The element 'PropertyGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'TargetPath' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Property, AllowUnsafeBlocks, AppConfigForCompiler, ApplicationIcon, ApplicationRevision, ApplicationVersion, AppDesignerFolder, AspNetConfiguration, AssemblyKeyContainerName, AssemblyKeyProviderName, AssemblyName, AssemblyOriginatorKeyFile, AssemblyOriginatorKeyFileType, AssemblyOriginatorKeyMode, AssemblyType, AutoGenerateBindingRedirects, AutorunEnabled, BaseAddress, BootstrapperComponentsLocation, BootstrapperComponentsUrl, BootstrapperEnabled, CharacterSet, CheckForOverflowUnderflow, CLRSupport, UseDebugLibraries, CodePage, Configuration, ConfigurationName, ConfigurationOverrideFile, CreateDesktopShortcut, CreateWebPageOnPublish, CurrentSolutionConfigurationContents, DebugSecurityZoneURL, DebugSymbols, DebugType, DefaultClientScript, DefaultHTMLPageLayout, DefaultTargetSchema, DefineConstants, DefineDebug, DefineTrace, DelaySign, DisableLangXtns, DisallowUrlActivation, CodeAnalysisAdditionalOptions, CodeAnalysisApplyLogFileXsl, CodeAnalysisConsoleXsl, CodeAnalysisCulture, CodeAnalysisFailOnMissingRule....	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	22	7	Miscellaneous Files
Warning	6	warning : The selected culture 'fr_FR' is not available; falling back to 'en'	C:\Users\\Documents\Unreal Projects\SSS2\Intermediate\ProjectFiles\LogTextLocalizationManager	SSS2


The compiler is complaining about the added arguments to the constructor. Prior to UE4.6, you have to use the automatically generated constructor as-is:


ACustomMeshBridge::ACustomMeshBridge(const FPostConstructInitializeProperties &)

Any extra initialization parametres (TArray<FProceduralMeshTriangle,FDefaultAllocator> &,UMaterial *) have to be passed through a separate initialization function.

All your member functions are flagged as “static”. This means they do not require an object instance, but you are trying to access the instance members RootComponent and triangles within. Remove the static keyword from your functions.

In CreateMesh, you are creating a local variable b and then returning a pointer to it (&):


ACustomMeshBridge* ACustomMeshBridge::CreateMesh(TArray<FProceduralMeshTriangle>& OutTriangles, UMaterial* Material)
{
	ACustomMeshBridge b = ACustomMeshBridge(OutTriangles, Material);
	return &b;
}

The local variable will cease to exist when you exit CreateMesh, thus returning a pointer to something that no longer exists.

Seems like you’re dealing with pretty basic C++ errors as well as UE4-specific mistakes. The latter is definitely understandable as UE uses many unusual C++ constructs, but you’ll have a very hard time understanding them unless you have a solid grasp of C++. Maybe it’d be easier for you to start with a blueprint based project?

it may be easier but will not get you any further with c++
maybe start with a more simple c++ task

many thanks, what I intended to do couldn’t be made in bluprints, so I wanted to port the procedual mesh generation to bluprint, and I now managed to do it without any constructors, I will post the code later on the thread in the wiki with an exampl. thanks for your help tho, you helped me understand more about these nasty pointers :slight_smile: