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.
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:
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:
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:
[!code-csharpExample1]
[!code-cppExample2]
Scripts attached to the actors can be queries like the actors using a dedicated methods:
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:
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();
}
Flax implements API to find objects.
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.
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();
}