-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from goostengine/rng-singleton
Add `Random` singleton
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "random.h" | ||
|
||
Random *Random::singleton = nullptr; | ||
|
||
void Random::_bind_methods() { | ||
ClassDB::bind_method(D_METHOD("new_instance"), &Random::new_instance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef GOOST_RANDOM_H | ||
#define GOOST_RANDOM_H | ||
|
||
#include "core/math/random_number_generator.h" | ||
|
||
class Random : public RandomNumberGenerator { | ||
GDCLASS(Random, RandomNumberGenerator); | ||
|
||
private: | ||
static Random *singleton; | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
static Random* get_singleton() { return singleton; } | ||
|
||
Ref<Random> new_instance() const { return memnew(Random); } | ||
|
||
Random() { | ||
if (!singleton) { | ||
singleton = this; | ||
} | ||
} | ||
}; | ||
|
||
#endif // GOOST_RANDOM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="Random" inherits="RandomNumberGenerator" version="3.2"> | ||
<brief_description> | ||
An instance of [RandomNumberGenerator] available at [@GlobalScope]. | ||
</brief_description> | ||
<description> | ||
This is a singleton which allows to use [RandomNumberGenerator] methods without instantiating a dedicated object. This means that [Random] can be used via script with methods such as [method @GDScript.randi]: | ||
[codeblock] | ||
Random.randomize() # Time-based. | ||
Random.seed = hash("Goost") # Manual. | ||
var i = Random.randi() % 100 | ||
var f = Random.randf_range(-1.0, 1.0) | ||
[/codeblock] | ||
The class may implement other methods other than what [RandomNumberGenerator] already provides out of the box. | ||
It's not possible to instantiate a new [Random] instance with [code]Random.new()[/code] in GDScript. If you'd like to instantiate a local instance of [Random], use [method new_instance] instead, or [code]ClassDB.instance("Random")[/code], see [method ClassDB.instance]. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="new_instance" qualifiers="const"> | ||
<return type="Random"> | ||
</return> | ||
<description> | ||
Instantiates a new local [Random] instance based on [RandomNumberGenerator]. Does not override the [Random] instance accessible at [@GlobalScope]. | ||
</description> | ||
</method> | ||
</methods> | ||
<constants> | ||
</constants> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
extends "res://addons/gut/test.gd" | ||
|
||
|
||
func test_create_local_instance(): | ||
var rng = Random.new_instance() | ||
Random.seed = hash("Goost") | ||
rng.seed = 37 | ||
assert_ne(rng, Random, "The new local instance should not override the global one") | ||
assert_ne(rng.seed, Random.seed) | ||
assert_lt(rng.randf(), 1.0) | ||
|
||
|
||
func test_singleton(): | ||
Random.randomize() | ||
Random.seed = 37 | ||
for x in 100: | ||
var f = Random.randf() | ||
assert_lt(f, 1.0) | ||
assert_gt(f, 0.0) | ||
for x in 100: | ||
var i = Random.randi() % 100 | ||
assert_lt(i, 99) | ||
assert_gt(i, 0) |