Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Frequently asked questions about the new scripting engine (https://forums.graalonline.com/forums/showthread.php?t=65548)

Skyld 04-21-2006 05:14 PM

Frequently asked questions about the new scripting engine
 
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.test1temp.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.test1temp.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!

Malinko 04-21-2006 05:24 PM

Nice!

xXziroXx 04-21-2006 05:59 PM

Great work Skyld, thumbs up!

RefinoheaT 04-27-2006 03:29 AM

Is there/will there be a new commands.txt for gscript2 which explains this kind of stuff?

If not it would be really nice to have a convert commands.txt. For example, one that shows the old way, then shows the new way. It would be extremely useful.

ApothiX 04-27-2006 02:14 PM

Quote:

Originally Posted by RefinoheaT
Is there/will there be a new commands.txt for gscript2 which explains this kind of stuff?

If not it would be really nice to have a convert commands.txt. For example, one that shows the old way, then shows the new way. It would be extremely useful.

wiki.graal.us and wiki.graal.net both provide the 'commands.rtf-esque' documentation for the new engine.

Or you can run Graal4 with the -listscriptfunctions command line option. This will generate a file called docu_scriptfunctionslatest.txt or something along those lines.

xAndrewx 04-27-2006 05:30 PM

Very cool Skyld, you should write a little something about the findnpc() too.

Raeiphon 05-15-2006 06:55 AM

Curious, is there any difference between \n and NL, besides personal preference?

Loriel 05-15-2006 07:17 PM

Quote:

Originally Posted by Skyld
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:

How does the return function return values, then? Clearly if it just returns them, there will be infinite recursion.

Quote:

Please post them! Only constructive criticism, opinions or suggestions are welcome, though. Others will be deleted!
Can you give an unbiased definition of such?

Skyld 05-15-2006 07:29 PM

Quote:

Originally Posted by Raeiphon
Curious, is there any difference between \n and NL, besides personal preference?

\n is used inside strings, where NL is a string concationation operator that should be used between values.
PHP Code:

this.var = "Foo\nBar";
this.var = "Foo" NL "Bar"


AgretTehLeet 06-09-2006 09:14 AM

Skyld I take it you mean concatenation and i'd have just said "string connecting operator"

ZeroTrack 09-22-2006 05:20 AM

I'd look into TAB again skyld, doesn't work...

Skyld 09-22-2006 08:58 AM

Quote:

Originally Posted by ZeroTrack (Post 1220519)
I'd look into TAB again skyld, doesn't work...

Yes it does. :)

ZeroTrack 09-22-2006 01:52 PM

Err then how come
temp.test = "This" TAB "Tabs";
echo(temp.test);

Didn't create a tab, it delimited it fine... But no tab

Skyld 09-22-2006 05:05 PM

1 Attachment(s)
I don't see a problem with it.

It could be that the client F2 window and RC are just not showing tabspaces, but they are being inserted. See the textlist attached.

xAndrewx 09-22-2006 07:09 PM

Quote:

Originally Posted by Skyld (Post 1220686)
I don't see a problem with it.

It could be that the client F2 window and RC are just not showing tabspaces, but they are being inserted. See the textlist attached.

I remember using tab with sendtorc, seemed to work fine... :confused:


All times are GMT +2. The time now is 06:03 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.