PDA

View Full Version : Learning GS2: A Tailor Script


_Z3phyr_
05-26-2006, 08:48 AM
If you do not like the forum format, check this site out for a website format of this post: - http://www.freewebs.com/zephyrtk/gs2articles/art01GS2.html
The link to the original post of this in the Delterian Forums is here:
- http://forums.delteria.com/index.php?showtopic=571&st=0&p=7221&#entry7221


This thread is made for those who are completely new to GS2, but are assumed to be at least familiar with GS1, or old GScript. Sort of a way for GS1 scripters to familiarize themselves with this new monster... except its in plain English and makes it all easy to understand. The following is an example of a GS2 Tailor Script created solely by me from Scratch, and declared correct by scripters Riot, Ajira, Maniaman, and MisconceptioN.

I hope that the following will help you learn GS2, if you are not already smart about it:

-=-=-



Here's how I'll work this:
I will post the old GScript (if I bothered to convert it somehow) and then the new one. In this post, I have the old one and I'll post it first and do a fast brief on how it works, then do the new GScript and then put the effort onto explaining that.

TAILOR SCRIPT
BEFORE YOU READ THE SCRIPT, please keep in mind that there are many many ways to make GScript do whatever you want to. The glass is half full, or the glass is half empty. They both mean the same thing, but they use different ways of explaining it. The same applies to GScript and any other computer or foreign language you ever might want to learn.

In these two scripts, the function is for the player to say "/set clothes/tunic PART COLOR."

ALSO, in GS2 I was told that the functions "setPARTcolor()" is deprecated (meaning they're old and there's better/easier ways to do them), but for the purposes of this I'm doing this so you can relate the Old to the New GSCript.



