Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sephirot47/9d2c495012f75119c961 to your computer and use it in GitHub Desktop.
Save sephirot47/9d2c495012f75119c961 to your computer and use it in GitHub Desktop.
UE4 C++ Set Object Type or Set Collision Channel Type
/*
Ok, so you want to change your Object Type (or collision channel) to a custom channel in runtime.
But there's a problem, it seems UE4 doesn't support this yet, but there's a workaround hehe :)
For example, imagine you create the ObjectType Building.
Now, imagine you want to change the ObjectType of all the StaticMeshComponents of the object OBJECT (from WorldStatic to Building, for example).
You can try something like this:
*/
void MyActor::ChangeMeshesObjectTypeToBuilding()
{
TArray<UStaticMeshComponent*> meshes; GetComponents<UStaticMeshComponent>(meshes);
for (UStaticMeshComponent* mesh : meshes)
{
mesh->SetCollisionObjectType( ????????? ); //What do I pass here???
}
}
/*
The ???????? is the problem here.
StaticMeshComponent has the funcion SetCollisionObjectType(ECollisionChannel channel). But how do I get my Building channel to pass into this function !?!?!?
For the preset ones you can go ECC_WorldStatic, ECC_WorldDynamic, etc. But for custom ones?
Well, once you have created your custom ObjectType in the editor, UE4 fills one out of the 18 custom ObjectType slots. In C++ you can access the custom slots like this: ECC_GameTraceChannel1, ECC_GameTraceChannel2, ECC_GameTraceChannel3....
To know in which slot your custom ObjectType is, just check the file Config/DefaultEngine.ini, and search the line containing the ObjectType definition, it would look something like this:
-DefaultChannelResponses=(Channel=ECC_GameTraceChannel3,Name="Building",DefaultResponse=ECR_Block,bTraceType=False,bStaticObject=False)
So finally, using this, in this case, my function would be:
*/
void MyActor::ChangeMeshesObjectTypeToBuilding()
{
TArray<UStaticMeshComponent*> meshes; GetComponents<UStaticMeshComponent>(meshes);
for (UStaticMeshComponent* mesh : meshes)
{
mesh->SetCollisionObjectType( ECC_GameTraceChannel3 ); //NOW WE KNOW WHAT TO PUT :)
}
}
@leonardochaia
Copy link

You can now use SetCollisionProfileName(TEXT("SomePreset"));

@Manoloon
Copy link

and if you want it to be more readable , you just need to add #define COLLISION_SOMETHING ECC_GameTraceChannel3 (For example if you want to use channel3) to your project.h file.

@Manoloon
Copy link

so instead of using mesh->SetCollisionObjectType( ECC_GameTraceChannel3 ); , you can use : mesh->SetCollisionObjectType( COLLISION_SOMETHING );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment