Search code examples
c++unity-game-engineunreal-engine4

Adding C++ script to objects in Unreal in the same way as Unity?


I created a SkeletalMesh object in the scene, and in the Editor I added a C++ component, but once I attached it to the SkeletalMesh I can't find out where it is in the Unreal panel. I know it's working, because after I attached the script to the object it prints some simple debug output.

The main reason I want to know where to find my script component is because I want to able to reference the SkeletalMesh the script is attached through the C++ in the same way you can in Unity with C#.

Is there a way I can reference the object my C++ script is attached to and access the SkeletalMesh values through C++?

My code is just a shell right now, but here I commented the places I want to create a reference to the SkeletalMesh and where I want to animate it:

#include "Glove.h"


// Sets default values for this component's properties
UGlove::UGlove()
{
    PrimaryComponentTick.bCanEverTick = true;
}


// Called when the game starts
void UGlove::BeginPlay()
{
    Super::BeginPlay();
    // GET SKELETALMESH VALUE HERE SOMEHOW
}


// Called every frame
void UGlove::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    // ANIMATE SKELETALMESH HERE SOMEHOW
}

Solution

  • To Referece SkeltaMesh to your script ,there is no way to do it like unity when you attach a script , however there is a way around.

    1. Select your SkeltaMesh in the scene
    2. Go to detail panel and add your c++ component.
    3. Check your detail panel to know where your script live in the tree and who is its parent , if the sketalmesh directly is its parent, this will be too easy, all you need to get the parent component or actor and cast to it , then return value is the thing you looking for if the cast is successful
    4. in this example I did for a static mesh . Event begin play assigning the skeltamesh

    But you have to make your variable in the Script public and editable inside blueprint to be able to set them at run time .

    I had the same issue but was with blueprint only .