-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSession.cs
More file actions
120 lines (103 loc) · 5.36 KB
/
Session.cs
File metadata and controls
120 lines (103 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using Sandbox.Definitions;
using Sandbox.Game;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
using VRage.Game;
using VRage.Game.Components;
using VRage.Utils;
using BlendTypeEnum = VRageRender.MyBillboard.BlendTypeEnum; // required for MyTransparentGeometry/MySimpleObjectDraw to be able to set blend type.
namespace Digi.Examples
{
// This object is always present, from the world load to world unload.
// NOTE: all clients and server run mod scripts, keep that in mind.
// NOTE: this and gamelogic comp's update methods run on the main game thread, don't do too much in a tick or you'll lower sim speed.
// NOTE: also mind allocations, avoid realtime allocations, re-use collections/ref-objects (except value types like structs, integers, etc).
//
// The MyUpdateOrder arg determines what update overrides are actually called.
// Remove any method that you don't need, none of them are required, they're only there to show what you can use.
// Also remove all comments you've read to avoid the overload of comments that is this file.
[MySessionComponentDescriptor(MyUpdateOrder.BeforeSimulation | MyUpdateOrder.AfterSimulation)]
public class Example_Session : MySessionComponentBase
{
public static Example_Session Instance; // the only way to access session comp from other classes and the only accepted static field.
public override void LoadData()
{
// amogst the earliest execution points, but not everything is available at this point.
// These can be used anywhere, not just in this method/class:
// MyAPIGateway. - main entry point for the API
// MyDefinitionManager.Static. - reading/editing definitions
// MyGamePruningStructure. - fast way of finding entities in an area
// MyTransparentGeometry. and MySimpleObjectDraw. - to draw sprites (from TransparentMaterials.sbc) in world (they usually live a single tick)
// MyVisualScriptLogicProvider. - mainly designed for VST but has its uses, use as a last resort.
// System.Diagnostics.Stopwatch - for measuring code execution time.
// ...and many more things, ask in #programming-modding in keen's discord for what you want to do to be pointed at the available things to use.
Instance = this;
}
public override void BeforeStart()
{
// executed before the world starts updating
}
protected override void UnloadData()
{
// always catch errors here because throwing them will NOT crash the game and instead prevent other mods from unloading properly, causing all sorts of hidden issues...
try
{
// executed when world is exited to unregister events and stuff.
}
catch(Exception e)
{
MyLog.Default.Error(e.ToString());
}
finally
{
Instance = null; // important for avoiding this instance and all its references to remain allocated in memory
}
}
public override void HandleInput()
{
// gets called 60 times a second before all other update methods, regardless of framerate, game pause or MyUpdateOrder.
}
public override void UpdateBeforeSimulation()
{
// executed every tick, 60 times a second, before physics simulation and only if game is not paused.
}
public override void Simulate()
{
// executed every tick, 60 times a second, during physics simulation and only if game is not paused.
// NOTE in this example this won't actually be called because of the lack of MyUpdateOrder.Simulation argument in MySessionComponentDescriptor
}
public override void UpdateAfterSimulation()
{
// executed every tick, 60 times a second, after physics simulation and only if game is not paused.
try // example try-catch for catching errors and notifying player, use only for non-critical code!
{
// ...
}
catch(Exception e) // NOTE: never use try-catch for code flow or to ignore errors! catching has a noticeable performance impact.
{
MyLog.Default.WriteLineAndConsole(e.ToString());
if(MyAPIGateway.Session?.Player != null)
MyAPIGateway.Utilities.ShowNotification($"[ ERROR: {GetType().FullName}: {e.Message} | Send SpaceEngineers.Log to mod author ]", 10000, MyFontEnum.Red);
}
}
public override void Draw()
{
// gets called 60 times a second after all other update methods, regardless of framerate, game pause or MyUpdateOrder.
// NOTE: this is the only place where the camera matrix (MyAPIGateway.Session.Camera.WorldMatrix) is accurate, everywhere else it's 1 frame behind.
}
public override void SaveData()
{
// executed AFTER world was saved
}
public override MyObjectBuilder_SessionComponent GetObjectBuilder()
{
// executed during world save, most likely before entities.
return base.GetObjectBuilder(); // leave as-is.
}
public override void UpdatingStopped()
{
// executed when game is paused
}
}
}