Hi everyone,
I’m trying to migrate from Unity and I’m finding the way to be a little bit rough.
What I’m trying to do is add a torque to my sphere, which is pretty simple to be done via blueprints, but I’d like to do that in C++.
I derived a class from Pawn and defined a UStaticMeshComponent *rbComponent
. What I’m trying to do is the following:
rbComponent = Cast<UStaticMeshComponent>(RootComponent);
I also tried rbComponent = (UStaticMeshComponent*)RootComponent;
But rbComponent is always unassigned. I’m placing this code on the constructor of my class.
I’ve derived a Blueprint from the new class I’ve created and I put a Sphere as being the root component.
Is there something I’m missing? My code compiles just fine.
What I think is happening is that my C++ class doesn’t know I have a sphere as the root component on the blueprint, but how do I solve this?
Thank you from now!
So you trying to set RootComponent that you set in blueprint in to rbComponent inside constructor? You can’t do that or elkse you set RootComponent yourself in constructor beforehand
In UE4 contructors work little diffrent, it’s only called when Class Default Object (CBO) is created when you start engine (this includes editor) when nothing pretty much exists and it’s never called again, thats why constructors can only contain defaults. You need to use diffrent event, i think the best for you will be PostInitiazedComponents:
Just remeber that it also called in editor when you place thing on level, if you want to run something only in gameplay you need to use BeginPlay event
Thanks for the answer, . Yeah, I couldn’t get the component because it didn’t exist on my C++ class. What I’m trying to do now is to auto-create a component on the constructor, so that every time I create a new Blueprint that derives from my C++ class, the component is already there.
So I’m defining a UStaticMeshComponent *rbComponent;
in my .h file and in the constructor I’m doing:
rbComponent = NewNamedObject<UStaticMeshComponent>(this, TEXT("Static Mesh"));
rbComponent->AttachTo(RootComponent);
And it works, but when I click on the Static Mesh Component on my derived Blueprint, the details panel is empty?
This should be pretty simple, I guess. I must be missing something.
I really appreciate your help on this. Tips?
I was able to fix this problem by adding UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = SomeCategory)
before declaring my variable. 