Zshandi / SuperSpherebox65

This is a preliminary repository for my groups GGJ 2024 entry.
0 stars 0 forks source link

Add custom rng class, with instance and static methods #38

Closed Zshandi closed 5 months ago

Zshandi commented 5 months ago

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() becomes Random.i() randi_range() becomes Random.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 the Array.pick_random() method. The color utility methods random_color and random_color_a can be moved from Main and renamed to just color and color_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()

Zshandi commented 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.