system
February 1, 2023, 4:46pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
SloshySpace
I need to randomize the location of the enemies in my game and I have it set so it would depend on a random number, but Godot keeps giving decimals which don’t work with my code. Heres a part of my code:
`if my_random_number ==1:
$RandoSpriteSheet.position.x = 577
$RandoSpriteSheet.position.y = 300
$Friend.position.x = -305
$Friend.position.y = -14`
I need to make it so it is only whole number values being outputted.
Thanks
system
February 1, 2023, 4:53pm
2
Reply From:
jgodfrey
Here’s a set of random number utility functions I tend to keep handy…
onready var rng = RandomNumberGenerator.new()
func _ready():
rng.randomize()
func get_random_int_between(min_val, max_val):
return rng.randi_range(min_val, max_val)
func get_random_float_between(min_val, max_val):
return rng.randf_range(min_val, max_val)
func get_random_bool():
return rng.randi() % 2 == 1
# Return 1 or -1
func get_random_direction():
return (randi() & 2) - 1
So, specifically, you could call the above get_random_int_between()
for the case you mention. Or, just use its internals as necessary…
system
February 2, 2023, 5:07pm
3
Reply From:
CassanovaWong
you can convert floats with int() and/or convert negatives with abs() or do both with abs(int())