Object management
Add this in your code:
#include <engine/tools/gameplay_utility.h>
Description
Class to manage Objects (GameObject and Components).
You can Instantiate, Check and Destroy Objects.
Methods
IsValid
Check if a GameObject or a Component is valid to use.
If an object is waiting to be destroyed, the object is not valid anymore.
Only takes Component, GameObject and Transform.
Parameters:
pointer
: Pointer to the object to check
template <typename T>
bool IsValid(const std::shared_ptr<T>& pointer)
bool IsValid(const std::weak_ptr<T>& pointer)
Code sample:
std::shared_ptr<GameObject> myGameObject = CreateGameObject("MyGO");
if(IsValid(myGameObject))
{
// This object is valid
}
Destroy(myGameObject);
if(IsValid(myGameObject))
{
// This object is not valid anymore, this if won't be executed
}
Instantiate (GameObject)
Create a new GameObject from another.
(Not very recommended, can be buggy, use prefabs instead).
Parameters:
gameObject
: GameObject to duplicate
std::shared_ptr<GameObject> Instantiate(const std::shared_ptr<GameObject>& gameObject)
Code sample:
std::shared_ptr<GameObject> myGameObject = CreateGameObject("MyGO");
std::shared_ptr<GameObject> myGameObject2 = Instantiate(myGameObject);
Instantiate (Prefab)
Create a new GameObject from a prefab.
Parameters:
prefab
: Prefab to instanciate
std::shared_ptr<GameObject> Instantiate(const std::shared_ptr<Prefab>& prefab)
Code sample:
std::shared_ptr<GameObject> myGameObject = Instantiate(myPrefab);
Destroy
Destroy a GameObject or component.
warning
The pointer won't be null right after the Destroy call.
Do not use the object after calling Destroy.
Objects are fully destroy after the begin of the next frame.
Parameters:
component
/gameObject
: component or GameObject to destroy
void Destroy(const std::shared_ptr<Component>& component)
void Destroy(const std::shared_ptr<GameObject>& gameObject);
void Destroy(const std::weak_ptr<Component>& component)
void Destroy(const std::weak_ptr<GameObject>& gameObject);
Code sample:
std::shared_ptr<GameObject> myGameObject = CreateGameObject("MyGO");
Destroy(myGameObject);