I’m making a turn based RPG where first you set all the actions, commands essentially, for each of your party members first then the battle plays out. However i’m using coroutines as the commands, when i go to create and execute the command nothing happens, but when using a simple command whose return type is void and just prints something to the console it works fine.
Do coroutines not work with the Command Pattern?
Code for the interface
using System.Collections;
public interface ICommand
{
IEnumerator Execute();
}
Where the command is being created, queued and called
public void SetEnemy(int enemyIndex)
{
targetEnemy = enemies[enemyIndex];
CreateCommand();
commandBuffer.Dequeue().Execute();
}
public void CreateCommand()
{
ICommand command;
if (battleAction == BattleAction.Fight)
{
command = new FightCommand(battleLogs, actingCharacter, targetEnemy);
commandBuffer.Enqueue(command);
}
}
The actual command whose return type is IEnumerator
using System.Collections;
using UnityEngine;
using TMPro;
public class FightCommand : ICommand
{
GameObject[] battleLogs;
Character character;
Enemy enemy;
public FightCommand(GameObject[] battleLogs, Character character, Enemy enemy)
{
this.battleLogs = battleLogs;
this.character = character;
this.enemy = enemy;
}
public IEnumerator Execute()
{
battleLogs[0].SetActive(true);
battleLogs[0].GetComponentInChildren<TextMeshProUGUI>().text = character.characterName;
yield return new WaitForSeconds(0.5f);
battleLogs[1].SetActive(true);
battleLogs[1].GetComponentInChildren<TextMeshProUGUI>().text = enemy.characterName;
yield return new WaitForSeconds(1.1f);
bool critical;
int damage = StatCalculations.CharacterFight(character, enemy, out critical);
if (critical)
{
battleLogs[2].SetActive(true);
battleLogs[2].GetComponentInChildren<TextMeshProUGUI>().text = "Crit";
yield return new WaitForSeconds(1);
}
int trueDamge = damage - enemy.absorb;
if (trueDamge <= 0)
trueDamge = 1;
battleLogs[3].SetActive(true);
battleLogs[3].GetComponentInChildren<TextMeshProUGUI>().text = trueDamge.ToString();
yield return new WaitForSeconds(1);
enemy.currentHp -= trueDamge;
if (enemy.currentHp <= 0)
{
battleLogs[4].SetActive(true);
yield return new WaitForSeconds(1);
enemy.transform.parent.gameObject.SetActive(false);
//Destroy(enemy.gameObject);
}
}
}