在虚幻引擎5的C++代码中如何将场景组件附加到角色。

huangapple go评论106阅读模式
英文:

How to attach scene component to actor from the c++ code in Unreal Engine 5

问题

I need to attach a scene component to an actor but in a way that I will be able to access the things (functions, variables) in the scene component from another actor. For example, I have an actor called Door1 and I have a scene component called DoorSceneComponent, and I want to attach the DoorSceneComponent to the root of Door1 but in the C++ code, not in the Unreal Editor. I also have a Character class called PlayerCharacter. In this class, I have a reference to Door1. The thing is that I want to be able to call a function that is defined in DoorSceneComponent from the PlayerCharacter class. My question is, how can I attach the DoorSceneComponent to Door1 in C++?

英文:

I need to attach a scene component to an actor but in a way that i will be able to access the things(function, variables) in the scene component from another actor. for example, i have an actor called Door1 and i have a scene component called DoorSceneComponent and i want to attach the DoorSceneComponent to the root of Door1 but in the c++ code, not in the Unreal Editor. I also have a Character class called PlayerCharacter in this class, i have a refrence to Door1, the thing is that i want to be able to call a function that is defiened in DoorSceneComponent from the PlayerCharacter class. My question is, how can i attach the DoorSceneComponent to Door1 in c++?
I use Unreal Engine 5.1
I use Manjaro Linux as my OS.
If you need any more information just say.
Thanks 在虚幻引擎5的C++代码中如何将场景组件附加到角色。

答案1

得分: 0

首先,您需要保持对`DoorSceneComponent`实例的指针。所以在您的Door1头文件中:

Door1.h

private: //(或者根据您的需求选择protected)
UPROPERTY()
DoorSceneComponent* DoorComp;

(如果它有在编辑器中需要编辑的属性,那么您需要根据您需要的确切行为之一,向UPROPERTY添加[这些标签][1]。)

在Door1构造函数中,您需要实际创建场景组件:

Door1.cpp

ADoor1::ADoor1() {
// 创建组件
DoorComp = CreateDefaultSubobject<UDoorSceneComponent>(TEXT("Door Component));
// 将其附加到根组件
DoorComp->SetupAttachment(RootComponent);
}

[CreateDefaultSubobject][2]

[SetupAttachment][3]

完成此操作并重新编译后,该组件将附加到您的角色的任何蓝图/实例中。

[1]: https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/GameplayArchitecture/Properties/Specifiers/
[2]: https://docs.unrealengine.com/4.26/en-US/API/Runtime/CoreUObject/UObject/UObject/CreateDefaultSubobject/2/
[3]: https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/Components/USceneComponent/SetupAttachment/
英文:

This one takes a couple separate parts.
Firstly, you'll want to hold a pointer to an instance of your DoorSceneComponent. So in your Door1 header:

Door1.h

private: //(or protected, depending on your needs)
UPROPERTY()
DoorSceneComponent* DoorComp;

(If it has properties that need to be edited in the editor, then you'll need to add one of these tags to the UPROPERTY depending on what exact behaviour you require.)

In your Door1 constructor, you need to actually create the scene component:

Door1.cpp

ADoor1::ADoor1() {
    // Create the component
    DoorComp = CreateDefaultSubobject&lt;UDoorSceneComponent&gt;(TEXT(&quot;Door Component));
    // Attach it to the root
    DoorComp-&gt;SetupAttachment(RootComponent);
}

CreateDefaultSubobject

SetupAttachment

Once you've done this and recompiled, the component will be attached in any blueprints/instances of your actor.

huangapple
  • 本文由 发表于 2023年4月6日 21:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定