Events In Script
Last updated
Last updated
This image below show barely how Force group these events, and basically in witch order it calls.
As was described in the first chapter of scripting, 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.
That methods is OnCreate and OnUpdate. But Force has a lot more of this methods, and this methods has its own usage. And also have its differences that will different behaves on GameObjects in scene on some time of gameplay.
All these events called only if it was declared in script, other wise it will be skipped.
Here a list with all supported by Force method events, that shown here in order of its execution.
Events | Description |
---|---|
OnCreate() | Calls right after the script first creates instance to itself. |
OnValidate() | Calls after |
OnEnable() | Calls once when game object attached to script becomes enabled and its script is executable. |
OnStart() | Calls when script instantiate all object from scene (create its instances), instantiate prefabs, and all data for playing. |
OnUpdate(float delta) | Calls every frame in scene, after start and to that point when user not stop the scene. |
OnPostUpdate() | Calls right away after the |
OnPreRender() | Calls after the main framebuffer clear the screen (main scene viewport), but before main render pass. |
OnRender() | Calls in scene between rendering context of the scene and allows draw primitives. |
OnPostRender() | Calls after the main render pass, and usefull for custom rendering scenes. Order of drawing primitives here is takes count. |
OnRenderGui() | Calls when between UI rendering context, and usefull for panel rendering. |
OnDisable() | Calls once when game object attached to script becomes disabled and/or its script becomes not executable. |
OnDisabled() | Calls every frame, only when script was disabled for processing. This allows to user re-enable disabled script. |
OnDestroy() | Calls when user destroy script from game object in inspector or by |
When scene starts to play, and if it GameObject's atleast has one Script first couple events can be executed. First of all when any script attached to GameObject in Editor to CSharpScript component (in Script API its MonoScript or MonoEmptyScript) here it will creates first instance to itself (instantiate it) to be accessible, and right after that Force calls OnCreate()
event. At this point all objects that has contains in running scene and other objects like prefabs, not have its instances at this moment yet. So you cannot access and Find()
objects here, this literally will be exception.
Next what happens that Force instantiate all scene GameObjects and its Prefab objects, so user can access to it from script, and then OnValidate()
will called. Also this event will called every time when scripts will recompile in play mode to always have a valid and not-null instance. From this point user safely can Find()
objects and change its behaviour, add or destroy components, objects etc.
Right after OnValidate()
, Force will call OnEnable()
event on that script GameObject that was enabled throw check box in Editor.
And finally last from this group will called OnStart()
. This is great place to setting up you game properties, logic, instantiate other objects, and variables, or prefabs. And you should use this almost every time for initialization. Its like constructor body if you are familiar with C# constructors. But if you debug game, and recompile scripts in play mode definitely read this.
The next group of events are frame events. This means that each of these events will be called one or more times in one frame.
One of the main events is OnUpdate(float delta)
. It takes one input parameter - delta time or delta time step, which means how many milliseconds have passed since the previous frame. It must be used when you move objects in the scene so that they mix with the time of your game and with the current frame rate. At the moment, this is one of the main events for gameplay. Right after OnUpdate(), OnPostUpdate()
will executed. It has no special use yet and the only difference is that it goes without a delta. The only use of it so far is for special cases, the behavior and control of the camera.
Next, events for custom rendering will be called, these are OnPostRender()
, OnPreRender()
, OnRender()
and all methods from RenderEngine2D can be called there.
They are used so that you can draw ordinary primitives that do not have the property of normal objects and are not part of Entity Component System, just graphical primitives through the Force renderer and control of scene rendering commands.
OnPreRender()
is used to draw primitives before the main rendering of the current scene. That is, if these are 2D primitives, they will always be behind the main objects and if 3D, then there will be no difference.
OnRender()
is allowed to draw primitives into the current rendering context of the main scene. OnPostRender()
allows you to create your own rendering passes and draw primitives there, as well as the order of these passes will play a role. In any case, all 2D primitives drawn here will be displayed first if, for 3D primitives , their Z axis (depth) will play a role.
Another event that will be called is OnRenderGui()
which is an ideal place to draw your own panels using EditorPanel.
When the scene stops playing, Force will also trigger several events to notify the user that the script has been destroyed, the object or script has become disabled.
OnDisable()
will be called first if and only if the object to which the script was attached has become disabled for the scene. Immediately after this, Force will call OnDisabled()
, which will do the same as for the script-only object. If the script is in progress became unenforceable.
And the most recent event will be called OnDestroy()
, before Force destroys the script instance on itself and deletes all created instances of objects of the current scene and user objects.
When scripts rebuilds in play mode, all object that was created from OnStart()
method became invalid and will point to old assembly, because OnStart()
call once per game play. In other hand we have OnValidate()
method, that handle this problem, because this method call every time when scene starts to play AND when script was reloaded in play mode.
So assign objects in OnValidate()
updates the objects to new script assembly and keep your game running without manually replay it or reassign the instances of objects.
For example we have object Player that we find and assign it instance it OnStart()
:
In this code we simply assign our variable to GameObject Player. And this variable can be safely using everywhere in this scripts, until it not becomes null. But when script will rebuild at play mode and we try to use its instance you just cannot, because this object points to old script assembly. Here we have two ways to fix this.
First is simply reassign it instance by calling Find("Player")
again in OnUpdate()
with some condition. And second its use OnValidate
to assign all GameObjects from there. Thats is main difference between OnStart
and OnValidate
.
So rewrite our example code like this:
And this code will works perfectly in our case.
Remember its just one way to use OnValidate()
that not means that you need completely ignore OnStart()
at all, and use first one every time. Its depends on you game and yours gameplay mechanics.