Closed Zshandi closed 5 months ago
Due to a limitation of gdscript (static & non-static members can't have the same name), I had to add the static functions in a separate class. This static class is named Rand
for succinctness. So it will actually be the same number of characters as the built-in global scope random functions. For example, instead of rand_i()
, you would call Rand.i()
. The Rand
class has all the same functions and properties as the Random
class, except that everything is static and references the corresponding functions in a static instance
variable.
This is to address several shortcomings of the built-in rng system which Godot uses. First, the rngs in Godot cannot be depended upon to have consistent repeatable results between versions, as they may change the underlying algorithm. Second, the global rng does not allow saving / loading of state, though this is more for convenience.
To start with, this will just be a wrapper for the built-in Godot rng. Though this will not address the first issue listed above, it will make it easy for us to swap in our own implementation later. It could be called just "Random" for succinctness, and to distinguish from the Godot rng class.
For succinctness, we can drop the "rand" prepended to each of the methods, but otherwise provide the same methods as the Godot rng. These methods will be available as instance calls, or statically for a global rng.
Examples:
randi()
becomesRandom.i()
randi_range()
becomesRandom.i_range()
The seed, state, and randomize() properties and method are essentially identical to the built-in rng, and have static versions.
There should also be additional methods to replace methods that use the global rng, including the utility methods we have written. For example, a
Random.pick()
method to replace theArray.pick_random()
method. The color utility methodsrandom_color
andrandom_color_a
can be moved fromMain
and renamed to justcolor
andcolor_a
.So, here is the final list of the minimum functions and properties it will have, which will each have an instance member for specific rng, and a static member for global scope equivalent:
seed
,state
,randomize()
,i()
,i_range(from,to)
,f()
,f_range(from,to)
,pick(Array)
,color()
,color_a()
Random
class (empty template)RandomNumberGenerator
class (for internal use only)seed
,state
,randomize()
RandomNumberGenerator
functions likei()
,i_range(from,to)
,f()
,f_range(from,to)
pick(Array)
,color()
,color_a()
(some moved from theMain
class)Random
, along with static versions of each of the methods that call them on the static instancerandomize()
called on it during initialization