Putting Utilities in a Global class

Godot Version

4.2.1

Question

I find myself generating random numbers all over my code. It’s super easy to just create a RandomNumberGenerator instance whenever I need one. This seems wasteful, yet the Godot devs seemingly designed it this way on purpose? Should I just have one RNG object on my global class? Or do those instances use very little memory and am I overthinking this? Thanks for any advice!

This should cover when to use which:

2 Likes

You don’t need to create a RandomNumberGenerator every time, just use the global scope functions randf_range, randf, randi_range and randi_range. Or create an autoload and access your RandomNumberGenerator from there.

Or do those instances use very little memory and am I overthinking this? Thanks for any advice!

Unless you’re creating hundreds of that, you’re overthinking.

1 Like

Man why did I not know global scope had those. I probably approach ~100 as a level gets towards the end, so, great to know. Thank you!