ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
GameObject类 参考final

游戏物体类 更多...

#include <GameObject.h>

Public 成员函数

 ~GameObject ()=default
 GameObject (const GameObject &)=delete
GameObject & operator= (const GameObject &)=delete
 GameObject (GameObject &&)=delete
GameObject & operator= (GameObject &&)=delete
void destroy ()
 标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体
void setParent (GameObject *parent)
 设置父物体
GameObject * getParent () const
 获取父物体
const std::vector< GameObject * > & getChildren () const
 获取子物体列表
void addChild (GameObject *child)
 添加子物体(等价于 child->setParent(this))
const std::string & getName () const
const std::string & getTag () const
Scene * getScene () const
bool isNeedDestroy () const
bool isActive () const
 自身启用标志(isActiveInHierarchy 为含父链的最终生效状态)
bool isActiveInHierarchy () const
 层级生效状态:自身与所有祖先都启用(Unity 语义:父失活子随失活)
void setActive (bool active)
void setName (const std::string &name)
void setTag (const std::string &tag)
void setScene (Scene *scene)
 设置所属场景(同时触发未注册组件的 onAttach)
void setNeedDestroy (bool needDestroy)
std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents ()
 获取全部组件(按 type_index 索引)
template<typename Fn>
void forEachComponent (Fn &&fn)
 只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map) 安全遍历:先快照指针再逐回调,且回调前重验组件仍归属本对象—— 回调内 removeComponent 修改 map 不会使迭代器失效,已移除的组件也不会被调用。
template<typename T>
WeakComponentRef< T > getWeakRef ()
 创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)
Component * addComponentInstance (Component *component)
 动态添加组件实例(Prefab 实例化等反射场景用,所有权转移给 GameObject)
template<typename T, typename... Args>
T * addComponent (Args &&... args)
 添加组件
template<typename T>
T * getComponent ()
 获取组件
template<typename T>
bool hasComponent ()
 检查是否存在某个组件
template<typename T>
void removeComponent ()
 移除某个组件
void removeComponent (const std::type_index &typeIndex)
 按类型索引移除组件(编辑器/反射场景用,与 removeComponent<T>() 等价)

友元

class Scene

详细描述

游戏物体类

构造函数私有,只能通过 Scene::createGameObject 或 Scene::instantiate 创建。

构造及析构函数说明

◆ ~GameObject()

~GameObject ( )
default

◆ GameObject() [1/2]

GameObject ( const GameObject & )
delete

◆ GameObject() [2/2]

GameObject ( GameObject && )
delete

成员函数说明

◆ addChild()

void addChild ( GameObject * child)
inline

添加子物体(等价于 child->setParent(this))

◆ addComponent()

template<typename T, typename... Args>
T * addComponent ( Args &&... args)
inline

添加组件

模板参数
T组件类型(须继承 Component)
Args构造参数类型
参数
args传递给组件构造函数
返回
组件指针(若已存在则返回已有的)

◆ addComponentInstance()

Component * addComponentInstance ( Component * component)

动态添加组件实例(Prefab 实例化等反射场景用,所有权转移给 GameObject)

参数
component已通过反射工厂创建的组件实例
返回
添加后的组件指针(若已存在同类型,则丢弃传入实例并返回已有的)

◆ destroy()

void destroy ( )

标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体

◆ forEachComponent()

template<typename Fn>
void forEachComponent ( Fn && fn)
inline

只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map) 安全遍历:先快照指针再逐回调,且回调前重验组件仍归属本对象—— 回调内 removeComponent 修改 map 不会使迭代器失效,已移除的组件也不会被调用。

◆ getChildren()

const std::vector< GameObject * > & getChildren ( ) const
inline

获取子物体列表

◆ getComponent()

template<typename T>
T * getComponent ( )
inline

获取组件

模板参数
T组件类型
返回
组件裸指针

◆ getComponents()

std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents ( )
inline

获取全部组件(按 type_index 索引)

◆ getName()

const std::string & getName ( ) const
inline

◆ getParent()

GameObject * getParent ( ) const
inline

获取父物体

◆ getScene()

Scene * getScene ( ) const
inline

◆ getTag()

const std::string & getTag ( ) const
inline

◆ getWeakRef()

template<typename T>
WeakComponentRef< T > getWeakRef ( )
inline

创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)

◆ hasComponent()

template<typename T>
bool hasComponent ( )
inline

检查是否存在某个组件

模板参数
T组件类型
返回
是否存在组件

◆ isActive()

bool isActive ( ) const
inline

自身启用标志(isActiveInHierarchy 为含父链的最终生效状态)

◆ isActiveInHierarchy()

bool isActiveInHierarchy ( ) const
inline

层级生效状态:自身与所有祖先都启用(Unity 语义:父失活子随失活)

◆ isNeedDestroy()

bool isNeedDestroy ( ) const
inline

◆ operator=() [1/2]

GameObject & operator= ( const GameObject & )
delete

◆ operator=() [2/2]

GameObject & operator= ( GameObject && )
delete

◆ removeComponent() [1/2]

template<typename T>
void removeComponent ( )
inline

移除某个组件

模板参数
T组件类型

生命周期调用顺序:onDetach → onDestroy

◆ removeComponent() [2/2]

void removeComponent ( const std::type_index & typeIndex)
inline

按类型索引移除组件(编辑器/反射场景用,与 removeComponent<T>() 等价)

参数
typeIndex组件类型索引(TypeInfo::typeIndex / typeid(T))

◆ setActive()

void setActive ( bool active)
inline

◆ setName()

void setName ( const std::string & name)
inline

◆ setNeedDestroy()

void setNeedDestroy ( bool needDestroy)
inline

◆ setParent()

void setParent ( GameObject * parent)

设置父物体

自动从旧父物体的子列表中移除自己,并加入新父物体的子列表。 传入 nullptr 即解除当前父子关系。跨 Scene 的父子关系将被拒绝并记 WARN。

参数
parent新父物体(可为 nullptr)

◆ setScene()

void setScene ( Scene * scene)

设置所属场景(同时触发未注册组件的 onAttach)

◆ setTag()

void setTag ( const std::string & tag)
inline

◆ Scene

friend class Scene
friend

该类的文档由以下文件生成: