Skip to content

Latest commit

 

History

History
67 lines (50 loc) · 2.77 KB

new-script.md

File metadata and controls

67 lines (50 loc) · 2.77 KB

Create and use a script

Scripts in Flax are written in C# language (source files with extension .cs). To provide better organization in a project workspace script files are located in Source/ directory. In that way scripts are separated from the assets which reduces mess and makes it easier to work with project sources.

Flax Editor creates a solution file (.sln) and C# projects (.csproj) for game scritps and editor plugins.

Workspace

Note

We recommend using Visual Studio for code editing with Flax Engine Tools for Visual Studio installed.

Create a script

  1. Navigate to 'Source/<game_module_name>' directory in a Content window (double ckick on folder)
    Step 1
  2. Right-click and select New -> Script
    Step 2
  3. Specify a new script name and confirm using Enter
    Step 3
  4. Script is ready
    Step 4
  5. Double-click to open a file
using System;
using System.Collections.Generic;
using FlaxEngine;

namespace GraphicsFeaturesTour
{
	public class MyScript : Script
	{
		public override void OnStart()
		{
			// Here you can add code that needs to be called when script is created
		}

		public override void OnUpdate()
		{
			// Here you can add code that needs to be called every frame
		}
	}
}

Use a script

Scripts are attached to actors. Every actor can contain an unlimited amount of scripts (including multiple instances of the same script type). This means that scripts lifetime is related to actors and scenes lifetime. For instance, if you load a scene, scripts attached to the objects in that scene also will be loaded.

  1. Select an actor to add script to it (note label Drag scripts here in Scripts group in Properties window)
    Step 1
  2. Drag and drop the script into the Drag scripts here area
    Step 2
  3. Script is ready (sample script with 3 public fields)
    Step 3

Flax Editor shows public script properties and fields using a dedicated group (within Scripts group). Each script group header shows a script class type name, settings button on the right and script toggle checkbox on the left. You can disable or enable script by using this checkbox.

To remove, edit or reorder script use settings button which shows a popup with various options.

Script settings

You can also easily pick a reference to a script or reorder it. Simply click and drag the three-bar icon button as shown on a gif below:

Reorder Script