godotgdscriptgodot4

randf_range called with callv always returns upper limit


Godot - GDscript provides us with a callv method:

Calls the method represented by this Callable. Unlike call(), this method expects all arguments to be contained inside the arguments Array.


I'm trying to use implement some CONST's for my randf_range call:

So instead off

rng.randf_range(0.1, 0.3)

I'm using:

const RANGE = [ .1, .3 ]    
rng.randf_range.callv(RANGE)

However, this always results the TO value as you can see in this mre:

const RANGE = [ .1, .3 ]    
    
var rng = RandomNumberGenerator.new()
rng.seed = hash("HuH")

for i in range(3):
    print('%d -> %f' % [ i, rng.randf_range.callv(RANGE) ])
    
rng = RandomNumberGenerator.new()
rng.seed = hash("HuH")

for i in range(3):
    print('%d -> %f' % [ i, rng.randf_range(RANGE[0], RANGE[1]) ])

Outputs:

0 -> 0.300000
1 -> 0.300000
2 -> 0.300000
0 -> 0.214059
1 -> 0.191159
2 -> 0.203312

Why is that?


GDScript Playground


Solution

  • This is open Issue 93600:

    Callable.callv() will make call with wrong arguments if a const Array is passed

    The workaround is to make the array mutable or duplicate() it when you use it. callv will not make changes to it either way:

      var RANGE = [ .1, .3 ]
    # ^^^
    

    or

      const RANGE = [ .1, .3 ]
    
      # ...
    
      print('%d -> %f' % [ i, rng.randf_range.callv(RANGE.duplicate()) ])
      #                                                  ^^^^^^^^^^^^