Search Results for

    Show / Hide Table of Contents

    Accessing scene objects

    One of the most important aspects of the scripts is interaction and accessing other scene objects including of course the actor that script is attached to. For instance, if game wants to spawn a ball in front of the player, it needs to get a player location and a view direction which can be done by using Flax.

    Accessing actors

    Every script has inherited property Actor that represents an actor the script is attached to. For example you can use it to modify the actor position every frame:

    • C#
    • C++
    public override void OnFixedUpdate()
    {
        Actor.Position += new Vector3(0, 2, 0);
    }
    
    void ScriptExample::OnFixedUpdate()
    {
        GetActor()->SetPosition(GetActor()->GetPosition() + Vector3(0, 2, 0));
    }
    

    You can also print its name:

    • C#
    • C++
    Debug.Log(Actor.Name);
    
    DebugLog::Log(GetActor()->GetName());
    

    See Actor class reference to learn more.

    You can also print all child actors and rotate the parent actor:

    • C#
    • C++
    using FlaxEngine;
    
    public class MyScript : Script
    {
        public override void OnStart()
        {
            // Prints all children names
            var children = Actor.Children;
            foreach (var a in children)
                Debug.Log(a.Name);
    
            // Changes the child point light color (if has)
            var pointLight = Actor.GetChild<PointLight>();
            if (pointLight)
                pointLight.Color = Color.Red;
        }
    
        public override void OnFixedUpdate()
        {
            // Rotates the parent object
            Actor.Parent.LocalEulerAngles += new Float3(0, 2, 0);
        }
    }
    
    #pragma once
    
    #include "Engine/Scripting/Script.h"
    #include "Engine/Debug/DebugLog.h"
    
    API_CLASS()
    class GAME_API ScriptExample : public Script
    {
        API_AUTO_SERIALIZATION();
        DECLARE_SCRIPTING_TYPE(ScriptExample);
    
        // [Script]
        void OnEnable() override;
        void OnDisable() override;
        void OnStart() override
        {
            // Prints all children names
            Array<Actor*> children = GetActor()->GetChildren<Actor>();
            for each (auto a in children)
                DebugLog::Log(a->GetName());
    
            // Changes the child point light color (if has)
            auto pointLight = GetActor()->GetChild<PointLight>();
            if (pointLight)
                pointLight->Color = Color::Red;
        }
        void OnUpdate() override
        {
            // Rotates the parent object
            Float3 targetOrientation = GetActor()->GetParent()->GetLocalOrientation().GetEuler();
            targetOrientation += {0, 2, 0};
    
            GetActor()->GetParent()->SetLocalOrientation(Quaternion::Euler(targetOrientation));
        }
    };
    

    Accessing other scripts

    Scripts attached to the actors can be queries like the actors using a dedicated methods:

    • C#
    • C++
    private void OnTriggerEnter(Collider collider)
    {
        // Deal damage to the player when enters the trigger
        var player = collider.GetScript<Player>();
        if (player)
            player.DealDamage(10);
    }
    
    void ScriptExample::OnTriggerEnter(Collider* collider)
    {
        // Deal damage to the player when enters the trigger
        auto player = collider->GetScript<Player>();
        if (player)
            player->DealDamage(10);
    }
    

    You can also query all the scripts of the any actor and use them to perform any action:

    • C#
    • C++
    private void OnTriggerEnter(Collider collider)
    {
        foreach (var provider in collider.GetScripts<IAdProvider>())
           provider.ShowAd();
    }
    
    void ScriptExample::OnTriggerEnter(Collider* collider)
    {
        for each (auto provider in collider->GetScripts<IAdProvider>())
            provider.ShowAd();
    }
    

    Finding actors

    Flax implements API to find objects.

    • C#
    • C++
    private void OnTriggerLeave(Collider collider)
    {
        var obj = Actor.Scene.FindActor("Spaceship");
        Destroy(obj);
    }
    
    void ScriptExample::OnTriggerLeave(Collider* collider)
    {
        auto obj = GetActor()->GetScene()->FindActor(TEXT("Spaceship"));
        obj->DeleteObject();
    }
    

    However, in most cases, the best solution is to expose a field with reference to the object and set it in the editor to improve game performance.

    • C#
    • C++
    public Actor Spaceship;
    
    private void OnTriggerLeave(Collider collider)
    {
        Destroy(ref Spaceship);
    }
    
    //.h
    API_FIELD()
    ScriptingObjectReference<Actor> Spaceship;
    
    //.cpp
    void ScriptExample::OnTriggerLeave(Collider* collider)
    {
        CHECK(Spaceship);
        Spaceship->DeleteObject();
    }
    
    • Improve this Doc
    In This Article
    Back to top Copyright © 2012-2024 Wojciech Figat