Download

create and attach a UPhysicsHandleComponent to an actor at runtime??

hi guys can anyone point me in the right direction of how i can do what the title suggests and create and attach a UPhysicsHandleComponent to an actor at runtime.
basically the c++ version of this blueprint node 2cbdb137c1304c238247f2195090d21d.png

so lets say i have the default pawn. at runtime i just want to make sure it has a physicshandlecomponent and if it doesnt then i want to make the physics handle component and attach it to the pawn.

this is what i have so far



           // look for attached physics handle
	       physicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	       
           if (physicsHandle)
	{
           // TODO physics handle found . do some stuff here
	}
	else
	{
	   //add temporary log to inform that they have forgotten to attach a physicsHandleComponent
	   UE_LOG(LogTemp, Warning, TEXT("physics handle component not found, please attach a physics handle component and try again!!"))

           // TODO create a physicsHandleComponent and attach it to the pawn
	}

thanks

You could call the static function that is the blueprint library function definition. Or you could search the source code for that function on github. If you look at the API docs, it tells you the header and source file for the function.

Sorry can you explain what you mean by “call the static function that is the blueprint library function definition” where would I find this and how would I call it etc?

Also I was trying to find something in the documents but all I could find was that blueprint node I linked above or this http://docs.unrealengine.com/latest/INT/API/Runtime/Engine/PhysicsEngine/UPhysicsHandleComponent/

Which gives you all the functions and uses that the physics handle actually does but nothing about how to actually create or attach it .

I googled how to create components at runtime and tried all the things I could find but most of them was old posts or the code examples didn’t compile as I think the code is deprecated now

Ok so after some more digging I think the old method used to be ConstructObject() and now it has been replaced with newObject()
https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/Creation/index.html
Is this right and also I think I uunderstand most of the function parameters it is asking
For except the " getTransientPackage()" bit

Is this the right function I should be using and if so can anyone show me how I would
Construct it for a UPhysicsHandleComponent.

Thanks

At a bare minimum you should need to do this. It is a function I use in a plugin demo of mine.


static void AddDynamicComponent(AActor* NewActor, TSubclassOf<UActorComponent> ComponentClass)
{
                        UClass* ComponentClass = *ComponentClass;

			// Construct the new component and attach as needed
			UActorComponent* NewInstanceComponent = NewObject<UActorComponent>(NewActor, ComponentClass, TEXT("DynamicallyAddedComponentName"));
                        if(NewInstanceComponent)
                        {
			    // Add to SerializedComponents array so it gets saved
			    NewActor->AddInstanceComponent(NewInstanceComponent);
			    NewInstanceComponent->OnComponentCreated();
			    NewInstanceComponent->RegisterComponent();
                        }
}

ok thanks for the help . so i have managed to get the component created now but im getting an error.
“Native components are editable when declared as a Uproperty in c++”

and i am unable to edit any of the properties

a9f3b87d999b41f6a226fb5b4f1282259b9bed3e.png

any idea how to fix this thanks??

Okay, so you didn’t mean Runtime, like on the game startup. You meant runtime in the Editor. There are just a few more function calls you need to make for this to work. Here is the revamped code:


static void AddDynamicComponent(AActor* NewActor, TSubclassOf<UActorComponent> ComponentClass)
{
                        //Add this line below
                        NewActor->Modify(); //Notify that this actor is about to be edited by any interested parties.
                        UClass* ComponentClass = *ComponentClass;

			// Construct the new component and attach as needed
			UActorComponent* NewInstanceComponent = NewObject<UActorComponent>(NewActor, ComponentClass, TEXT("DynamicallyAddedComponentName"));
                        if(NewInstanceComponent)
                        {
			    // Add to SerializedComponents array so it gets saved
			    NewActor->AddInstanceComponent(NewInstanceComponent);
			    NewInstanceComponent->OnComponentCreated();
			    NewInstanceComponent->RegisterComponent();

                            //And add this
                            NewActor->RerunConstructionScripts(); //Have the editor rebuild the object
                        }
}

not sure what you mean that screenshot i posted is when i press play and then i eject from the pawn. click on the pawn and that is how it is in the editor details panel??

this is what i have in my .cpp file


createdPhysicsHandleComponent = NewObject<UPhysicsHandleComponent>(this, TEXT("createdPhysicsHandle"));

		if (createdPhysicsHandleComponent)
		{
			UE_LOG(LogTemp, Warning, TEXT("physics handle component has been succesfully created!!"))
				createdPhysicsHandleComponent->OnComponentCreated();
				createdPhysicsHandleComponent->RegisterComponent();
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT(" %s is missing physics handle component, please attach a physics handle component and try again!!"), *GetOwner()->GetName())
		}
	}

i can manually set the properties in code ie if i use


createdPhysicsHandleComponent->SetLinearDamping(300.f);

when i eject from the pawn the linear dampening has changed from the default 200.f to 300.f

just doesnt seem right that it is not possible to edit it from the actual editor.
guess will just have to hard-code it for now.

SOLUTION FOUND

ok for some reason it sort of works now but you have to go up to the parent class and then you can see the inherited components variables and can edit them

solution 1.png

anyway thanks for all you’re help

Hey, saw that you posted this here. I think for the sake of sanity I can keep helping you via PM and Github. But this actually shows me exactly what you are going for, for the most part. Always glad to help as well.