How to make animations play randomly on player enter area

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ogeeice

I have a player of type kinematicbody3d and a room of starticbody3d with an area3d attached to the room node,I want random animations which can be either glass_fall or tv_switch_on to play upon the player entering the room.Does Godot have an easy way of doing this.

Maybe you can use the Area3D node to detect when the player enters the room. Then set a signal to fire off an animation randomly, e.g.

func _on_area3d_enter():
     if randf() > 0.5:

You can fine tune the randf() > some_number part by using different ranges between 0.0 and 1.0.

Ertain | 2019-07-03 23:24

:bust_in_silhouette: Reply From: Dlean Jeans

This plays a random animation from any AnimationPlayer:

func play_random_animation(animation_player:AnimationPlayer):
	var animation_list = animation_player.get_animation_list()
	var random_animation = animation_list[randi() % animation_list.size()]

If it’s functions:

func call_random_function(functions:Array, args:Array = []):
	var random_index = randi() % functions.size()
	var random_func = functions[random_index]
	if not args.empty():
		args = args[random_index]
	callv(random_func, args)


Without arguments:

func _ready():
	call_random(['f0', 'f1', 'f2'])

func f0(): print('f0')
func f1(): print('f1')
func f2(): print('f2')

With arguments:

func _ready():
	call_random(['f0', 'f1', 'f2'], [[], [1], [2, 3]])

func f0(): print('f0')
func f1(a): print('f1: ', a)
func f2(a1, a2): print('f2: %s - %s' % [a1, a2])

Remember to call randomize() somewhere.