There are quite a few common questions about the new scripting engine, and it's syntax, which I hope to answer here for those new to the new engine.
There is a starting guide to the new engine, which you
can find here. If you still have unanswered questions, take a look in this thread.
Do I have the new engine enabled on my server?
You can test to see if the new engine is enabled on your server by using a basic script like this:
PHP Code:
function onCreated()
{
sendtorc("The new engine is enabled!");
}
This script should send "The new engine is enabled!" to RC chat if it is enabled, and if not, you can request it be enabled on your server by using the
GraalOnline Support Center.
Does the old engine scripting still work on a server where the new engine is enabled?
Yes. However, you may notice a couple of differences. Mostly, they are:
- Any compiler errors will appear in RC, regardless of which version of gscript you are using.
- Strings are unset when their value is set to 0 (zero), and thus, may not appear in the player attributes or server strings.
Is it okay to mix new script engine code and old script engine code?
Preferably not. Even though the code will work, it is generally encouraged that a script uses only the new or the old engine.
From using the old scripting engine, I have been using #s, #v and #I to access variables. How do I do it in the new engine?
Old gscript was not able to tell the difference between strings, numeric variables and "string lists" (arrays), so these message codes were used to get the contents of these variables. However, in the new engine, the variable are "
variant", which means that the engine already knows what type of data is being stored. Therefore, you can store variables like this:
PHP Code:
this.string = "String data";
this.number = 3;
this.array = {"First Member", "Second Member", "etc"};
And you can similarly read them just by using their names:
PHP Code:
echo(this.string); // will echo "String data"
echo(this.number); // will echo "3"
echo(this.array); // will echo "First member,Second Member,etc"
echo(this.array[0]); // will echo "First member"
echo(this.array[1]); // will echo "Second member"
In the old engine, I used more message codes like #c and #n to modify the chat text, nickname and other attributes about the NPC or player. How do I do this in the new engine?
In the new engine, there are new objects for the player, the NPC, etc. Here are some examples on how to set the player's attributes:
PHP Code:
setplayerprop #c,New chat text;
player.chat = "New chat text";
setplayerprop #n,New nickname;
player.nick = "New nickname";
setplayerprop #g,New guild tag;
player.guild = "New guild tag";
Similarly, because you can set attributes about the NPC in a similar way:
PHP Code:
setcharprop #c,New chat text;
this.chat = "New chat text";
setcharprop #n,New nickname;
this.nick = "New nickname";
setcharprop #g,New guild tag;
this.guild = "New guild tag";
Is there a list of all the alternatives to the message codes (#c, etc)?
Yes! There is a list on the GraalBible. You can
find it here.
How do I join together values?
There are a set of operators for this. They are called "
string concatenation" operators. They work like this:
PHP Code:
this.value = "foo" @ "bar";
// producing "foobar"
this.value = "foo" SPC "bar";
// producing "foo bar"
this.value = "foo" TAB "bar";
// producing "foo bar"
this.value = "foo" NL "bar";
// producing "foo
bar"
There MUST be a value on each side of an operator like this. This will not work:
PHP Code:
this.value = "foo" NL;
How do timeouts work in the new engine?
There is a new command in the new engine for setting a timeout. It works like this:
PHP Code:
function onCreated()
{
setTimer(3); // set the timeout to 3 seconds
}
function onTimeout()
{
// this will be called after 3 seconds
}
What are the new engine alternatives to getnpc() and getplayer()?
They are findplayer(), findnpc(), findweapon(), findweaponnpc(). Except, there is now new functionality that allows you to do this:
PHP Code:
findPlayer("Skyld").chat = "foo";
findPlayer("Skyld").clientr.variable = "value";
However, the old with () method still works:
PHP Code:
with (findPlayer("Skyld"))
{
clientr.variable = "value";
chat = "foo";
}
I understand that functions can now take parameters, how does this work?
They absolutely do! Function parameters work in one of two ways. If you define the function with variables, then this is how it will work:
PHP Code:
myFunction("foo", "bar");
function myFunction(temp.test1, temp.test2)
{
// temp.test1 will equal "foo"
// temp.test2 will equal "bar"
}
Also, the params[] array is populated with any parameters given to the function, so in the previous example, params[] would look like this:
PHP Code:
params = {"foo", "bar"};
Function parameters can take any data type (strings, numbers, arrays, object links, etc).
Can functions return values to wherever the function was called?
Yes! The
return; function is used for this, and this is how it is done:
PHP Code:
this.variable = myFunction("foo", "bar");
function myFunction(temp.test1, temp.test2)
{
return temp.test1 @ temp.test2;
}
this.variable will be set to "foobar".
Are there any more reference materials on the new engine?
Yes! You can find them on the GraalBible, at
http://wiki.graal.net/Creation/Dev/GScript.
Opinions/suggestions?
Please post them! Only constructive criticism, opinions or suggestions are welcome, though. Others will be deleted!