Post by Aquan on Feb 16, 2015 5:51:05 GMT
I'll keep this brief-ish, since I'm not a particularly good teacher.
Functions are an easy way to keep all of your code in one place. Instead of a hodgepodge of dependency files, or worse, a ton of code written into strings, functions can be used to create chunks of code to run within a Mystery Script program.
Lets start with the basics. The basic way to create a function is as follows:
This will create a function called funcname(), which can be called in your program. If you type
Into your program it will, at that point in the code, print "This is a function." to the entire world. If you were, instead, to type
It would instead print
...And yes, I'm aware this might not be the most clear explanation in the world at this point. Anyway, moving on...
A slightly more complex function declaration takes the form...
Which will allow you to pass variables into a function. Since, normally, variables used outside the function cannot be used inside the function, this is quite useful.
As an example...
...While something this simple is best done without a function, I hope it gets the point across. If, in the main program, you type
The program will print the number 9.
Now, for the part functions are most useful for. Since a function written this way can be used anywhere within the program, you can use it for a code argument. Built-in functions like list_cycle() and setcustommode() use turn strings into code through some sort of technomagic. Using a function in said strings will allow you to keep your code compact and readable.
Please note the space before the player-made function calls in the quotes. Last I checked, these were necessary to make the interpreter not cry. You do not want the interpreter to cry during your modes. (Edit: As of the latest version of 1.98u, this is no longer necessary.)
(Note: my is a variable used in several functions to indicate the object running the function. In the case of a list_cycle(), the object running the function is the current cycled object.)
The above code will create a mode that will print the start message and then print the alias of the killer to every player in-game. It does this all in one file, without any dependencies needed.
That should about cover it for this tutorial. If you have any questions, feel free to reply and I'm sure someone who knows what they're talking about will answer you.
Functions are an easy way to keep all of your code in one place. Instead of a hodgepodge of dependency files, or worse, a ton of code written into strings, functions can be used to create chunks of code to run within a Mystery Script program.
Lets start with the basics. The basic way to create a function is as follows:
func funcname()
{
wprint("This is a function.");
return("This is a return value.");
}
This will create a function called funcname(), which can be called in your program. If you type
funcname();
Into your program it will, at that point in the code, print "This is a function." to the entire world. If you were, instead, to type
returnvalue = funcname();
wprint(returnvalue);
It would instead print
This is a function.
This is return value.
...And yes, I'm aware this might not be the most clear explanation in the world at this point. Anyway, moving on...
A slightly more complex function declaration takes the form...
func funcname(arg1 | arg2 | ...)
{
/*code here*/
}
Which will allow you to pass variables into a function. Since, normally, variables used outside the function cannot be used inside the function, this is quite useful.
As an example...
func adder(num1 | num2)
{
return(num1 + num2);
}
...While something this simple is best done without a function, I hope it gets the point across. If, in the main program, you type
wprint(adder(3, 6));
The program will print the number 9.
Now, for the part functions are most useful for. Since a function written this way can be used anywhere within the program, you can use it for a code argument. Built-in functions like list_cycle() and setcustommode() use turn strings into code through some sort of technomagic. Using a function in said strings will allow you to keep your code compact and readable.
func tellkilleralias(person)
{
printto(person, strcat("The killer is ", get_alias(killer), "."));
}
func FuncModeStart()
{
wprint("<big>Dead body blah blah blah, doors locked blah blah blah.</big>");
ingame = GetIngame();
player_list_cycle(ingame, " tellkilleralias(my);");
}
func FuncModeEnd()
{
wprint("The killer was unimportant.");
}
setcustommode("Func", 1, "http://misuteriihigh.boards.net/thread/330/semi-basic-functions-tutorial", "Func.ogg", "Func.dmi", " FuncModeStart();", " FuncModeEnd();");
Please note the space before the player-made function calls in the quotes. Last I checked, these were necessary to make the interpreter not cry. You do not want the interpreter to cry during your modes. (Edit: As of the latest version of 1.98u, this is no longer necessary.)
(Note: my is a variable used in several functions to indicate the object running the function. In the case of a list_cycle(), the object running the function is the current cycled object.)
The above code will create a mode that will print the start message and then print the alias of the killer to every player in-game. It does this all in one file, without any dependencies needed.
That should about cover it for this tutorial. If you have any questions, feel free to reply and I'm sure someone who knows what they're talking about will answer you.