英文:
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
答案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<UDoorSceneComponent>(TEXT("Door Component));
// Attach it to the root
DoorComp->SetupAttachment(RootComponent);
}
Once you've done this and recompiled, the component will be attached in any blueprints/instances of your actor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论