// OLD GSCRIPT
if (created) {
setstring this.parts,"skin","coat","sleeves","shoe","belt";
}
if (playerchats) {
tokenize #c;
if (startswith(/set,#c)) {
if (strequals(#t(1),clothes) || strequals(#t(1),tunic)) {
for (i=0;i<=strlen(this.parts);i++) {
if (strequals(#t(2),#I(this.parts,#v(i)))) {
setskincolor #t(3);
//setskincolor is the only one I listed just as an example rather than type it all out.
} else {
message Invalid Clothing part/color!;
}
}
}
}
}
//~Michael Lee, 05.11.05


Brief description of the above OLD GSCRIPT:
I will assume that you know how the basic syntax works and what "playerchats/startswith/strequals" and all that crap means...
This script tokenizes what the player says and checks to see if certain phrases are met before they set the part. They use (startswith(/set,#c)) to check if the player is trying to use a /set command, then checks everything after it to see what the player is trying to set (in this case, the player is trying to change his clothing colors). Everything after that checks and sets the player's request accordingly (Note that "all" isn't set in this script).



// NEW GSCRIPT
function onPlayerchats() {
tokens = player.chat.tokenize();
if (tokens[0] == "/set") {
if (tokens[1] in {"clothes","tunic"}) {
switch (tokens[2]) {
case "skin": setskincolor(tokens[3]); break;
case "coat": setcoatcolor(tokens[3]); break;
case "sleeve": setsleevecolor(tokens[3]); break;
case "shoe": setshoecolor(tokens[3]); break;
case "belt": setbeltcolor(tokens[3]); break;
default : message("**Error: Invalid part**"); break;
}
}
}
}
//~Michael Lee 05.11.05


Brief description of the above NEW GSCRIPT:

First off, everything in GS2 is in functions. When in GS1 you would say:
if (created) { ... }
in GS2 you would say:
function onCreated() { ... }
Here, where in GS1 you would say:
if (playertouchsme) { ... }
in GS2 you would say:
function onPlayertouchsme() { ... }
This rule applies to all built-in conditions. Also note that "offCreated" or "offPlayertouchsme" don't exist but I can't tell you that 100% even though it makes sense. A near-but-not-entirely-complete list of all default built-in functions are listed in the Graal Wiki (http://wiki.graal.net/index.php/Main_Page).

Secondly, the next line in the GS2 script:
tokens = player.chat.tokenize();
is the equivalent to the GS1 command:
tokenize #c;
In many programming languages like JavaScript or C++, you can perform functions to variables AS you call them or even set them!
In GS2, you use the tokenize(); function at the end of "player.chat" in order to tokenize the variable "player.chat" (which is the equivalent of #c in GS1). As for the variable "tokens," it is an array. Instead of (in GS1) saying "#t(NUMBER)", you can (in GS2) say "tokens[NUMBER]" to do the exact same thing (same rules apply - 0 is the first word, 1 is the second, etc).

Thirdly, the actual checking of the tokens in the new GS2 is simple if you understand what I just said above. The function of this script is so that the player can change his or her clothing colors by saying /set PART COLOR. the first word this player should say is "/set" which should be equal to tokens[0] (or in GS1, "#t(0)" ).

if (tokens[0] == "/set") {
...
}

The same applies to the other words, where tokens[1] (or, #t(1) in GS1) is the name of the part and tokens[2] (or, #t(2) in GS1) is the name of the color.

if (tokens[1] in {"clothes","tunic"}) {
switch (tokens[2]) {
case "skin": setskincolor(tokens[3]); break;
case "coat": setcoatcolor(tokens[3]); break;
case "sleeve": setsleevecolor(tokens[3]); break;
case "shoe": setshoecolor(tokens[3]); break;
case "belt": setbeltcolor(tokens[3]); break;
default : message("**Error: Invalid part**"); break;
}
}

I will assume that you know how a switch function works in scripting. If you don't, check this out (http://www.w3schools.com/php/php_conditionals.asp) for an example of how it works in PHP.

The part about "in {"clothes","tunic"}" simply means that if tokens[1] is one of the things in the bracket signs { } (either "clothes" or "tunic", then it will be true). You can have one thing (although that wouldn't be the most efficient way of doing it...), two things, five things, whatever...

Now this GS2 tailor script doesn't check to see if you supply the right colors, only if you supply the correct part name (as long as it's listed in the "case" section, but you can always add more on the off-chance that more parts can be changed in the game in the future). If you give the incorrect part name, then it says:

default : message("**Error: Invalid part**"); break;

Note that in GS2, the function "message();" is the same as saying "message msghere; in GS1.


SIDENOTE: When, like in the message(); command, you want to not show a variable, you do not put the variable in quotes. To make that work, you end the quotes and then put in a special symbol (in the GraalWiki, it's called the "concat" short for concatenating (http://dictionary.reference.com/search?q=concatenating) function but that's nothing more than a fancy word for "combine" and the correct symbol is "@" the "at" sign like in an e-mail address).

QUICK EXAMPLE OF HOW TO USE THE @ (CONCAT) SIGN:
message("You currently have" @ player.hp @ "HEARTS");
the @ sign is used as a break between the constant text (which is in quotes) and the variable (player.hp). The basic rule for this is that when you have something in quotes, the script will read the text as text ONLY. If you put "player.hp" in quotes then it will say "player.hp" and think that you're just saying the word, not the variable.




Hope this helps.




-=-=-

If there are any questions whatsoever, please direct them to [email protected] . I might not be able to respond easily on these forums because I don't have an active Gold account so I can't post whenever I feel like it when I'm here. Always in moderation, right?

ApothiX
05-26-2006, 03:28 PM
A more efficient way to write a taylor script might be like:


function onCreated() {
// Don't know the order of the partnames in the colors[] array
// You'll have to find this bit out for yourself.
this.partslist = { "skin", "coat", "sleeves", "shoes", "belt" };
}

function onPlayerChars() {
if(player.chat.starts("/set")) {
temp.tokens = player.chat.substring(4,-1).tokenize();

if(this.partslist.index(temp.tokens[0]) >= 0) {
player.colors[this.partslist.index(temp.tokens[0])] = temp.tokens[1];
} else {
player.chat = "Invalid Body Part";
}
}
}

_Z3phyr_
06-11-2006, 09:45 AM
Thirdly, the actual checking of the tokens in the new GS2 is simple if you understand what I just said above. The function of this script is so that the player can change his or her clothing colors by saying /set PART COLOR. the first word this player should say is "/set" which should be equal to tokens[0] (or in GS1, "#t(0)" ).

if (tokens[0] == "/set") {
...
}

The same applies to the other words, where tokens[1] (or, #t(1) in GS1) is the name of the part and tokens[2] (or, #t(2) in GS1) is the name of the color.

if (tokens[1] in {"clothes","tunic"}) {
switch (tokens[2]) {
case "skin": setskincolor(tokens[3]); break;
case "coat": setcoatcolor(tokens[3]); break;
case "sleeve": setsleevecolor(tokens[3]); break;
case "shoe": setshoecolor(tokens[3]); break;
case "belt": setbeltcolor(tokens[3]); break;
default : message("**Error: Invalid part**"); break;
}
}


Correction, since I can't edit my post for some reason:

The function of the script is REALLY to say /set clothes_or_tunic PART COLOR.

I said this at the top of the post, but changed it there. My fault.

tokens[0] (#t(0)) is /set
tokens[1] (#t(1))is either clothes OR tunic
tokens[2] (etc) is PART
tokens[3] is COLOR


and btw the script the above poster made is good and all if you're fluent in GS2, but this tutorial is about teaching GS1 scripters how to do GS2, so yeah - teach it by relating one to the other.

xXziroXx
06-11-2006, 01:14 PM
Or even a better way to make a tailoring script..


function onPlayerChats()
{
tokens = player.chat.tokenize();
BodyParts = { "skin", "coat", "sleeves", "shoes", "belt", "all"};
BodyColors = { "white", "yellow", "orange", "pink", "red",
"darkred", "lightgreen", "green", "darkgreen", "lightblue",
"blue", "darkblue", "brown", "cynober", "purple",
"darkpurple", "lightgray", "gray", "black"};
if (BodyParts.index( tokens[0]) > -1 && BodyColors.index( tokens[1]) > -1) {
if (tokens[0] != "all") player.colors[ BodyParts.index( tokens[0])] = tokens[1];
else {
for (i: BodyParts) player.colors[ BodyParts.index( i)] = tokens[1];
}
player.chat = "Changed " @ tokens[0] @ " to " @ tokens[1] @ "!";
}
}




HOWEVER, good job with the tutorial!

ApothiX
06-12-2006, 06:42 AM
Or even a better way to make a tailoring script..


function onPlayerChats()
{
tokens = player.chat.tokenize();
BodyParts = { "skin", "coat", "sleeves", "shoes", "belt", "all"};
BodyColors = { "white", "yellow", "orange", "pink", "red",
"darkred", "lightgreen", "green", "darkgreen", "lightblue",
"blue", "darkblue", "brown", "cynober", "purple",
"darkpurple", "lightgray", "gray", "black"};
if (BodyParts.index( tokens[0]) > -1 && BodyColors.index( tokens[1]) > -1) {
if (tokens[0] != "all") player.colors[ BodyParts.index( tokens[0])] = tokens[1];
else {
for (i: BodyParts) player.colors[ BodyParts.index( i)] = tokens[1];
}
player.chat = "Changed " @ tokens[0] @ " to " @ tokens[1] @ "!";
}
}




HOWEVER, good job with the tutorial!
I'd prefer my version of it.

You are limiting what types of colors they can change to. In Graal you can also change your color to a number instead of the color name.
Those annoying spaces between the opening parenthesis and the first parameter. Why the HELL do you and contiga do that? It is the stupidest thing I've ever seen, and is beyond annoying to read.

xXziroXx
06-12-2006, 03:09 PM
I'd prefer my version of it.

You are limiting what types of colors they can change to. In Graal you can also change your color to a number instead of the color name.
Those annoying spaces between the opening parenthesis and the first parameter. Why the HELL do you and contiga do that? It is the stupidest thing I've ever seen, and is beyond annoying to read.


Blame Contiga for showing me that stupid way of styling back on Endora!

ApothiX
06-13-2006, 01:11 AM
Blame Contiga for showing me that stupid way of styling back on Endora!
I have been. Do the honorable thing and drop it!

Python523
06-13-2006, 10:37 AM
I have been. Do the honorable thing and drop it!
Eh, it is a perfectly valid way of formatting. Some people[or a lot of programmers of other prog. languages] prefer his way, I believe. For example, Rick uses that way . Everyone has their own way of doing it... as long as it is [I]some kind of standard, it's alright.

ApothiX
06-14-2006, 08:27 AM
Eh, it is a perfectly valid way of formatting. Some people[or a lot of programmers of other prog. languages] prefer his way, I believe. For example, Rick uses that way . Everyone has their own way of doing it... as long as it is [I]some kind of standard, it's alright.
No, a lot of programmers put spaces between the first AND last parenthesis. Contiga and Ziro only put a space between the first parenthesis and the parameter list.


Normal way:foo( bar );
Their way:foo( bar);

Python523
06-14-2006, 10:25 AM
No, a lot of programmers put spaces between the first AND last parenthesis. Contiga and Ziro only put a space between the first parenthesis and the parameter list.


Normal way:foo( bar );
Their way:foo( bar);
Oh, I didn't even notice that. That's just a little bit weird, but you must be pretty picky.

_Z3phyr_
06-14-2006, 10:55 AM
*re-rail*

So, I'm sure this thread along with its wonderful contributions by the above posters will help people who aren't familiar with GS2, but are familiar with GS1 learn the language, right? That's the purpose of this thread, after all...


(btw about y'all's argument/debate/whatever...

BEFORE YOU READ THE SCRIPT, please keep in mind that there are many many ways to make GScript do whatever you want to. The glass is half full, or the glass is half empty. They both mean the same thing, but they use different ways of explaining it. The same applies to GScript and any other computer or foreign language you ever might want to learn.

hope that contributes to the ending of it and on with the purpose of the thread, which is to introduce a methodology of GS2 learning by translation and explanation so that the scripter can learn through relation, which is the method that I am learning)

D4rKv310c1ty
06-15-2006, 02:59 PM
I swear someone said something about a GS1 to GS2 script convertor.

Why don't you just stick to learning GS1 and convert it all :D
and then learn the scripts which actually do require GS2

Skyld
06-15-2006, 05:51 PM
I swear someone said something about a GS1 to GS2 script convertor.

Why don't you just stick to learning GS1 and convert it all :D
and then learn the scripts which actually do require GS2
Because it makes so much more sense to skip old gscript completely and learn the new engine. Old gscript is a very illogical language at times; it is easier to learn the new engine if you just ignore the old one.

I guess the old engine won't survive for long with Graal 4 being widely spread out anyway.

ApothiX
06-16-2006, 08:05 AM
Oh, I didn't even notice that. That's just a little bit weird, but you must be pretty picky.
Perhaps, but it just drives me when they're doing it with arrays and such.
ie:
function SomeFunction( foo, bar) {
this.myarray = new[ 1];
this.myarray[ 0].add( "weeEe");
echo( this.myarray[ 0][ 0]);
}

Rick
06-16-2006, 09:10 AM
They might as well just do this.

function SomeFunction(foo,bar){this.myarray=new[1];this.myarray[0].add("weeEe");echo(this.myarray[0][0]);}

contiga
06-16-2006, 10:01 AM
They might as well just do this.

function SomeFunction(foo,bar){this.myarray=new[1];this.myarray[0].add("weeEe");echo(this.myarray[0][0]);}


Indeed, then my style is a lot better! >_< Anyway.. I think the fact that I do:

function SomeFunction( foo, bar) {
//foo
}

Is because I'm used to it in writing also.. the word, then the comma attached to it, a space and the next word..

xXziroXx
06-16-2006, 03:15 PM
Just a random post; I no longer style as the foul demon Contiga!

ApothiX
06-16-2006, 03:22 PM
They might as well just do this.

function SomeFunction(foo,bar){this.myarray=new[1];this.myarray[0].add("weeEe");echo(this.myarray[0][0]);}
Both styles are bad, does not mean that that one style cancels out how horrible contiga's is.

Indeed, then my style is a lot better! >_<
that's arguable.

Anyway.. I think the fact that I do:

function SomeFunction( foo, bar) {
//foo
}

Is because I'm used to it in writing also.. the word, then the comma attached to it, a space and the next word..
In English you don't put a space after the parenthesis (and if you do, you put one before the last parenthesis aswell, It's about being symmetrical.)

Just a random post; I no longer style as the foul demon Contiga!
You're one step closer to being a GScript-Master!!

contiga
06-16-2006, 03:49 PM
You're one step closer to being a GScript-Master!!
Lol, so with this argument, you call yourself more a GScript-Master then people that use my style?
Noob plx, everyone has his own style... I think my scripts look more organized than yours >_<

Bl0nkt
06-16-2006, 07:53 PM
Holy ****, way to highjack this thread.

Python523
06-16-2006, 08:10 PM
Holy ****, way to highjack this thread.
Threads evolve, man.

ApothiX
06-17-2006, 02:12 AM
Lol, so with this argument, you call yourself more a GScript-Master then people that use my style?
It's called a joke, son.

Noob plx, everyone has his own style... I think my scripts look more organized than yours >_<
Except yours have random spaces appearing out your ass, and no symmetry on the other side. That, my friend, is what we call horribly formatted code.

_Z3phyr_
06-17-2006, 03:04 AM
I can tell nobody cares about teaching GS2 to people who don't know it.

gtfo my thread.

ApothiX
06-17-2006, 04:40 AM
I can tell nobody cares about teaching GS2 to people who don't know it.

gtfo my thread.
gtfo my forums ;) *waits for people to take this seriously and spam with "THESE ARENT YOUR FORUMS NOOB"*

Loriel
06-17-2006, 05:17 AM
I can tell nobody cares about teaching GS2 to people who don't know it.
I do not think you have any reason to come to this conclusion. People just want to teach proper formatting while they are at it.

gtfo my thread.
It is not your thread just because you posted the first post. The thread belongs to the scripting community!

Python523
06-17-2006, 09:13 AM
I can tell nobody cares about teaching GS2 to people who don't know it.

gtfo my thread.
Dude, sorry if the thread got a little 'off topic', but it was just the natural progression of it. I'm a mod, and even I said it's okay.

ApothiX
06-17-2006, 09:20 AM
Dude, sorry if the thread got a little 'off topic', but it was just the natural progression of it. I'm a mod, and even I said it's okay.
Jagen for President.

Like Loriel said, though. The discussion was still related to gscript, it was just more the aesthetical aspect of it.