最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

godot - randf_range called with callv always returns upper limit - Stack Overflow

programmeradmin3浏览0评论

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()) ])
  #                                                  ^^^^^^^^^^^^
发布评论

评论列表(0)

  1. 暂无评论