Actor creation and lifespan

Hello friends,

Coming from a Unity/Godot world, I’m a bit surprised that creating an actor dynamically is a persistent action. I would expect that whatever happened during a play session would be reverted once that session is stopped.
Am I trying to use Flax as if it was the other engines? What is the proper flow?

Also, the OnBeginPlay OnEndPlay OnEnabled OnDisabled keep being fired after each compilation, not when play starts/ends… I must be missing something here.

What I am trying to achieve is is having an Actor on the scene that once play starts, it spawns other Actors. Any explains is welcomed :slight_smile:

Hi,

if you add a new actor using

Actor myActor = Actor.AddChild(typeof(EmptyActor));

it should get removed from the scene hierarchy once play mode exits.

Can you tell me what method you used? I wanna try to reproduce this.

Also you can read more on script event execution order Script events | Flax Documentation :slight_smile:

Hi Saas :wave:

That’s the method I use AddChild<T>

	public override void OnBeginPlay()
	{
		base.OnBeginPlay();
		AddChild<EmptyActor>();
	}

Each time the script reloads, a new empty actor is created.
I’m using Flax from the 1.12 branch btw

Hi,

oh so you are creating a custom Actor, I see.

Just to make sure, if you just want to make a script you will have to create a class that inherits from Script. What you are currently doing is creating a new Actor type (which is nothing wrong, just wanted to point that out in case you wanted to create a script instead).

If you want to go with a custom actor, you can do this:

	public override void OnBeginPlay()
	{
		base.OnBeginPlay();
        if (Engine.IsPlayMode)
        {
		    AddChild<EmptyActor>();
        }
	}

to get your desired result (-> Extra Actor will be spawned on play mode enter/ when it is added to the scene and removed when exiting play mode, no actor spawned when actor is added to the scene during non play mode).

The Engine.IsPlayMode will be true in the editor if the editor is in play mode and false if not. Also it will always return true in a shipped game.

I think it is intentional that OnBeginPlay() is called in the editor as well (during non play mode), the comment text in the method body also says that:

1 Like

I might be using Flax the same way I was using Godot… but my intension was indeed to create an Actor. One thing I love about that engine is the 1 node 1 functionality; something I would try to maintain while using Flax to the best of my ability.

if (Engine.IsPlayMode)
Was afraid the solution would be having a check like that one :frowning:
I don’t believe there is a preprocessor (in C#) only available in play mode? Something that could be using in a Conditional attribute?

I don’t believe there is a preprocessor (in C#) only available in play mode? Something that could be using in a Conditional attribute?

There is one, but that will only differentiate between Editor and cooked game, not way to differentiate between play in editor and “editing in editor” mode:

Also yeah, I would keep the 1 actor/ node 1 functionality thing, but coming from Godot myself and now using Flax, I can tell you that sometimes it’s nice to have multiple scripts on one Actor, so don’t force yourself in that paradigm too much :slight_smile:

1 Like

Solved it by introducing a base actor

public abstract class BaseActor : FlaxEngine.Actor
{
	public virtual void OnBegin() { }
	public sealed override void OnBeginPlay()
	{
		base.OnBeginPlay();
#if FLAX_EDITOR
		if (Engine.IsPlayMode)
#endif
		{
			OnBegin();
		}
	}
}

The preprocessor removes the overhead on a cooked game. The virtual methods are there just to make sure I don’t get bitten by it in the future

2 Likes