- Home /
Alternative to Update
I'm trying to subtract health in a turn based battle system that I've been working on recently and whenever I would try and subtract player health in a specific enum/state in state machine (housed within update) only once but every time I would run the code the variable would subtract multiple times, but my function for subtracting Enemy Health would subtract once (as it should) since it was housed within a Task On Click function. Is there any way I can subtract Player Health within my state machine without having it repeat like this? I'm still fairly new to coding in Unity so any help I can get would be very appreciated. Thank you!
Code for reference:
public void TaskOnClick()
{
if (currentState == BattleStates.PLAYERCHOICE)
{
currentState = BattleStates.PLAYERATTACK;
StartCoroutine(PlayerAttack());
StartCoroutine(SwitchStates1());;
}
}
public IEnumerator SwitchStates1()
{
yield return new WaitForSeconds(2);
Player1AttackingOver = true;
yield return new WaitForSeconds(0.5f);
currentState = BattleStates.ENEMYCHOICE;
}
public IEnumerator EnemyAttack()
{
yield return new WaitForSeconds(0.5f);
enemy1.transform.position = EnemyAttackPosition;
spriteChange.playerHP -= 25;
yield return new WaitForSeconds(1f);
enemy1.transform.position = EnemyPosition;
yield return new WaitForSeconds(1f);
}
public enum BattleStates
{
BATTLESTART,
PLAYERCHOICE,
PLAYERATTACK,
ENEMYCHOICE,
ENEMYATTACK,
LOSE,
WIN
}
void Update()
{
M_ATTACK.onClick.AddListener(TaskOnClick);
Debug.Log(currentState);
Debug.Log(Values.EnemyHealth);
Debug.Log(spriteChange.playerHP);
switch (currentState)
{
case (BattleStates.BATTLESTART):
StartCoroutine(BattleWaitStart());
break;
case (BattleStates.PLAYERCHOICE):
break;
case (BattleStates.PLAYERATTACK):
UI.GetComponent<GraphicRaycaster>().enabled = true;
break;
case (BattleStates.ENEMYCHOICE):
Player1AttackingOver = false;
StartCoroutine(EnemyAttack());
break;
case (BattleStates.ENEMYATTACK):
break;
case (BattleStates.LOSE):
break;
case (BattleStates.WIN):
break;
}
}
Im not sure I am 100% sure what you mean. Can you update your post to be more clear about what exactly the problem is?
If I was making a turn based game I wouldn't put any game logic in update.
I agree.. coroutines and events are your best friends
Answer by highpockets · Dec 22, 2020 at 08:38 PM
I usually change state with getters and setters. Perhaps this is more along the lines of what you want. I don’t know if you just want currentState to be private or not, but I have included a Public getter and setter.
public enum BattleStates
{
BATTLESTART,
PLAYERCHOICE,
PLAYERATTACK,
ENEMYCHOICE,
ENEMYATTACK,
LOSE,
WIN
}
private BattleStates _state = BattleStates.PLAYERCHOICE; //give an initial state
public BattleStates state
{
get { return _state; }
set {
_state = value;
switch( value ){
case BattleStates.PLAYERCHOICE :
//player choice code
break;
case BattleStates.BATTLESTART :
//battle start code
break;
case BattleStates.PLAYERATTACK :
//player attack code
break;
case BattleStates.ENEMYCHOICE :
//Enemy choice code
break;
case BattleStates.ENEMYATTACK :
//enemy attack code
break;
case BattleStates.LOSE :
//Lose code
break;
case BattleStates.WIN :
//Win code
break;
}
}
}
private void SomeFunction()
{
state = BattleStates.WIN; //Use setter and the switch statement will run the necessary code for the win state
}