Script Basis
Last updated
Last updated
Scripting system implemented in Force by using Mono Runtime. Mono its specific platform that allows to access to Force Engine features written initially on C++, using C# and scripts.
If you select a script, you can view its preview as part of the asset.
Since Force uses Mono it will compile the code of all scripts into one separate dynamic model Force.CSharp.Project.dll, (DLL) where .Project is name of you working project, This file can be deleted from your project directory. And if that the case Force just recompiles script project and place this binary back there. But from project Binary/.runtime folder you cannot delete it if you in play mode.
When you double-click a script Asset in Force or by clicking on Open in IDE in preview asset, it will be opened in a text editor. Force by default uses as text editor Visual Studio. If you want to change it, go in to File -> Preferences -> Project Settings -> Build -> Default Script IDE. But really you can opened this file in wherever editor you want, even now you can edit it using built-in Force Script Editor. And of course not forgot to save it.
For fast changing script file you can use Script Editor as Force built-in tool. But to do that you need firstly assign this script to any object's script component. About that we will talk later.
When you open script the initial content of file will looking like this:
The script works in such a way that it does not run out of thin air. It is called using Mono Runtime at a certain point of gameplay. Each script is derived from the MonoScript class which is also a normal component. When we create a script component in the editor and bind a script file to it, it creates a reflection of this script in C#, and this is the main task of many for MonoScript. Thus, every time you bind a script component to a GameObject's, it will create its reflection (a new instance of the object) in C# code that we can use. The name of the script is taken directly from the name of its file. It is not recommended to create other names for the script classes if you have not changed its file name.
Initially, when creating a script, it will have two methods by default. These methods are called Events because they are not independent, but are called at a certain point in the operation of your game by Force.
OnStart method calls when script attached and you click on play and go to in runtime. This method will call once per runtime session, only on start. OnStart
usually using for initialize other variables, objects, or instantiate prefabs. OnUpdate will calls every game cycle and using for writing mostly all logic for you game at least for now in Force. The OnUpdate
also has one parameter is delta. Delta or Delta Time is basically framerate of you application/game in float point value. To get normal frame rate count, you can just write float FPS = 1.0f / delta
.
If you use languages such as C#, Java, C++, then you may have noticed that there is no constructor in the script. This is exactly what is a feature of scripts, because you do not need to initialize script objects yourself, since Force does it for you. If you try to create a constructor in the script, it will invokes as normal but may lead to an undefined behaviour, script can not start and most likely the editor will crash without an error at all.