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.

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);
How to gain access to the scene object
var scene = Component_Scene.All[0];
var scene = Component_Scene.First;
How to find component by the name
var scene = Component_Scene.All[0];
var obj = scene.GetComponent("MyObject");
How to call C# script method
var scene = Component_Scene.All[0];
var result = scene.GetComponent("C# Script 2").MethodInvoke("Method", 2, 3);
How to call method or property of any object by name
var result = obj.MethodInvoke("MyMethod", 2, 3);

obj.PropertySet("MyProperty", 5); 
var value = obj.PropertyGet("MyProperty");
How to add property to any component from code

You can add a property directly in the editor.

// Add.
var property = scene.CreateComponent<Component_Property>(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");
How to place 3D model in the scene
// Create mesh in space object without enabling.
var meshInSpace = scene.CreateComponent<Component_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;
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 = (Component_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<Component_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;

See also
Built-in C# ProgrammingEvent Handlers