1 /** 2 Access to engine properties. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.engine; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.mainloop; 24 /** 25 Access to engine properties. 26 27 The $(D Engine) singleton allows you to query and modify the project's run-time parameters, such as frames per second, time scale, and others. 28 */ 29 @GodotBaseClass struct EngineSingleton 30 { 31 package(godot) enum string _GODOT_internal_name = "_Engine"; 32 public: 33 @nogc nothrow: 34 union { /** */ godot_object _godot_object; /** */ GodotObject _GODOT_base; } 35 alias _GODOT_base this; 36 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 37 package(godot) __gshared bool _classBindingInitialized = false; 38 package(godot) static struct GDNativeClassBinding 39 { 40 __gshared: 41 godot_object _singleton; 42 immutable char* _singletonName = "Engine"; 43 @GodotName("get_author_info") GodotMethod!(Dictionary) getAuthorInfo; 44 @GodotName("get_copyright_info") GodotMethod!(Array) getCopyrightInfo; 45 @GodotName("get_donor_info") GodotMethod!(Dictionary) getDonorInfo; 46 @GodotName("get_frames_drawn") GodotMethod!(long) getFramesDrawn; 47 @GodotName("get_frames_per_second") GodotMethod!(double) getFramesPerSecond; 48 @GodotName("get_idle_frames") GodotMethod!(long) getIdleFrames; 49 @GodotName("get_iterations_per_second") GodotMethod!(long) getIterationsPerSecond; 50 @GodotName("get_license_info") GodotMethod!(Dictionary) getLicenseInfo; 51 @GodotName("get_license_text") GodotMethod!(String) getLicenseText; 52 @GodotName("get_main_loop") GodotMethod!(MainLoop) getMainLoop; 53 @GodotName("get_physics_frames") GodotMethod!(long) getPhysicsFrames; 54 @GodotName("get_physics_interpolation_fraction") GodotMethod!(double) getPhysicsInterpolationFraction; 55 @GodotName("get_physics_jitter_fix") GodotMethod!(double) getPhysicsJitterFix; 56 @GodotName("get_singleton") GodotMethod!(GodotObject, String) getSingleton; 57 @GodotName("get_target_fps") GodotMethod!(long) getTargetFps; 58 @GodotName("get_time_scale") GodotMethod!(double) getTimeScale; 59 @GodotName("get_version_info") GodotMethod!(Dictionary) getVersionInfo; 60 @GodotName("has_singleton") GodotMethod!(bool, String) hasSingleton; 61 @GodotName("is_editor_hint") GodotMethod!(bool) isEditorHint; 62 @GodotName("is_in_physics_frame") GodotMethod!(bool) isInPhysicsFrame; 63 @GodotName("set_editor_hint") GodotMethod!(void, bool) setEditorHint; 64 @GodotName("set_iterations_per_second") GodotMethod!(void, long) setIterationsPerSecond; 65 @GodotName("set_physics_jitter_fix") GodotMethod!(void, double) setPhysicsJitterFix; 66 @GodotName("set_target_fps") GodotMethod!(void, long) setTargetFps; 67 @GodotName("set_time_scale") GodotMethod!(void, double) setTimeScale; 68 } 69 /// 70 pragma(inline, true) bool opEquals(in EngineSingleton other) const 71 { return _godot_object.ptr is other._godot_object.ptr; } 72 /// 73 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 74 { _godot_object.ptr = n; return null; } 75 /// 76 pragma(inline, true) bool opEquals(typeof(null) n) const 77 { return _godot_object.ptr is n; } 78 /// 79 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 80 mixin baseCasts; 81 /// Construct a new instance of EngineSingleton. 82 /// Note: use `memnew!EngineSingleton` instead. 83 static EngineSingleton _new() 84 { 85 static godot_class_constructor constructor; 86 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Engine"); 87 if(constructor is null) return typeof(this).init; 88 return cast(EngineSingleton)(constructor()); 89 } 90 @disable new(size_t s); 91 /** 92 Returns engine author information in a Dictionary. 93 `lead_developers` - Array of Strings, lead developer names 94 `founders` - Array of Strings, founder names 95 `project_managers` - Array of Strings, project manager names 96 `developers` - Array of Strings, developer names 97 */ 98 Dictionary getAuthorInfo() const 99 { 100 checkClassBinding!(typeof(this))(); 101 return ptrcall!(Dictionary)(GDNativeClassBinding.getAuthorInfo, _godot_object); 102 } 103 /** 104 Returns an Array of copyright information Dictionaries. 105 `name` - String, component name 106 `parts` - Array of Dictionaries {`files`, `copyright`, `license`} describing subsections of the component 107 */ 108 Array getCopyrightInfo() const 109 { 110 checkClassBinding!(typeof(this))(); 111 return ptrcall!(Array)(GDNativeClassBinding.getCopyrightInfo, _godot_object); 112 } 113 /** 114 Returns a Dictionary of Arrays of donor names. 115 {`platinum_sponsors`, `gold_sponsors`, `silver_sponsors`, `bronze_sponsors`, `mini_sponsors`, `gold_donors`, `silver_donors`, `bronze_donors`} 116 */ 117 Dictionary getDonorInfo() const 118 { 119 checkClassBinding!(typeof(this))(); 120 return ptrcall!(Dictionary)(GDNativeClassBinding.getDonorInfo, _godot_object); 121 } 122 /** 123 Returns the total number of frames drawn. If the render loop is disabled with `--disable-render-loop` via command line, this returns `0`. See also $(D getIdleFrames). 124 */ 125 long getFramesDrawn() 126 { 127 checkClassBinding!(typeof(this))(); 128 return ptrcall!(long)(GDNativeClassBinding.getFramesDrawn, _godot_object); 129 } 130 /** 131 Returns the frames per second of the running game. 132 */ 133 double getFramesPerSecond() const 134 { 135 checkClassBinding!(typeof(this))(); 136 return ptrcall!(double)(GDNativeClassBinding.getFramesPerSecond, _godot_object); 137 } 138 /** 139 Returns the total number of frames passed since engine initialization which is advanced on each $(B idle frame), regardless of whether the render loop is enabled. See also $(D getFramesDrawn). 140 */ 141 long getIdleFrames() const 142 { 143 checkClassBinding!(typeof(this))(); 144 return ptrcall!(long)(GDNativeClassBinding.getIdleFrames, _godot_object); 145 } 146 /** 147 148 */ 149 long getIterationsPerSecond() const 150 { 151 checkClassBinding!(typeof(this))(); 152 return ptrcall!(long)(GDNativeClassBinding.getIterationsPerSecond, _godot_object); 153 } 154 /** 155 Returns Dictionary of licenses used by Godot and included third party components. 156 */ 157 Dictionary getLicenseInfo() const 158 { 159 checkClassBinding!(typeof(this))(); 160 return ptrcall!(Dictionary)(GDNativeClassBinding.getLicenseInfo, _godot_object); 161 } 162 /** 163 Returns Godot license text. 164 */ 165 String getLicenseText() const 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(String)(GDNativeClassBinding.getLicenseText, _godot_object); 169 } 170 /** 171 Returns the main loop object (see $(D MainLoop) and $(D SceneTree)). 172 */ 173 MainLoop getMainLoop() const 174 { 175 checkClassBinding!(typeof(this))(); 176 return ptrcall!(MainLoop)(GDNativeClassBinding.getMainLoop, _godot_object); 177 } 178 /** 179 Returns the total number of frames passed since engine initialization which is advanced on each $(B physics frame). 180 */ 181 long getPhysicsFrames() const 182 { 183 checkClassBinding!(typeof(this))(); 184 return ptrcall!(long)(GDNativeClassBinding.getPhysicsFrames, _godot_object); 185 } 186 /** 187 Returns the fraction through the current physics tick we are at the time of rendering the frame. This can be used to implement fixed timestep interpolation. 188 */ 189 double getPhysicsInterpolationFraction() const 190 { 191 checkClassBinding!(typeof(this))(); 192 return ptrcall!(double)(GDNativeClassBinding.getPhysicsInterpolationFraction, _godot_object); 193 } 194 /** 195 196 */ 197 double getPhysicsJitterFix() const 198 { 199 checkClassBinding!(typeof(this))(); 200 return ptrcall!(double)(GDNativeClassBinding.getPhysicsJitterFix, _godot_object); 201 } 202 /** 203 Returns a global singleton with given `name`. Often used for plugins, e.g. `GodotPayment` on Android. 204 */ 205 GodotObject getSingleton(in String name) const 206 { 207 checkClassBinding!(typeof(this))(); 208 return ptrcall!(GodotObject)(GDNativeClassBinding.getSingleton, _godot_object, name); 209 } 210 /** 211 212 */ 213 long getTargetFps() const 214 { 215 checkClassBinding!(typeof(this))(); 216 return ptrcall!(long)(GDNativeClassBinding.getTargetFps, _godot_object); 217 } 218 /** 219 220 */ 221 double getTimeScale() 222 { 223 checkClassBinding!(typeof(this))(); 224 return ptrcall!(double)(GDNativeClassBinding.getTimeScale, _godot_object); 225 } 226 /** 227 Returns the current engine version information in a Dictionary. 228 `major` - Holds the major version number as an int 229 `minor` - Holds the minor version number as an int 230 `patch` - Holds the patch version number as an int 231 `hex` - Holds the full version number encoded as a hexadecimal int with one byte (2 places) per number (see example below) 232 `status` - Holds the status (e.g. "beta", "rc1", "rc2", ... "stable") as a String 233 `build` - Holds the build name (e.g. "custom_build") as a String 234 `hash` - Holds the full Git commit hash as a String 235 `year` - Holds the year the version was released in as an int 236 `string` - `major` + `minor` + `patch` + `status` + `build` in a single String 237 The `hex` value is encoded as follows, from left to right: one byte for the major, one byte for the minor, one byte for the patch version. For example, "3.1.12" would be `0x03010C`. $(B Note:) It's still an int internally, and printing it will give you its decimal representation, which is not particularly meaningful. Use hexadecimal literals for easy version comparisons from code: 238 239 240 if Engine.get_version_info().hex >= 0x030200: 241 # Do things specific to version 3.2 or later 242 else: 243 # Do things specific to versions before 3.2 244 245 246 */ 247 Dictionary getVersionInfo() const 248 { 249 checkClassBinding!(typeof(this))(); 250 return ptrcall!(Dictionary)(GDNativeClassBinding.getVersionInfo, _godot_object); 251 } 252 /** 253 Returns `true` if a singleton with given `name` exists in global scope. 254 */ 255 bool hasSingleton(in String name) const 256 { 257 checkClassBinding!(typeof(this))(); 258 return ptrcall!(bool)(GDNativeClassBinding.hasSingleton, _godot_object, name); 259 } 260 /** 261 262 */ 263 bool isEditorHint() const 264 { 265 checkClassBinding!(typeof(this))(); 266 return ptrcall!(bool)(GDNativeClassBinding.isEditorHint, _godot_object); 267 } 268 /** 269 Returns `true` if the game is inside the fixed process and physics phase of the game loop. 270 */ 271 bool isInPhysicsFrame() const 272 { 273 checkClassBinding!(typeof(this))(); 274 return ptrcall!(bool)(GDNativeClassBinding.isInPhysicsFrame, _godot_object); 275 } 276 /** 277 278 */ 279 void setEditorHint(in bool enabled) 280 { 281 checkClassBinding!(typeof(this))(); 282 ptrcall!(void)(GDNativeClassBinding.setEditorHint, _godot_object, enabled); 283 } 284 /** 285 286 */ 287 void setIterationsPerSecond(in long iterations_per_second) 288 { 289 checkClassBinding!(typeof(this))(); 290 ptrcall!(void)(GDNativeClassBinding.setIterationsPerSecond, _godot_object, iterations_per_second); 291 } 292 /** 293 294 */ 295 void setPhysicsJitterFix(in double physics_jitter_fix) 296 { 297 checkClassBinding!(typeof(this))(); 298 ptrcall!(void)(GDNativeClassBinding.setPhysicsJitterFix, _godot_object, physics_jitter_fix); 299 } 300 /** 301 302 */ 303 void setTargetFps(in long target_fps) 304 { 305 checkClassBinding!(typeof(this))(); 306 ptrcall!(void)(GDNativeClassBinding.setTargetFps, _godot_object, target_fps); 307 } 308 /** 309 310 */ 311 void setTimeScale(in double time_scale) 312 { 313 checkClassBinding!(typeof(this))(); 314 ptrcall!(void)(GDNativeClassBinding.setTimeScale, _godot_object, time_scale); 315 } 316 /** 317 If `true`, the script is currently running inside the editor. This is useful for `tool` scripts to conditionally draw editor helpers, or prevent accidentally running "game" code that would affect the scene state while in the editor: 318 319 320 if Engine.editor_hint: 321 draw_gizmos() 322 else: 323 simulate_physics() 324 325 326 See $(D url=https://docs.godotengine.org/en/3.3/tutorials/misc/running_code_in_the_editor.html)Running code in the editor$(D /url) in the documentation for more information. 327 $(B Note:) To detect whether the script is run from an editor $(I build) (e.g. when pressing `F5`), use $(D OS.hasFeature) with the `"editor"` argument instead. `OS.has_feature("editor")` will evaluate to `true` both when the code is running in the editor and when running the project from the editor, but it will evaluate to `false` when the code is run from an exported project. 328 */ 329 @property bool editorHint() 330 { 331 return isEditorHint(); 332 } 333 /// ditto 334 @property void editorHint(bool v) 335 { 336 setEditorHint(v); 337 } 338 /** 339 The number of fixed iterations per second. This controls how often physics simulation and $(D Node._physicsProcess) methods are run. This value should generally always be set to `60` or above, as Godot doesn't interpolate the physics step. As a result, values lower than `60` will look stuttery. This value can be increased to make input more reactive or work around tunneling issues, but keep in mind doing so will increase CPU usage. 340 */ 341 @property long iterationsPerSecond() 342 { 343 return getIterationsPerSecond(); 344 } 345 /// ditto 346 @property void iterationsPerSecond(long v) 347 { 348 setIterationsPerSecond(v); 349 } 350 /** 351 Controls how much physics ticks are synchronized with real time. For 0 or less, the ticks are synchronized. Such values are recommended for network games, where clock synchronization matters. Higher values cause higher deviation of the in-game clock and real clock but smooth out framerate jitters. The default value of 0.5 should be fine for most; values above 2 could cause the game to react to dropped frames with a noticeable delay and are not recommended. 352 $(B Note:) For best results, when using a custom physics interpolation solution, the physics jitter fix should be disabled by setting $(D physicsJitterFix) to `0`. 353 */ 354 @property double physicsJitterFix() 355 { 356 return getPhysicsJitterFix(); 357 } 358 /// ditto 359 @property void physicsJitterFix(double v) 360 { 361 setPhysicsJitterFix(v); 362 } 363 /** 364 The desired frames per second. If the hardware cannot keep up, this setting may not be respected. A value of 0 means no limit. 365 */ 366 @property long targetFps() 367 { 368 return getTargetFps(); 369 } 370 /// ditto 371 @property void targetFps(long v) 372 { 373 setTargetFps(v); 374 } 375 /** 376 Controls how fast or slow the in-game clock ticks versus the real life one. It defaults to 1.0. A value of 2.0 means the game moves twice as fast as real life, whilst a value of 0.5 means the game moves at half the regular speed. 377 */ 378 @property double timeScale() 379 { 380 return getTimeScale(); 381 } 382 /// ditto 383 @property void timeScale(double v) 384 { 385 setTimeScale(v); 386 } 387 } 388 /// Returns: the EngineSingleton 389 @property @nogc nothrow pragma(inline, true) 390 EngineSingleton Engine() 391 { 392 checkClassBinding!EngineSingleton(); 393 return EngineSingleton(EngineSingleton.GDNativeClassBinding._singleton); 394 }