Testing if EnemyAI has movement points for move actions or other actions

Hello,

My unit has been separated in 2 different types of “actions” the Move action and “Other” actions, they use different points and are independent of eachother, in this case how should i go about testing if the player has action points to do either a move action or an other action in the EnemyAI script, TryTakeEnemyAIAction.

I have two functions for checking if the unit has enough points to either move or to do other actions(naming will change most likely)

public bool CanSpendActionPointsToMoveAction(BaseAction baseAction)
	{
		if(moveActionPoints >= baseAction.GetActionPointsCost())
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public bool CanSpendActionPointsToTakeOtherActions(BaseAction baseAction)
	{
		if(otherActionPoints >= baseAction.GetActionPointsCost())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

I dont know if i explained well enough what im looking for here, if not please let me know and il try to explain it better.

So apologies for the stupid question, I was quite tired at the time of posting it but i did figure out the solution to it… il leave the code snippet here in case anybody is doing something similar to what i am and might find this usefull.
Il just paste the check, the rest of the code is exactly the same as in the video.

private bool TryTakeEnemyAIAction(Unit enemyUnit,Action onEnemyAIActionComplete)
	{
		EnemyAIAction bestEnemyAIAction = null;
		BaseAction bestBaseAction = null;
		foreach (BaseAction baseAction in enemyUnit.GetBaseActionArray())
		{
			if(baseAction.GetActionName() == "Move")
			{
				if (!enemyUnit.CanSpendActionPointsToMoveAction(baseAction))
				{
					continue;
				}
			}
			else if(baseAction.GetActionName() != "Move")
			{
				if (!enemyUnit.CanSpendActionPointsToTakeOtherActions(baseAction))
				{
					continue;
				}
			}
1 Like

I would caution you against using strings to identify the action, strings are very brittle and error prone, you should never use them for identifying things. I actually talked about this topic in a very recent video https://youtu.be/0_UiF-4-7xM?t=348
You could instead ask the enemyUnit to give you the MoveAction reference, assign the MoveAction in a separate field, then just compare baseAction == moveAction;
Nice job on implementing separate Move and Action points!

2 Likes

Hello there my favorite CodeMonkey!
Il take this time to thank you for all the hard work and all the knowledge you have shared with the community across the years, i have been following you for a long time and its a pleasure doing this course, i plan on releasing a game on steam and il use this course as a base to build upon, so thank you and keep up the good work.

Now on the topic at hand, you are absolutely correct and i have made the necessary changes to avoid using strings, i will place the code here in case anybody else needs it.
Also in the future i might want to make certain abilities(actions) use both a movement point and an action point so the if statements might need to check for something else instead of just the action, maybe a 2 bool flags, but thats for future me.

private bool TryTakeEnemyAIAction(Unit enemyUnit,Action onEnemyAIActionComplete)
	{
		EnemyAIAction bestEnemyAIAction = null;
		BaseAction bestBaseAction = null;
		foreach (BaseAction baseAction in enemyUnit.GetBaseActionArray())
		{
			if(baseAction == enemyUnit.GetMoveAction())
			{
				if (!enemyUnit.CanSpendActionPointsToMoveAction(baseAction))
				{
					continue;
				}
			}
			else if(baseAction != enemyUnit.GetMoveAction())
			{
				if (!enemyUnit.CanSpendActionPointsToTakeOtherActions(baseAction))
				{
					continue;
				}
			}
2 Likes

A small optimization here, as repeated access to a method that will yield the same result every time is unneccessary…
At the beginning of TryTakeEnemyAIAction:

BaseAction moveAction = enemyUnit.GetMoveAction();

then in the foreach loop

if(baseAction == moveAction)
			{
				if (!enemyUnit.CanSpendActionPointsToMoveAction(baseAction))
				{
					continue;
				}
			}
            //At this point, baseAction cannot == the moveAction, as 
            //that case has already be dealt with in the if clause that this else refers to
            //making this if redundant.
			else //if(baseAction != enemyUnit.GetMoveAction())   
			{
				if (!enemyUnit.CanSpendActionPointsToTakeOtherActions(baseAction))
				{
					continue;
				}
			}
3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms