But what is a method exactly?
To put it simply, it's a way of simplifying and organizing your code by segmenting it into different parts.
All of the same syntax applies, it's just WAAAY easier to read in some cases.
Let's say we have an object that we want to do a series of commands. Let's say we want a Jedi to Force shock a Sith.
That script might look like this:
Code:
void main(){
object oJedi = GetObjectByTag("JediKnight");
object oEnemy = GetNearestObjectByTag("Sith");
AssignCommand(oJedi, ActionUseSpellOnObject(FORCE_POWER_SHOCK, oEnemy);
}
Fair enough, but what if we want the Jedi to use more than one spell in a sequence? That would look something like this:
Code:
void main(){
object oJedi = GetObjectByTag("JediKnight");
object oEnemy = GetNearestObjectByTag("Sith");
AssignCommand(oJedi, ActionUseSpellOnObject(FORCE_POWER_SHOCK, oEnemy);
DelayCommand(1.0, AssignCommand(oJedi, ActionUseSpellOnObject(FORCE_POWER_PLAGUE, oEnemy));
}
*Notice the use of DelayCommand. You CANNOT assign multiple commands in one instance.
Fair enough again, but what if we have another Jedi join in on the Sith torture? Would we have to keep adding AssignCommand calls? No, not necessarily. Check out the following code:
Code:
void torture(object obj, object victim);
void main(){
object oJedi = GetObjectByTag("JediKnight");
object oJedi2 = GetObjectByTag("JediMaster");
object oJedi3 = GetObjectByTag("JediPadawan");
object oEnemy = GetNearestObjectByTag("Sith");
torture(oJedi, oEnemy);
torture(oJedi2, oEnemy);
torture(oJedi3, oEnemy);
}
void torture(object obj, object victim){
AssignCommand(obj, ActionUseSpellOnObject(FORCE_POWER_SHOCK, victim);
DelayCommand(1.0, AssignCommand(obj, ActionUseSpellOnObject(FORCE_POWER_PLAGUE, victim));
}
For every instance of torture in void main, it will actually read the code in void torture(), substituting the parameters for whichever objects you write in! Cool huh?
Now, not ever method has to be void main() or void whatever(), you can actually write something like:
int countMyFoes()
string getMyName()
object getWeakestEnemy()
but that's a lesson for another time...