GameObject

An object in the scene. Contains components that define behavior and can have child GameObjects.

using Xengine.Scene;

Creating GameObjects

// Via Scene
var obj = scene.CreateGameObject("Player");
var obj2 = scene.CreateGameObject("Enemy", new Vector3(5, 0, 0));

// With position and rotation
var obj3 = scene.CreateGameObject("Item", position, rotation);

// Direct construction
var obj4 = new GameObject("MyObject");
scene.AddGameObject(obj4);

Properties

PropertyTypeDescription
IdintUnique numeric ID
GuidGuidUnique GUID
NamestringObject name
EnabledboolEnable/disable object
ActiveboolTrue if enabled and parent active
SceneScene?Scene containing this object
ParentGameObject?Parent object (set to reparent)
ChildrenIReadOnlyListChild objects
ComponentsIReadOnlyListAttached components
TransformGameTransformPosition, rotation, scale
IsDestroyedboolTrue if destroyed
IsRootboolTrue if no parent
RootGameObjectRoot of hierarchy

Component Methods

AddComponent<T>

T AddComponent<T>() where T : Component, new()
var renderer = player.AddComponent<CubeRenderer>();
renderer.Color = Color.Blue;

GetComponent<T>

T? GetComponent<T>() where T : Component
var health = enemy.GetComponent<HealthComponent>();
if (health != null)
    health.TakeDamage(10);

GetComponents<T>

IEnumerable<T> GetComponents<T>()
foreach (var renderer in obj.GetComponents<CubeRenderer>())
    renderer.Color = Color.Red;

RemoveComponent<T>

bool RemoveComponent<T>()
obj.RemoveComponent<CubeRenderer>();

HasComponent<T>

bool HasComponent<T>()
if (obj.HasComponent<RigidbodyComponent>())
    Log.Info("Has physics!");

GetComponentInChildren<T>

T? GetComponentInChildren<T>(bool includeInactive = false)

Find component on this object or any descendant.

GetComponentInParent<T>

T? GetComponentInParent<T>()

Find component on this object or any ancestor.

Hierarchy

// Set parent
child.Parent = parent;

// Check ancestry
if (parent.IsAncestorOf(child))
    Log.Info("Is ancestor!");

// Get root
var root = deepChild.Root;

// Iterate children
foreach (var child in parent.Children)
    Log.Info(child.Name);

Destroying

// Destroy object and all children/components
obj.Destroy();

// Check if destroyed
if (obj.IsDestroyed) return;

Complete Example

// Create player with components
var player = scene.CreateGameObject("Player", new Vector3(0, 1, 0));
player.AddComponent<PlayerMovement>();
player.AddComponent<CubeRenderer>().Color = Color.Blue;
player.AddComponent<HealthComponent>().MaxHealth = 100;

// Create weapon as child
var weapon = scene.CreateGameObject("Weapon");
weapon.Parent = player;
weapon.Transform.LocalPosition = new Vector3(0.5f, 0, 0.5f);
weapon.AddComponent<WeaponComponent>();

// Access from component
public class PlayerMovement : Component
{
    protected override void OnUpdate()
    {
        // Access transform
        Transform.Position += Vector3.Forward * Time.Delta;
        
        // Access sibling component
        var health = GetComponent<HealthComponent>();
        
        // Access child component
        var weapon = GetComponentInChildren<WeaponComponent>();
        
        // Access parent's scene
        var enemies = Scene.FindAllByName("Enemy");
    }
}