Download

Unable to change texture by using settextureparametervalue

Hi,
I am trying to apply a texture dynamically by creating a dynamic material instance and then applying supplied texture via settextureparametervalue on a customized Material.
Following is the code that I am using.



void AFloor::ApplyThisToTheCurrentSelection(UTexture2D * _texture)
{
	for (int32 i = 0; i < CurrentSelections.Num(); i++)
	{
		UMaterialInstanceDynamic* materialInstance;
		materialInterface = CurrentSelections*->GetMaterial(0);
		if (CurrentSelections*->GetMaterial(0)->GetFName().ToString().Contains("MaterialInstanceDynamic")){
			materialInstance = Cast<UMaterialInstanceDynamic>(materialInterface->GetMaterial());
			//UE_LOG(YourLog, Warning, TEXT("Material casted "));			
		}
		else
		{			
			materialInstance = UMaterialInstanceDynamic::Create(materialInterface->GetMaterial(), this);
			//UE_LOG(YourLog, Warning, TEXT("Material created "));
		}		
		try
		{
			materialInstance->SetTextureParameterValue(FName("MainTexture"), _texture); //I am getting error because of this particular line
		}
		catch (const std::exception& e)
		{
			UE_LOG(YourLog, Warning, TEXT("Error thrown %s"),e.what());
		}
		//
		CurrentSelections*->SetMaterial(0, materialInstance);
	}
}


The editor crashes and I am getting the following stack, can someone please guide me as to what am I doing wrong. Although when I comment the line because of which I am getting the error, the material does get applied also the texture is loaded & the currentselections TArray is populated.

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Engine!GameThread_FindParameterByName<FTextureParameterValue>() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\materials\materialinstancesupport.h:185]
UE4Editor_Engine!UMaterialInstance::SetTextureParameterValueInternal() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\materials\materialinstance.cpp:2234]
UE4Editor_Engine!UMaterialInstanceDynamic::SetTextureParameterValue() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\materials\materialinstancedynamic.cpp:50]
UE4Editor_FlipSpaces_8095!AFloor::ApplyThisToTheCurrentSelection() [d:\gitlab\flipspaces\flipspaces\source\flipspaces\private\floor.cpp:57]
UE4Editor_FlipSpaces_8095!AFloor::execApplyThisToTheCurrentSelection() [d:\gitlab\flipspaces\flipspaces\source\flipspaces\public\floor.h:16]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:5077]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:641]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2060]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:866]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:765]
UE4Editor_CoreUObject!UObject::execVirtualFunction() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2166]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:866]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:5077]
UE4Editor_CoreUObject!UObject::ProcessEvent() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1245]
UE4Editor_Engine!AActor::ProcessEvent() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\actor.cpp:639]
UE4Editor_Engine!FInputActionHandlerDynamicSignature::Execute() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\classes\components\inputcomponent.h:93]
UE4Editor_Engine!FInputActionUnifiedDelegate::Execute() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\classes\components\inputcomponent.h:189]
UE4Editor_Engine!UPlayerInput::ProcessInputStack() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\userinterface\playerinput.cpp:1216]
UE4Editor_Engine!APlayerController::ProcessPlayerInput() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2313]
UE4Editor_Engine!APlayerController::TickPlayerInput() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:3851]
UE4Editor_Engine!APlayerController::PlayerTick() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:1976]
UE4Editor_Engine!APlayerController::TickActor() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:3929]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\actor.cpp:111]
UE4Editor_Engine!FTickFunctionTask::DoTask() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private icktaskmanager.cpp:262]
UE4Editor_Engine!TGraphTask<FTickFunctionTask>::ExecuteTask() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\core\public\async askgraphinterfaces.h:999]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\core\private\async askgraph.cpp:932]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\core\private\async askgraph.cpp:679]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\core\private\async askgraph.cpp:1776]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private icktaskmanager.cpp:530]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private icktaskmanager.cpp:1432]
UE4Editor_Engine!UWorld::RunTickGroup() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:704]
UE4Editor_Engine!UWorld::Tick() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:1197]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build++ue4+release-4.12+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1349]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build++ue4+release-4.12+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:368]
UE4Editor!FEngineLoop::Tick() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:2775]
UE4Editor!GuardedMain() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\launch\private\launch.cpp:148]
UE4Editor!GuardedMainWrapper() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\build++ue4+release-4.12+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Yeah, that’s definitely not how you do it. Assuming CurrentSelections is an array of UPrimitiveComponents, this code should work:



void AFloor::ApplyThisToTheCurrentSelection(UTexture2D * _texture)
{
	for (int32 i = 0; i < CurrentSelections.Num(); i++)
	{
		if (IsValid(CurrentSelections*))
		{
			UMaterialInstanceDynamic* materialInstance = CurrentSelections*->CreateDynamicMaterialInstance(0);
			materialInstance->SetTextureParameterValue(FName("MainTexture"), _texture);
		}
	}
}