need help implementing algoritm

hi all,

i need a bit of help implementing an algorithm.

im creating a beat’em’up game and according to what the first player does there are consequences on the second player and vice versa

let me be a bit more clear:

the players, for now, have 4 choises => attack, defende, idle, especial

i have an array whit all the possibilities = {1010,1020,1030,1040, 2010,2020,2030,1040, … etc}
1010 means player1 choise is attack and player2 is all so attack
1020 means player1 choise is attack and player2 is all so defence
etc…

example:

player1 choice is attack, i save this info as an int value = 10
player2 choice is defence, i save this info as an int value = 20
choice = 1020

for(int i =0; i< possibilities .Length; i++)
		{
			if(possibilities [i].Equals(choise))
			{
				if(metodos_possiveis[i].Equals(1010)
				{
					Ataque_Ataque(jogador1,jogador2);
				}
				else if(metodos_possiveis[i].Equals(1020))
				{
					Ataque_Defesa(jogador1,jogador2);
				}
				else if(metodos_possiveis[i].Equals(1030))
				{
					Ataque_Especial(jogador1,jogador2);
				}
				else if(metodos_possiveis[i].Equals(1040))
				{
					Ataque_Idle(jogador1,jogador2);
				}
			}
		}

so i need to call attack_defence() method, to calculate the dmg given to each player

its solution is allready too big, there are allready too many options and i want to scale this up, so that the players will have 3 attack, 3 defence, 1 idle and 10 special and those are just too many combinations

can i create an Array of methods => Array[ ] methods = [attack_attack(),attack_defence() … etc]???
so i can do

for(int i =0; i< possibilities .Length; i++)
		{
			if(possibilities [i].Equals(choise))
			{
				methods [i];
			}
		}

or another solution.

thx guys.

look into delegates. they encapsulate function pointers in a data type and thus can be used as array. a delegate can also store mutiple methods which ar executed when the delegate is invoked.

[SOLVED]

thx exiguous

ur sugestion did the trick :slight_smile:

for any one who as the same question and does not want to do a milion if’s here is a bit of code i did inspired by exiguous .

using UnityEngine;
using System.Collections;

public class s_delegate_testes : MonoBehaviour {


	delegate void Movimento();
	Movimento aa;
	Movimento ad;
	Movimento ai;


	string[] s_teste = {"aa","ad","ai"};
	Movimento[] a_teste = new Movimento[3];

	// Use this for initialization
	void Start () {
	
		aa += C_Ataque_Ataque;
		a_teste[0] = aa;
		ad += C_Ataque_Defesa;
		a_teste[1] = ad;
		ai += C_Ataque_Idle;
		a_teste[2] = ai;

	}
	
	// Update is called once per frame
	string t ="" ;
	void OnGUI() 
	{

		t = GUI.TextField(new Rect(0,0,75,30),t );

		if( GUI.Button(new Rect(80,0,50,30),"teste"))
		{

			for(int i =0; i<a_teste.Length; i++)
			{
				if(s_teste[i].Equals(t) )
				{
					a_teste[i]();
				}
			}
		}

	}

	public void C_Ataque_Ataque()
	{
		print("Ataque_Ataque");
	}
	public void C_Ataque_Defesa()
	{
		print("C_Ataque_Defesa");
	}
	public void C_Ataque_Idle()
	{
		print("C_Ataque_Idle");
	}

}

glad to be of help and thanks for the feedback. many people miss this these days. good luck with your project.