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.
Note
We recommend using Visual Studio for code editing with Flax Engine Tools for Visual Studio installed.
- Navigate to 'Source/<game_module_name>' directory in a Content window (double ckick on folder)
- Right-click and select New -> Script
- Specify a new script name and confirm using Enter
- Script is ready
- 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
}
}
}
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.
- Select an actor to add script to it (note label Drag scripts here in Scripts group in Properties window)
- Drag and drop the script into the Drag scripts here area
- Script is ready (sample script with 3 public fields)
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.
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: