Search Results for

    Show / Hide Table of Contents

    Creating and destroying objects

    Scene objects lifetime is controlled by the Flax but the game can also access New/Destroy methods that allow to manage the scene at runtime. Some games keep a constant number of objects in the scene, but it is very common for characters, treasures and other object to be created and removed during gameplay.

    Spawning objects

    C#

    Example code that spawns a new point light:

    • C#
    • C++
    public override void OnStart()
    {
        var light = new PointLight();
        light.Color = Color.Blue;
        light.Parent = Actor;
    }
    
    void ExampleScript::OnStart()
    {
        auto light = New<PointLight>();
        light->Color = Color::Blue;
        light->SetParent(GetActor());
    }
    

    You can add new scripts to any objects by using AddScript method:

    • C#
    • C++
    public override void OnStart()
    {
        var player = Actor.AddScript<Player>();
        player.HP = 100;
    }
    
    void ExampleScript::OnStart()
    {
        auto script = New<Player>();
        script->SetParent(GetActor());
        script->HP = 100;
    }
    
    Note

    Scene objects (actors, scripts) should not use constructors to prevent issues.

    Removing objects

    Flax supports immediate and delayed objects removing system. This helps with cleanup up the scene from killed players or unused actors.

    • C#
    • C++
    using FlaxEngine;
    
    public class MyScript : Script
    {
        public SpotLight Flashlight;
    
        public override void OnStart()
        {
            Destroy(ref Flashlight);
        }
    }
    
    #pragma once
    
    #include "Engine/Scripting/Script.h"
    #include <Engine/Level/Actors/SpotLight.h>
    #include <Engine/Scripting/ScriptingObjectReference.h>
    
    API_CLASS() class EXAMPLE_API MyScript : public Script
    {
        API_AUTO_SERIALIZATION();
        DECLARE_SCRIPTING_TYPE(MyScript);
    
        API_FIELD()
        ScriptingObjectReference<SpotLight> Flashlight;
    
        // [Script]
        void OnStart() override
        {
            Flashlight.Get()->DeleteObject();
        }
    };
    

    Here is an example script that will remove object after a specified timeout (in seconds):

    • C#
    • C++
    using FlaxEngine;
    
    public class AutoRemoveObj : Script
    {
        [Tooltip("The time left to destroy object (in seconds).")]
        public float Timeout = 5.0f;
    
        public override void OnStart()
        {
            Destroy(Actor, Timeout);
        }
    }
    
    #pragma once
    
    #include "Engine/Scripting/Script.h"
    
    API_CLASS() class GAME_API AutoRemoveObj : public Script
    {
        API_AUTO_SERIALIZATION();
        DECLARE_SCRIPTING_TYPE(AutoRemoveObj);
    
        API_FIELD(Attributes = "Tooltip(\"The time left to destroy object (in seconds).\")")
        float Timeout = 5.0f;
    
        // [Script]
        void OnStart() override 
        {
            GetActor()->DeleteObject(Timeout);
        }
    };
    

    In the same way you can remove scripts:

    • C#
    • C++
    Destroy(Actor.GetScript<Player>());
    
    GetActor()->GetScript<ExampleScript>()->DeleteObject();
    
    • Improve this Doc
    In This Article
    Back to top Copyright © 2012-2023 Wojciech Figat