Writing Your First Script
Traditionally the first program you write in any language is the famous "Hello World!" program. Well who are we to break with convention? Our first script will be one that pops up a message box with the text "Hello World!" as the message.
We've already covered all the elements that will make up the script except for one. We still need to cover how to identify our file as a script.
Declaring your file as a messiah:script
file:
Ok this is really difficult so pay close attention. You need
to use the ".msa" file extention and the first line in the file
must be:
//#messiahscript
Simple huh? That's all there is to it, no caveats; just follow those two rules and you'll be fine. Ok here we go, we now have all the information we need to create our first script:
HelloWorld.msa
//#messiahscript
string main()
{
string msg = "Hello World!";
MessageBox( msg, 0 );
return( msg );
}
That's it! Our first line declares this file as a messiah:script file. Next we have the function definition. We declare a local string variable called 'msg' and assign it a value of "Hello World!". Next we pass that variable to the library function 'MessageBox' which will pop up a Windows message box with the text "Hello World!" as the caption. Finally we will return the string to the caller.
Now, how do we use scripts in messiah?
There are three ways that we can call a script from messiah: from an expression, from the command line, and indirectly through another script.
Calling the script from the
Command Line:
Simply type the function in the Command Line field and hit
enter. The function will execute immediately and will only
execute once. Remember to qualify your function with the name
of the script it belongs to
Calling the script from an
expression:
Enter the function call in your variable's expression just as you
would any other built-in function. The function will execute
every time the scene needs to update. This will be at least
every time the frame changes but possibly more often. If the
variables is bypassed (see Command mode documentation) the
function will not execute.
Calling the script
from another expression:
Already discussed in Functions.
Before we can call a function from a script we
need to place it where messiah can find it. Currently that must be
in the messiah/modules/scripts directory.
Now launch messiah (which causes all scripts in the scripts directory to be compiled) and type:
HelloWorld.main()
at the Command Line. A message box pops up the says… well you know.
We can actually simplify this command a little. When a script contains a function called 'main' then it is called implicitly but just using the script name. So the following are equivalent:
HelloWorld.main()
and
HelloWorld()
This is only true for the function called 'main'.
Since the function takes no arguments we can even leave out the empty parentheses and type:
HelloWorld
This is true for any function that doesn't take arguments, however the function definition in the script file must contain empty parentheses, they are only optional when we are calling the function not defining it.
Converted from CHM to HTML with chm2web Pro 2.82 (unicode) |