How to make an enemy A.I in turn based, that picks random move in his turn

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

hi, this is my first question im very beginner to godot, Im making a simple turn based game similar to hearbeast tutorial.
On extending his tutorial, Now my enemy does only damage in his turn
But i want him to pick random move in his turn such as healing…
This is my damage code in enemy scene :


func deal_damage():
rng.randomize()
damage = rng.randi_range(4,10)
BattleUnits.PlayerStats.hp -= damage

Should i create func heal_hp like that,Even after creating function healp_hp how can i make my enemy to pick any one move in his turn
This is my attack code in enemy scene

func attack() -> void:
yield(get_tree().create_timer(0.4), "timeout")
animationPlayer.play("Attack")
yield(animationPlayer, "animation_finished")
emit_signal("end_turn")

This is my code in battle scene

func start_enemy_turn():
battleActionButtons.hide()
textboxpanel.hide()
var enemy = BattleUnits.Enemy
if enemy != null and not enemy.is_queued_for_deletion():
	enemy.attack()		
	yield(enemy, "end_turn")
start_player_turn()

Is using function is a right way OR
Is there any other better solution to determine enemy to pick a random move in his turn… Please I need help

:bust_in_silhouette: Reply From: Lopy

You could do something like this (inside enemy) :
const moves := ["heal", "attack", "attack", "defend"]
var next_moves := []
func play():
. if next_moves.empty(): // Refill on moves
. . next_moves = moves.duplicate()
. . next_moves.shuffle()
. var action: String= next_moves.pop_front()
. call(action) //Call a function by name
func heal():
. pass
func attack():
. pass
func defend():
. pass

This allows you to easily change what the enemy can do as it goes, by adding or removing elements from moves.

hey, THANK YOU SO MUCH
works very well

Nirmal | 2020-12-23 06:12