Skip to content

Commit 74ada73

Browse files
authored
Merge pull request #195 from GoaLitiuM/csharp_script_doc
Improve C# scripting documentation
2 parents 476356d + 59511b0 commit 74ada73

7 files changed

Lines changed: 74 additions & 4 deletions

File tree

manual/editor/flax-build/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ This documentation section covers most of the topics related to Flax.Build tool.
2525
* [API tags](api-tags.md)
2626
* [Build Plugin](plugins.md)
2727
* [Build Tool Guide](guide.md)
28-
* [Nuget Packages](nuget-packages.md)
2928

3029
## Build Scripts
3130

manual/scripting/csharp/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# C# Scripting
2+
3+
Visit **[C# API Reference](../../../api-csharp/index.md)** to learn about scripting types.
4+
5+
## In this section
6+
7+
* [Project file management](project-file-management.md)
8+
* [Nuget Packages](nuget-packages.md)
9+
* [Restrictions](restrictions.md)
10+
11+
## How to create C# script?
12+
13+
C# scripting is enabled by default in [Flax Project modules](../../editor/flax-build/index.md), so C# scripts can be added without any further setup. In the *Content Window*, go to your project's module source folder `Source/<module_name>` and *right-click* to add **C# Script** in the module.
File renamed without changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Project file management
2+
3+
Flax.Build tool generates the required C# project files expected for the configured Code Editor, which usually uses the .csproj-file format accompanied with Visual Studio .sln-solution files. The code editor can then use these files to provide code completion and other features language server features to help the user navigate around the codebase. The compilation process does not use MSBuild which uses .csproj-files but rather compiles the source files directly with the C#-compiler using the module and build target configuration files.
4+
5+
As these files are constantly regenerated after modifying the module files or adding new source files, any permanent modifications to the generated .csproj-file should be avoided. The expected place to do these modifications are in the [module configuration files](../../editor/flax-build/index.md) (eg. `Game.Build.cd`), which are used to configure the project file generation process.
6+
7+
# Adding references
8+
9+
`/Source/Game/MyScript.cs(32,13,32,18): error CS1069: The type name 'Regex' could not be found in the namespace 'System.Text.RegularExpressions'. This type has been forwarded to assembly 'System.Text.RegularExpressions, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.`
10+
11+
When scripting requires referencing other common system references, we can modify the module build file (eg. `Game.Build.cs`) in the following way to add the reference to the required assembly:
12+
```cs
13+
public override void Setup(BuildOptions options)
14+
{
15+
base.Setup(options);
16+
17+
options.ScriptingAPI.SystemReferences.Add("System.Text.RegularExpressions");
18+
}
19+
```
20+
21+
Referencing third-party C# library files can be done with file references, but in this case we need to provide the path to the assembly file:
22+
```cs
23+
public override void Setup(BuildOptions options)
24+
{
25+
base.Setup(options);
26+
27+
// Note: the path is relative to the .build file itself
28+
options.ScriptingAPI.FileReferences.Add(Path.Combine(FolderPath, "..", "..", "Content", "CustomAssembly.dll"));
29+
}
30+
```
31+
32+
In order to add **Nuget-packages** to your project, please see the dedicated section [here](nuget-packages.md).
33+
34+
For a more thorough example to use third-party libraries can be found [here](../tutorials/use-third-party-library.html#using-c-library).
35+
36+
# Analyzers and source generators
37+
38+
Source generators and analyzers are also supported. System provided assemblies can be added in `SystemAnalyzers` and external file references to `Analyzers` lists:
39+
```cs
40+
public override void Setup(BuildOptions options)
41+
{
42+
base.Setup(options);
43+
44+
options.ScriptingAPI.SystemAnalyzers.Add("Microsoft.Interop.ComInterfaceGenerator");
45+
46+
// Note: the path is relative to the .build file itself
47+
options.ScriptingAPI.Analyzers.Add(Path.Combine(FolderPath, "..", "..", "Content", "CustomAnalyzer.dll"));
48+
}
49+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Flax tries to implement all the engine features and scripting APIs across all su
1414
- System.Reflection.Metadata.dll
1515
- System.Web.Extensions.Design.dll
1616

17+
## Static variables in editor
18+
19+
Static variables may behave differently when running the scripts in editor compared to running the cooked game. The static variables are usually initialized when the scripting assemblies are loaded and used for the first time, but Flax Editor may not always reload the assemblies between runs (unless recompilation is needed), so the values from previous runs are retained. There are many ways to work around this by using [Game Plugins](../plugins/index.md) to manage state or subscribing to `FlaxEditor.Editor.Instance.PlayModeBeginning` event which is called right before entering play mode.
20+
1721
## Ahead-of-time compile
1822

1923
Ahead-of-time (**AOT**) compile is a technique used to precompile all the managed code during game building process instead of using just-in-time (**JIT**) compilation on the target device. That's because some platforms do not allow runtime code generation. In most cases this has no effect on game scripting but in a few specific cases, AOT platforms require additional consideration.

manual/scripting/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ To start visual scripting see the related documentation [here](visual/index.md).
5959
* [Custom script editor](tutorials/custom-editor.md)
6060
* [Attributes](custom-editors/attributes.md)
6161
* [Preprocessor variables](preprocessor.md)
62-
* [Scripting restrictions](restrictions.md)
62+
* [C# Scripting](csharp/index.md)
63+
* [Project file management](csharp/project-file-management.md)
64+
* [Nuget Packages](csharp/nuget-packages.md)
65+
* [Scripting Restrictions](csharp/restrictions.md)
6366
* [C++ Scripting](cpp/index.md)
6467
* [Common Types](cpp/common-types.md)
6568
* [Collections](cpp/collections.md)

manual/toc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
### [API Tags](editor/flax-build/api-tags.md)
6161
### [Plugins](editor/flax-build/plugins.md)
6262
### [Guide](editor/flax-build/guide.md)
63-
### [Nuget Packages](editor/flax-build/nuget-packages.md)
6463
## [Editor Options](editor/options/index.md)
6564
## [Profiling](editor/profiling/index.md)
6665
### [Profiler](editor/profiling/profiler.md)
@@ -188,7 +187,10 @@
188187
### [Custom script editor](scripting/tutorials/custom-editor.md)
189188
### [Attributes](scripting/custom-editors/attributes.md)
190189
## [Preprocessor variables](scripting/preprocessor.md)
191-
## [Scripting restrictions](scripting/restrictions.md)
190+
## [C# Scripting](scripting/charp/index.md)
191+
### [Project file management](scripting/csharp/project-file-management.md)
192+
### [Nuget Packages](scripting/csharp/nuget-packages.md)
193+
### [Scripting Restrictions](scripting/csharp/restrictions.md)
192194
## [C++ Scripting](scripting/cpp/index.md)
193195
### [Common Types](scripting/cpp/common-types.md)
194196
### [Collections](scripting/cpp/collections.md)

0 commit comments

Comments
 (0)