Creating Functions

From COD Modding & Mapping Wiki
Revision as of 19:53, 15 October 2008 by Zeroy (talk | contribs) (New page: == Creating Functions == Image:Nutshell.png A custom function is a good way to use repeat sections of code more efficiently. For example, if you often use the same sequence of cod...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Creating Functions

A custom function is a good way to use repeat sections of code more efficiently. For example, if you often use the same sequence of code, you can template them into a custom function. Imagine this is your code...

wait 1;
brush1 moveZ(320, 5);
brush1 waittill("movedone");
wait 1;
brush2 moveZ(540, 3);
brush2 waittill("movedone");

This can be simplified using a custom function, lets call this function "_moveEnt" (it is common practice to use an underscore as the first character of a function)

_moveEnt(ent, dist, time)

As the above shows, we are going to need 3 arguements, ent (entity), dist (distance) and time.

Now lets look at the full code with custom function in use...

{
  _moveEnt(brush1, 320, 5);
  _moveEnt(brush2, 540, 3);
}
_moveEnt(ent, dist, time)
{
  wait 1;
  ent moveZ(dist, time);
  ent waittill("movedone");
}

As the above code shows, the custom function can simply be called using the required arguements, each time it is called, the details are changed in the custom function and are processed.

Once they are finished, it goes back to the main function.

Functions can also return values, for example performaing mathmatical equations. A new function to get 3D area...

{
  area = _areaEquation(2, 4, 6);
}
_areaEquation(a, b, c)
{
  answer = (a * b) * c;
  return answer;
}

Once the code calls the function '_areaEquation' the function works out the 'answer', 'answer' is the returned. This declares a new variable (area). The variable area, is the answer that is returned by the function.