Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GameObject

Add this in your code:

#include <engine/game_elements/gameobject.h>

Description

A GameObject is a class that contains some properties and a list of components.
Includes functions to create GameObjects.

Global Methods


CreateGameObject

Create a GameObject with the default name.

std::shared_ptr<GameObject> CreateGameObject()

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject();
Debug::Print(myGameObject->GetName()); // Prints "GameObject"

CreateGameObject

Create a GameObject.

Parameters:

  • name: Name of the GameObject
std::shared_ptr<GameObject> CreateGameObject(const std::string& name)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
Debug::Print(myGameObject->GetName()); // Prints "Player"

FindGameObjectByName

Find a GameObject with a name.

Parameters:

  • name: Name of the GameObject
std::shared_ptr<GameObject> FindGameObjectByName(const std::string& name)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
std::shared_ptr<GameObject> result = FindGameObjectByName("Player");
if(myGameObject == result)
{
    Debug::Print("GameObject found!");
}

FindGameObjectsByName

Find GameObjects with a name.

Parameters:

  • name: Name of the GameObject
std::vector<std::shared_ptr<GameObject>> FindGameObjectsByName(const std::string& name)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
std::shared_ptr<GameObject> myGameObject2 = CreateGameObject("Player");
std::vector<std::shared_ptr<GameObject>> results = FindGameObjectsByName("Player");
if(results.size() == 2)
{
    Debug::Print("Players found!");
}

FindGameObjectById

Find a GameObject with an id.

Parameters:

  • id: Unique id of the GameObject
std::shared_ptr<GameObject> FindGameObjectById(const uint64_t id)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
std::shared_ptr<GameObject> result = FindGameObjectById(myGameObject->GetUniqueId());
if(myGameObject == result)
{
    Debug::Print("GameObject found!");
}

FindComponentById

Find a component with an id.

Parameters:

  • id: Unique id of the component
std::shared_ptr<GameObject> FindComponentById(const uint64_t id)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
std::shared_ptr<AudioSource> myAudioSource = myGameObject->AddComponent<AudioSource>();
std::shared_ptr<Component> result = FindComponentById(myAudioSource->GetUniqueId());
if(myAudioSource == result)
{
    std::shared_ptr<AudioSource> resultAudioSource = std::dynamic_pointer_cast<AudioSource>(result);
    Debug::Print("Component found!");
}

Pubic Methods


AddChild

Add a child to the GameObject.

Parameters:

  • gameObject: Child to add
void AddChild(const std::shared_ptr<GameObject>& gameObject)

Code sample:

std::shared_ptr<GameObject> myGameObject = CreateGameObject("Player");
std::shared_ptr<GameObject> myChild = CreateGameObject("Gun");
myGameObject->AddChild(myChild);

SetParent

Set GameObject's parent.

Parameters:

  • gameObject: New parent
void SetParent(const std::shared_ptr<GameObject>& gameObject)

Code sample:

std::shared_ptr<GameObject> player0 = CreateGameObject("Player");
std::shared_ptr<GameObject> player1 = CreateGameObject("Player");
std::shared_ptr<GameObject> parent = CreateGameObject("Players");
player0->SetParent(parent);
player1->SetParent(parent);

AddComponent

Add a component.
T is a component type

template <typename T>
AddComponent()

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<Light> lightComp = gameObject->AddComponent<Light>();
std::shared_ptr<AudioSource> audioSourceComp = gameObject->AddComponent<AudioSource>();

GetComponent

Get a component.
T is a component type

template <typename T>
GetComponent()

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<Light> lightComp = gameObject->AddComponent<Light>();
std::shared_ptr<Light> comp = gameObject->GetComponent<Light>();

GetComponents

Get components.
T is a component type

template <typename T>
GetComponents()

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<Light> lightComp = gameObject->AddComponent<Light>();
std::shared_ptr<Light> lightComp2 = gameObject->AddComponent<Light>();
std::vector<std::shared_ptr<Light>> lightsComp = gameObject->GetComponents<Light>();

IsActive

Get if the GameObject is marked as active.

bool IsActive() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
gameObject->SetActive(false);
bool isActive = gameObject->IsActive(); // = false

//....

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<GameObject> child = CreateGameObject();
child->SetParent(child);
gameObject->SetActive(false);
bool isActive = child->IsActive(); // = true

IsLocalActive

Get if the GameObject is active based on his parents active state.

bool IsLocalActive() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
gameObject->SetActive(false);
bool isActive = gameObject->IsLocalActive(); // = false

//....

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<GameObject> child = CreateGameObject();
child->SetParent(child);
gameObject->SetActive(false);
bool isActive = child->IsLocalActive(); // = false

SetActive

Set GameObject as active or not.

Parameters:

  • active: Active value
void SetActive(const bool active)

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
gameObject->SetActive(false);
bool isActive = gameObject->IsActive(); // = false

GetChildrenCount

Get children count.

uint32_t GetChildrenCount() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<GameObject> child0 = CreateGameObject();
std::shared_ptr<GameObject> child1 = CreateGameObject();

gameObject->AddChild(child0);
gameObject->AddChild(child1);
uint32_t count = gameObject->GetChildrenCount(); // = 2

GetComponentCount

Get component count.

uint32_t GetComponentCount() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
gameObject->AddComponent<Light>();
gameObject->AddComponent<Light>();
gameObject->AddComponent<Light>();

uint32_t count = gameObject->GetComponentCount(); // = 3

GetTransform

Get transform.

const std::shared_ptr<Transform>& GetTransform() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
const std::shared_ptr<Transform>& transform = gameObject->GetTransform();

GetParent

Get parent GameObject.

const std::weak_ptr<GameObject>& GetParent() const

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
const std::weak_ptr<GameObject>& parent = gameObject->GetParent();

if(!parent.expired())
{
    Debug::Print("The gameobject does not have a parent");
}
else
{
    Debug::Print("The gameobject has a parent");
}

GetChild

Get a child by index.

Parameters:

  • index: Child index
std::weak_ptr<GameObject> GetChild(int index)

Code sample:

std::shared_ptr<GameObject> gameObject = CreateGameObject();
std::shared_ptr<GameObject> child0 = CreateGameObject();
std::shared_ptr<GameObject> child1 = CreateGameObject();

gameObject->AddChild(child0);
gameObject->AddChild(child1);

std::weak_ptr<GameObject> weakChild1 = gameObject->GetChild(1);

std::shared_ptr<GameObject> sharedChild1 = weakChild1.lock();
if(sharedChild1)
{
    // Use child...
}