Script Structure

Hi guys
I am working on the code for the text based side of my Volleyball Manager game, was seeing if I could get some help for the structure of my scripting

function Update () {

if (teamserve == 1  homerotation == 1) { // home team is serving in rotation 1
	matchcommentary = matchcommentary + "\n" + home1name + " is serving";
	servenumber = Random.Range(1,1000);
	passnumber = Random.Range(1,1000);
	servedirection = Random.Range(1,3);
	
	if (servedirection == 1) {
		matchcommentary = matchcommentary + "\n" + home1name + " has served at " + away2name;
		
			if (servenumber > passnumber) {
	
				if (servenumber >= 0  servenumber <= 100) {
				matchcommentary = matchcommentary + "\n" + away2name + " has shanked the ball into the crowd\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}	
				else if (servenumber > 100  servenumber <= 200) {
				matchcommentary = matchcommentary + "\n" + away2name + " has passed the ball off the net";
				}
				else if (servenumber > 200  servenumber <= 400) {
				matchcommentary = matchcommentary + "\n" + away2name + " has passed the ball nicely into position";	
				}
				else if (servenumber > 400  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away2name + " passes perfect";
				}
			}
			
			if (passnumber > servenumber) {
				
				if (servenumber >= 0  servenumber <= 500) {
				matchcommentary = matchcommentary + "\n" + away2name + " has passed nicely";
				}
				else if (servenumber > 500  servenumber <= 800) {
				matchcommentary = matchcommentary + "\n" + away2name + " has got the ball high in the middle";
				}
				else if (servenumber > 800  servenumber <= 900) {
				matchcommentary = matchcommentary + "\n" + away2name + " has trouble with the pass";	
				}
				else if (servenumber > 900  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away2name + " has been aced\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}
			}
	}
	
	if (servedirection == 2) {
		matchcommentary = matchcommentary + "\n" + home1name + " has served at " + away7name;
		
			if (servenumber > passnumber) {
	
				if (servenumber >= 0  servenumber <= 100) {
				matchcommentary = matchcommentary + "\n" + away7name + " has shanked the ball into the crowd\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}	
				else if (servenumber > 100  servenumber <= 200) {
				matchcommentary = matchcommentary + "\n" + away7name + " has passed the ball off the net";
				}
				else if (servenumber > 200  servenumber <= 400) {
				matchcommentary = matchcommentary + "\n" + away7name + " has passed the ball nicely into position";	
				}
				else if (servenumber > 400  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away7name + " passes perfect";
				}
			}
			
			if (passnumber > servenumber) {
				
				if (servenumber >= 0  servenumber <= 500) {
				matchcommentary = matchcommentary + "\n" + away7name + " has passed nicely";
				}
				else if (servenumber > 500  servenumber <= 800) {
				matchcommentary = matchcommentary + "\n" + away7name + " has got the ball high in the middle";
				}
				else if (servenumber > 800  servenumber <= 900) {
				matchcommentary = matchcommentary + "\n" + away7name + " has trouble with the pass";	
				}
				else if (servenumber > 900  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away7name + " has been aced\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}
			}
	}

	if (servedirection == 3) {
		matchcommentary = matchcommentary + "\n" + home1name + " has served at " + away5name;
		
			if (servenumber > passnumber) {
	
				if (servenumber >= 0  servenumber <= 100) {
				matchcommentary = matchcommentary + "\n" + away5name + " has shanked the ball into the crowd\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}	
				else if (servenumber > 100  servenumber <= 200) {
				matchcommentary = matchcommentary + "\n" + away5name + " has passed the ball off the net";
				}
				else if (servenumber > 200  servenumber <= 400) {
				matchcommentary = matchcommentary + "\n" + away5name + " has passed the ball nicely into position";	
				}
				else if (servenumber > 400  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away5name + " passes perfect";
				}
			}
			
			if (passnumber > servenumber) {
				
				if (servenumber >= 0  servenumber <= 500) {
				matchcommentary = matchcommentary + "\n" + away5name + " has passed nicely";
				}
				else if (servenumber > 500  servenumber <= 800) {
				matchcommentary = matchcommentary + "\n" + away5name + " has got the ball high in the middle";
				}
				else if (servenumber > 800  servenumber <= 900) {
				matchcommentary = matchcommentary + "\n" + away5name + " has trouble with the pass";	
				}
				else if (servenumber > 900  servenumber <= 1000) {
				matchcommentary = matchcommentary + "\n" + away5name + " has been aced\n*****(" + homescoreset1 + " - " + awayscoreset1 + ")*****";
				}
			}
	}

}

This is an example of the code I am currently implementing. This example shows how the actions of only ONE skill are decided in the game and inside all of these nested ifs will be branched out to around 4-5 more different skills which I know can create a ton of hassles when debugging in the future. I am not so concerned at the moment about the content of the code but I want to get into good coding habits which will make it easier to debug in the future and was seeing if anyone could help with the structure. One problem I already see is that I have hard-coded the player names in the choices which makes it difficult to substitute players when they are taken off the field or changed into position.

Thanks in advance

Perhaps you could use an array of player names and have a ‘pointer’ to the current player on the field.

(forgive me I cannot do Javascript - it’s in C# you’ll probs get the drift):

private int playerOnCourt = 0;
private string[] playerNames = new string[4] { "bill", "bob", "beverly", "barney" };

matchcommentary = playerNames[playerOnCourt] + " has shanked the ball";

Obviously you need to add all the different variables and you wouldn’t initialise the array in this way (with the static names). When you want to sub the payer you change the playerOnCourt value to index the array.

Hope this helps.

JT

/*
firstly, multiple IF-ELSE-IF-ELSE is always a sign of bad coding plactice...
Always try and break your code into small pieces rather than having one very large piece of code
Small code sections is always easier to debug. Even if you have 20 functions in stead of the one,
as long as you don't do something silly like put one command inside a function (unless you really HAVE to)

Secondly, you are making your life way to complicated with this code...
You are choosing a random value between 0 and a 1000 and then no matter what the value is, you choose one of 4
results. Thus, your chances of showing one of the 4 results is always 25%. So why not simply make a random selection
between 1 and 4 and choose your result as a direct result of the random value. I see, depending on another variable,
calculated in the same way, you give preference to one of the four options i.e either the first or last. In that case,
you could simply make a selection out of 6, instead of 4 and give specific results based on the 3 and one specific
value based on the last three. I.e.

value1 = Random.Range(0,3);
value2 = Random.Range(0,5);

if (value1 > value2)
{
	switch(value1)
	{
		case 0: Print("");
		case 1: Print("");
		case 2: Print("");
		default: Print("The others");
	}
} else
{
	switch(value1)
	{
		case 3: Print("");
		case 4: Print("");
		case 5: Print("");
		default: Print("The others");
	}
}

For my examples below, I will stick more or less to your way of doing things. However, instead of saying IF-ELSE-IF-ELSE,
I am going to put the values you test against inside 2 arrays and then select the correct value from there and print
the corresponding message from that...

I am going to split your code into 3 functions:
1: one that just spits out commentary
2: one that selects which commentary to get
3: one that adds the commentary to the already present commentary

Good coding practice means:
- Always keep your code small and manageable
- Comment your code so new people can immediately see what you were doing
- name your functions well so you can easily understand the code : function agp3ex() or function GetTheEngine() ???

*/

var CommentrySelection	= new Array();				//master array
CommentrySelection[0]	= new Array(0, 100, 200, 400, 1000);	//values for if value is greater than other value
CommentrySelection[1]	= new Array(0, 500, 800, 900, 1000);	//values for vice versa

var matchCommentary	: String = "";

// this function takes one argument which depicts wether the servenumber > passnumber
function GetServeNumber(commentaryArray: int) : int
{
   var result		: int = 0;
   var servenumber 	: int = Random.Range(1,1000);
   var index 		: int = 0;

   // instead of saying: if less than and bigger than or else if less than and bigger than or else...
   // I just skip through all the values that are smaller than the BIG value and return the last one
   for (var x in CommentrySelection[commentaryArray])
   {
	if (serveNumber > x)
	{
		result = index;
		break;
	}
	index ++;
   }

   // each serveDirection has 8 comments. Depending on the random value above, you will receive one of 4
   // values, but if you need to access the other 4, simply add 4 to it. If you want the first 4, multiplying
   // by 0 will not change the value and since the only possible values for the second argument is either 0 or 1
   // this following line of code will either add 4 or do nothing...
   result += 4 * commentaryArray;

   return result;
}

//here I return all the possible commentary you have shown in your code. I now just have to find an index into this list
//to determine which comment to show
function CreateCommentary(index : int, homeName : String, awayName: String, homeScore: float, awayScore: float) : String
{
   switch(index)
   {
	case 0: return "\n" + awayName + " has shanked the ball into the crowd\n*****(" + homeScore + " - " + awayScore + ")*****";
	case 1: return "\n" + awayName + " has passed the ball off the net";
	case 2: return "\n" + awayName + " has passed the ball nicely into position";
	case 3: return "\n" + awayName + " passes perfect";
	case 4: return "\n" + awayName + " has passed nicely";
	case 5: return "\n" + awayName + " has got the ball high in the middle";
	case 6: return "\n" + awayName + " has trouble with the pass";
	case 7: return "\n" + awayName + " has been aced\n*****(" + homeScore + " - " + awayScore + ")*****"; 
   }
}

// the odd case where 1 line of code saves you many more.
// instead of saying: if 1 then or else if 2 then or else, I simply place the generic line of code here and call it
//with different values below to yield the right result. This is more of a Macro than a function...
// you could of course add this code in directly and still keep the code down to a minimum, but since you asked
//about layout... I think this just looks neater...
function GetTopCommentry(awayName: String) : String
{
	return "\n" + home2name + " has served at " + awayName;
}

function ServeDirectionFunction(serveDirection: int)
{
	switch(serveDirection)
	{
	  case 1 : matchcommentary +=  GetTopCommentary(away2name);
	  case 2 : matchcommentary +=  GetTopCommentary(away7name);
	  case 3 : matchcommentary +=  GetTopCommentary(away5name);
	}

	// you choose 2 random values and then immediately compare if one is bigger and then do something based on this
	// this means you are looking for a random YES-NO answer. I am going to do this like so:
	var isLarger : boolean = (random.Range(0,1000) > 500) ? TRUE : FALSE;

	// now, from your code I can see that if the value was less than, then you want to show one of 4 specific values
	// or else you want to show one of 4 other, but still specific, values... This following function first of all
	// determines which of the 4 values to show. It takes an argument with a value of either 0 or 1 as this number is
	// multiplied by 4 and added to the selected number to determine wether it will use the selected number from the
	//first choice or from the second choice. Remember, I placed all 8 comments in an array and this function selects
	// a value of up to 4. Now x + (0 * 4) = x, right? However, x + (1* 4) = x + 4... Hence the below code...
	// you could always change this to a boolean value and change the above function accordingly and improve readability
	// that way...
	if (isLarger)
		serveNumber = GetServeNumber(0);
	else	serveNumber = GetServeNumber(1);

	//you now have the text index from 0 to 7, all you have to do is select the names to put in it
	 switch(serveDirection)
	 {
	     case 0: matchcommentary += CreateCommentary(serveNumber, "",away2name, homescoreset1, awayscoreset1); break;
	     case 1: matchcommentary += CreateCommentary(serveNumber, "",away7name, homescoreset1, awayscoreset1); break;
	     case 2: matchcommentary += CreateCommentary(serveNumber, "",away5name, homescoreset1, awayscoreset1); break;
         } 
}

function Update () { 

if (teamserve == 1  homerotation == 1)
{ // home team is serving in rotation 1 
   matchcommentary += "\n" + home1name + " is serving"; 

   serveDirection = Random.Range(0,2); 
   ServeDirectionFunction(serveDirectiont);
}
    
}