Click or drag to resize

Scripting Basic Knowledge

Built-in C# ProgrammingManual / Advanced / Scripting Basic KnowledgeEvent Handlers

Scripting Basic Knowledge

This page contains some basic advices on scripting. Also, you can check the Samples and Tutorials.

Contents
How to perform action right after scene loading

Read Event Handlers page with simple scripting examples.

How to write to the log

In order to write messages to the log and to display log messages in the console of the Player, following methods are used:

Log.Info(string message);
Log.Warning(string message);
Screenshot999999523.png
How to gain access to the scene object
var scene = Scene.First;

or better way to get by another component of your scene

var scene = component.FindParent<Scene>();
How to find component by the name
var scene = Scene.First;
var obj = scene.GetComponent("MyObject");
How to call C# script method
var scene = Scene.First;
var result = scene.GetComponent("C# Script 2").MethodInvoke("Method", 2, 3);
Log.Info(result);
How to call method or property of any object by name
var result = obj.MethodInvoke("MyMethod", 2, 3);
Log.Info(result);

obj.PropertySet("MyProperty", 5); 
var value = obj.PropertyGet("MyProperty");
How to create component
var obj = scene.CreateComponent<Component>(enabled: false);
... // Before enabling, configure the object parameters.
obj.Enabled = true;
How to destroy component

To delete an object, it must be detached from the object hierarchy. After this action, the object will be completely turned off.

The first way to delete is using RemoveFromParent method which supports a queue of deleted objects. Using a queue allows you to avoid errors in cases when the list of objects is changed while working with it.

obj.RemoveFromParent( true );

You can also delete an object using the Dispose method, although in this case the object cannot be used anymore.

obj.Dispose();
How to add property to any component from code

You can add a property directly in the editor.

// Add.
var property = scene.CreateComponent<VirtualProperty>(enabled: false);
property.Name = "MyProperty";
property.Type = MetadataManager.GetTypeOfNetType(typeof(double));
property.Enabled = true;

// Set, get.
scene.PropertySet("MyProperty", 5);
var value = obj.PropertyGet("MyProperty");
Log.Warning(value);
How to place 3D model in the scene
// Create mesh in space object without enabling.
var meshInSpace = scene.CreateComponent<MeshInSpace>(enabled: false);

// Set transform.
meshInSpace.Transform = new Transform(new Vector3(2, 1, 2), new Angles(0, 0, 45));

// Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt).
meshInSpace.SetPosition(2, 1, 2);
meshInSpace.SetRotation(0, 0, 45);

// Set 3D model.
var resourceName = @"Samples\Starter Content\Models\Sci-fi Drone\Sci-fi Drone.fbx";
var meshNameInsideResource = "$Mesh";
meshInSpace.Mesh = ReferenceUtility.MakeReference(resourceName + "|" + meshNameInsideResource);

// Enable the object in the scene.
meshInSpace.Enabled = true;
Screenshot999999631.png
How to place prepared scene object in the scene
// Get object type.
var resourceName = @"Samples\Starter Content\Scene objects\Sci-fi Box\Box type.scene";
var objectNameInsideResource = "$Box";
var boxType = MetadataManager.GetType(resourceName + "|" + objectNameInsideResource);

// Create the object without enabling.
var box = (ObjectInSpace)scene.CreateComponent(boxType, enabled: false);

// Set initial position.
// If object contains physical body, then need change transform of the body instead of updating the object itself.
box.GetComponent<RigidBody>().Transform = new Transform(new Vector3(2, 4, 3), new Angles(0, 0, -45));

// Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt).
box.SetPosition(2, 4, 3);
box.SetRotation(0, 0, -45);

// Enable the object in the scene.
box.Enabled = true;
Screenshot999999633.png

See also
Built-in C# ProgrammingEvent Handlers