Godot-docs: API difference between C# and GDSCript for Random and other traps

Created on 14 Feb 2018  Â·  15Comments  Â·  Source: godotengine/godot-docs

None of the random function (Random.Randf, etc) used in GDSCript can be used in C# contrary to what the docs say. (Godot 3.0 stable, doc stable or latest)

In the docs:
http://docs.godotengine.org/en/latest/getting_started/scripting/c_sharp/c_sharp_differences.html#random

Random functions like rand_range and rand_seed are located under Random, so use Random.RandRange instead of rand_range.

That's a lie because Random.Something resolves to System.Random.Something
MS docs for System.Random:
https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx
The good news is that System.Random is really good but that is tripping people, for example yesterday on the csharp Discord channel:

[17:13] marcules: I can'te get float r = Random.Range(0, 4); to work

More generally a real C# API documentation is needed. Not everybody uses an IDE with intellisense/omnisharp or equivalent, especially when testing Godot, and there are a lot of small traps everywhere. For example ButtonList.BUTTON_Left in GDScript is ButtonList.Left in C# and is not documented there:
http://docs.godotengine.org/en/latest/getting_started/scripting/c_sharp/c_sharp_differences.html#doc-c-sharp-differences

An automatic C# API export even without comments or explanation would be very useful. Maybe then can Random be found again?

Example project:
DocsLie[1].zip
Check the compiling error, and if you have a good IDE, check what does Random refers to.

Most helpful comment

By the way, if you need a shortcut for such a method, you might want to convert it to an extension method like this:
```c#
public static float FloatRange(this Random random, float min = 0.0f, float max = 1.0f) {
return (float) (random.NextDouble() * (max - min) + min);
}

Then you can just invoke it like a normal method:
```c#
var random = new Random(seed);
var value = random.FloatRange(0f, 5f);

All 15 comments

Two more victims on the discord server:

[02:21] jonbonazza: According to this, I should use Random.RandRange, but I can't find this anywhere
[04:39] Hippy: @jonbonazza I couldn't get it to work either. I had to use the overload of the 'Next' method.

That's a lie because Random.Something resolves to System.Random.Something

Have you considered that it might be a mistake and not a lie?..

The author made a mistake or got overtaken by the engine's development, the consequence is that the documents promise me something that does not exist. (or more honestly, I put a bit of click-bait).

I will make a pull request once I have understood how does Github works and checked that there is not already another pull request that solves this issue.

Has anyone figured out the best alternative to GD script's random functions? I'm so close to finishing this tutorial lol and everything seems the biggest issues that I've so far overcome is the common bugs w/ export/signal annotations (using new signal annot. from master that got merged) not showing up until restarting/rebuilding editor. Few missing api's like Random after that you can pretty much follow everything in the tutorial.

I use System.Random happily.
https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

Oh, Godot's random should be exposed to C# as well.

@Duehok i also use System.Random for most things, but doing things like a random float between an arbitrary range of numbers isn't trivial using System.Random. Requires doing some nasty bit twiddling to make it work. Godot's RandomRange was much nicer for this use case.

@jonbonazza Does rand_range function provide anything different than doing Random.NextDouble() * (maxValue – minValue) + minValue in C#?

@mysticfall Godot's rand functions use the seed you setup with seed(int) or randomize().

Doesn't System.Random take an optional seed from its constructor? I'm sorry if I'm asking the obvious, but I haven't used GDScript, so I was wondering why we'd need a separate random API in C#.

@mysticfall Yes, it has a parameter for the seed. I'm not sure whether it works the same way as Godot's does though.
Some people may be using C# together with other scripting languages or with custom Godot modules. Setting the seed twice would annoy them, so it's better to give them that choice :)

@neikeq Thanks for the explanation :)

@mysticfall yeah that's actually the way I did it to get it working for the tutorial

private float rand_rand(float min, float max) 
{
    return (float) (rand.NextDouble() * (max - min) + min);
}

By the way, if you need a shortcut for such a method, you might want to convert it to an extension method like this:
```c#
public static float FloatRange(this Random random, float min = 0.0f, float max = 1.0f) {
return (float) (random.NextDouble() * (max - min) + min);
}

Then you can just invoke it like a normal method:
```c#
var random = new Random(seed);
var value = random.FloatRange(0f, 5f);

I'm closing this. As explained in the docs, the equivalent to the global scope random functions can be found in the Godot.GD class. There's also the RandomNumberGenerator class now.

Don't use System.Random if you are looking for the same result as the Godot's rng.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

youreperfect picture youreperfect  Â·  3Comments

stsewd picture stsewd  Â·  3Comments

jcmonkey picture jcmonkey  Â·  4Comments

puthre picture puthre  Â·  4Comments

touilleMan picture touilleMan  Â·  4Comments