admin管理员组

文章数量:1384254

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

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

Share Improve this question edited Mar 18 at 17:11 0stone0 asked Mar 18 at 17:04 0stone00stone0 44.5k5 gold badges52 silver badges80 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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()) ])
  #                                                  ^^^^^^^^^^^^

本文标签: godotrandfrange called with callv always returns upper limitStack Overflow