Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Random singleton #29

Merged
merged 1 commit into from
Oct 27, 2020
Merged

Add Random singleton #29

merged 1 commit into from
Oct 27, 2020

Conversation

Xrayez
Copy link
Contributor

@Xrayez Xrayez commented Oct 27, 2020

Closes godotengine/godot-proposals#1741.

Provides a global instance of RandomNumberGenerator available via scripting at global scope, as a complementary for the lack of additional RNG methods in GDScript functions.

Random.randomize()
Random.seed = hash("Goost")
var i = Random.randi() % 100
var f = Random.randf_range(-1.0, 1.0)

is equivalent to:

randomize()
seed(hash("Goost"))
var i = randi() % 100
var f = rand_range(-1.0, 1.0)

Exposing this class as a global instance allows to use methods such as randfn(), randi_range() available in RandomNumberGenerator, which are not available in GDScript.

A new local instance has to be created with:

var rng = Random.new_instance()

You can still use RandomNumberGenerator, but a new set of randomization methods may/should be implemented in Random class as well, such as godotengine/godot#43103, but before this can happen, godotengine/godot#43113 has to be backported to Godot 3.2.4.

@Xrayez Xrayez added the feature 💡 New feature proposal label Oct 27, 2020
@Xrayez Xrayez merged commit 9822a07 into gd3 Oct 27, 2020
@Xrayez Xrayez deleted the rng-singleton branch October 27, 2020 22:38
Provides a global instance of `RandomNumberGenerator` available via
scripting at global scope, as a complementary for the lack of additional
RNG methods in GDScript functions, such as `randfn()`.

A new set of randomization methods may/should be implemented in this
class as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 💡 New feature proposal topic:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instantiate RandomNumberGenerator at global scope as Random singleton, or the functionality behind it
1 participant