Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Scripting Tutorial for Beginners (https://forums.graalonline.com/forums/showthread.php?t=45560)

GoZelda 06-18-2003 03:08 PM

Scripting Tutorial for Beginners
 
1 Attachment(s)
Okay, i first made this tutorial in a text file, but i thougt it was much more useful to post it here :D

Graal Scripting Tutorial for Beginners v1.0
1. Introduction
2. “if”
3. timeout - *under construction*
4. setting flags and strings - *under construction*
5. triggeraction - *under construction*

1.-Introduction
Well, this is the Graal Scripting Tutorial for Beginners I made!
Here I will explain you certain basics of GScripting. Just check it out. If something is new/important in a example, it is italic or underlined.
Okay, I hope you know how to open the editor. Open Graal, click on training, then when it’s loaded press F4. Now, create a new level. Click on the guy with the black hair and drag it in the field, then click again to place it. Just click on “close” when the window pop-ups. Double-click on a NPC to open the editor. Now, get the NPC to do something!

2.-“if”
The if command is practically the most important in Gscripting.
If something happens, for example the player touches the npc or says something, a certain event will occur. The script system will recognize this, and if you want the npc to do something when this happens, you need the if command, or some of the others that we won’t discuss here. These events are called “flags”.

Okay, go to the empty space and type in

if (playertouchsme)
message Keep your hands off me!;

Now, close the editor and click on “Play” or press P. Now, touch the NPC. It will say something :)
As you see, the event is within brackets (). There always is one bracket.
Now, off for a longer script. The NPC will push you away and take some of your health. If we have a longer script, we put that in an other type of brackets {}.

if (playertouchsme){
message Keep your hands off me!;
hurt 1;
}

Test it once again. Nice, no? Try to make some nice funny thingies with the following commands:
Events/Flags:
playerenters - The player enters the level
playertouchsme - The player touches the NPC
wa**** - The NPC was hit by the players’ sword

Commands:
hurt amount; - Hurt an certain amount of damage
message message; - Make the NPC say something (appear above head)
say2 text; - Make the npc say something. Use #b for a new alinea.
hide; - Hide the npc
show; - show the npc

Remember that all commands need to end with ;

2.2-Two flags
Had fun? Now off to something a bit harder. You can say something yourself by pressing
TAB and then type something, and afterwards press Enter. Press TAB again to continue playing.

if (playerchats&&strequals(#c,hello)){
Message good day!;
}

Go play the level and say “hello”. The NPC will say something back.
Have you noticed that if you open a new bracket, you will have to close it again?

About the Events:
Playerchats is if the player says something
&& means “and”
strequals(#c,hello) means if #c (the chat text of the player) is the same as what is typed after the comma. Try this with other chat texts, too.

Now, we’re gonna get the player a hat!

if (playerchats&&strequals(#c,hat please!)){
message here you go!;
setplayerprop #P1,hat0.png;
}

Play and say “hat please!”. You will get a cool helmet. Now, copy this NPC, but change it a bit. The script must contain strequals(#P1,hat0.png). Make it that if the player touches this new NPC, and he has the helmet, he gets another hat, and that hat is hat19.png, and the NPC must say something.

If done well, it should look like this:
if (playertouchsme&&strequals(#P1,hat0.png)){
message here, a nice crown!;
setplayerprop #P1,hat19.png;
}
That was quite hard, no?
But we go on. Now with variables and sub-flags.

2.3-Variables and sub-flags

if (playertouchsme){
players[index].rupees+=10;
}
if (playerchats&&strequals(#c,buy hat)){
if (playerrupees=>10){
message Fine.;
setplayerprop #P1,hat20.png;
players[index].rupees-=10;
sleep 2;
message ;
}
if (playerrupees=<10){
message You don’t have enough money!;
sleep 3;
message ;
}
}

Now, because this is quite hard, let’s do it slow and clearly.

players[index].rupees+=10;
This will make the money (rupees) of the player go up by 10. You can also use
playerrupees+=10; what is easier and shorter. Players[index].rupees is called a variable. Other useful variables are players[index].x, players[index].y, players[index].darts and players[index].bombs.

players[index].rupees-=10;
The same as above, except that the player loses rupees :D

if (playerrupees=>10){ and if (playerrupees=<10){
if the amount of money of the player is ten or more, the commands in the event will happen. Did you notice that there can be flags IN a flag? It is, that if the player says “buy hat” and he has enough rupees (playerrupees=>10), he gets the hat but loses 10 rupees, but if he hasn’t 10 rupees, (playerrupees=<10) the shopkeeper will say that he hasn’t enough money. This is to make the script much easier etc. We will call this “sub-flags”. Remember that you can also have more sub-flags and still they both work. For example, you can have
if (playertouchsme){
if (players[index].bombs=<50){
players[index].bombs=50;
}
if (players[index].darts=<50){
players[index].darts=50
}
}
and if both bombs and arrows are less then 50 you will get 50 of both, if only the arrows are less then 50 you gain 50 arrows, and if only the bombs are less then 50, you get 50 of those. If you want that if the bombs of the player are less then 50 and the arrows too, you get 50 of both, but if there are less then 50 arrows and more than 50 bombs, you won’t get anything, you should have players[index].darts=<50 and players[index].bombs=<50 linked together in one flag with &&.

sleep 3;
This is for the NPC to wait three seconds. In this seconds, almost nothing happens or can happen. Here it is for the NPC to say something, wait three seconds and then say nothing.

That was hard, no? I hope you still understand it ;-)
Now, there are many other flags and commands. You can find them in the commands next to the editor. Make one NPC that contains all the three practices, but also:
A script if you press A, then you say something.
A script that will say some text with say2 when you throw something against it
A script like in a shop and it sells you a better sword
If the NPC was shot with an arrow, hurt the player

Make another NPC that goes to your weapons when you enter and when you fire it you heal some hearts and the npc gets destroyed. Make the image beer.png



3.-timeout
Timeout is one of the most important commands of GScript. Here is how it works:
if (playerenters){
putleaps 5,x,y;
timeout = 1;
}
if (timeout){
putleaps 5,x,y;
}
As you see, if the player enters you see the water at the place of the NPC. After one second, you see the water again and from then no more. You can use timeout for a loop.

if (playerenters){
putleaps 5,x,y;
timeout = 1;
}
if (timeout){
putleaps 5,x,y;
timeout = 1;
}
Now, the water will come back every time. As you see, timeout can be quite handy =)


I also attached to file in .zip here, it looks a lot better then this.

wonderboysp2p 06-25-2003 03:21 AM

i wish i had something like that when i started scripting...

GoZelda 06-25-2003 12:29 PM

I learned almost everything from looking at other scripts...
And experimenting...

Kaimetsu 06-25-2003 12:50 PM

Y'know, 'if' is not a command.

GoZelda 06-25-2003 01:02 PM

Did i posted somewhere it's a command? :\ It's a program execution thingy. To lazy to check up commands.rtf

protagonist 06-25-2003 02:39 PM

Conditional is what I always called it.

Mykel 06-30-2003 04:14 PM

Re: Scripting Tutorial for Beginners
 
Quote:

Originally posted by GoZelda
if (playerchats&&strequals(#c,hello)){
Message good day!;
}

This helps a lot man, thanks. but make sure you make the M i highlighted lowercase.

GoZelda 06-30-2003 05:50 PM

Ah crap... That's because of the text editor i used x.x

GoZelda 07-05-2003 04:53 PM

Quote:

Originally posted by GoZelda
Did i posted somewhere it's a command? :\ It's a program execution thingy. To lazy to check up commands.rtf
it's
for program execution

Golbez 07-06-2003 03:36 PM

Well I read it I still dont understand time outs, can you explain a bit more?

GoZelda 07-06-2003 04:24 PM

Okay...
timeout = 1;
this means that after 1 second, timeout is set.
then you can use
if (timeout){
//whatever you want;
}
clear?
and inside timeout you can do anoter timeout.

Spark910 07-06-2003 05:55 PM

Quote:

Originally posted by GoZelda
Okay...
timeout = 1;
this means that after 1 second, timeout is set.
then you can use
if (timeout){
//whatever you want;
}
clear?
and inside timeout you can do anoter timeout.


Its useful as sleep does just that, sleep! So if you want to wait a certain time and trigger something, then timeout is good as sleep wont take any extra commands until its 'woke-up'.

So you can do:
NPC Code:

if (created) {
if (timeout) {
//yup;
}

timeout=1;
}




So it goes down when it reads it, asks if 'timeout' is set, and at first it is not, so it ignores all in the timeout part. It then goes down and sees that the timeout is set to 1.

Then it starts at the top again, reads down, this time timeout is set (to 1), so anything inside that is executed, and it will continue as it will go down and read timeout=1; again!

Prob Wrong =/

wonderboysp2p 07-06-2003 09:24 PM

you put the 'if (timeout)' inside of 'if (created)'? weird...

Spark910 07-06-2003 10:20 PM

Quote:

Originally posted by wonderboysp2p
you put the 'if (timeout)' inside of 'if (created)'? weird...
I remember reading somewhere that commands outside of insert_the_scripting_word_I_cant_remember then its bad. Like this:

NPC Code:


if (bob) {
say2 #bBOB!;
}

set bob;


Kaimetsu 07-07-2003 03:38 AM

Commands outside of event blocks are bad. But if(timeout){} is an event block, and nesting event blocks is just as bad.

wonderboysp2p 07-07-2003 09:16 AM

nesting event blocks in other event blocks has it's purposes though

Kaimetsu 07-07-2003 09:27 AM

Quote:

Originally posted by wonderboysp2p
nesting event blocks in other event blocks has it's purposes though
No it doesn't.

konidias 07-07-2003 10:02 AM

Wouldn't this do the same thing as sparks?

NPC Code:

if (created||timeout) {
//yup;
timeout=1;
}



If the npc is created, timeout = 1, if timeout, then loop.

sYnergy247 07-24-2003 12:09 PM

GoZelda..
 
I did the npcs you talked about here is what i came up with

// NPC made by Synergy The (Enchida)
//#CLIENTSIDE
if (keydownA) {
setplayerprop #c,Look at that..I clicked!;
}

^this one i could'nt get... :(

// NPC made by Synergy The (Enchida)
if (waspelt) {
say2 Look at that you hit me!;
}


// NPC made by Synergy The (Enchida)
if (playerchats&&strequals(#c,buy sword)){
if (playerrupees=>100){
message Fine.;
lay goldensword;
players[index].rupees-=100;
sleep 2;
message ;
}
if (playerrupees=<100){
message You don’t have enough money!;
sleep 3;
message ;
}
}

// NPC made by Synergy The (Enchida)
if (wasshot) {
hurt 1;
}

//What do you think?

GoZelda 07-24-2003 12:53 PM

1:
if (keydown(6)){
setplayerprop #c,:o i pressed A!!!;
}
All the others are correct.

sYnergy247 07-24-2003 01:14 PM

6?
 
if (keydown(6)) {

In my commands it has up,down,left,right,a,s,d so on and so forth...

GoZelda 07-24-2003 02:21 PM

Yes, but try using

say2 #k(6);

the order isn't right. Up, down, left, right are the same as dir. and playerdir.


Originally posted by konidias
Quote:

Wouldn't this do the same thing as sparks?
This is used a lot with water splashers and stuff :) But most people use (playerenters), not (created).

Um, Spark. The NPC only gets created once =\ As Kai said, it's the best to not put a timeout inside another event. And with timeout in another timeout i mean:
if (timeout){
timeout = 1;
}
for an infinite loop. Another way of infinite loop:
while (1==1){
//stuff;
sleep 1;
}

Kaimetsu 07-24-2003 02:48 PM

Quote:

Originally posted by GoZelda
As Kai said, it's the best to not put a timeout inside another event. And with timeout in another timeout i mean:
if (timeout){
timeout = 1;
}
for an infinite loop.

Dude, I didn't say anything like that, and that wouldn't create an infinite loop in the traditional sense. Timing out every x seconds is often essential, and certainly not something to shy away from.

GoZelda 07-24-2003 02:59 PM

Oh sorry, read it wrong if you get what i mean. But it is an infinite loop, because when it's timeout it'll always be triggering timeout =\ it won't stop

Kaimetsu 07-24-2003 03:03 PM

Quote:

Originally posted by GoZelda
Oh sorry, read it wrong if you get what i mean. But it is an infinite loop, because when it's timeout it'll always be triggering timeout =\ it won't stop
So? How's that a problem?

GoZelda 07-24-2003 03:57 PM

Did i stated it was a problem?

protagonist 07-24-2003 04:51 PM

Quote:

Originally posted by GoZelda
Oh sorry, read it wrong if you get what i mean. But it is an infinite loop, because when it's timeout it'll always be triggering timeout =\ it won't stop

If you do a variable check and include an increment inside your timeout it will not go forever.

Kaimetsu 07-25-2003 12:53 AM

Quote:

Originally posted by GoZelda
Did i stated it was a problem?
You said it's best not to do it.

GoZelda 07-25-2003 11:22 AM

Quote:

Originally posted by protagonist



If you do a variable check and include an increment inside your timeout it will not go forever.

Yes, but if we don't do that we'll have an infinite one =\ Unless you have it in a function someway and use return; o.0

Eric_1337 07-26-2003 02:49 AM

Quote:

Originally posted by GoZelda
Did i posted somewhere it's a command? :\ It's a program execution thingy. To lazy to check up commands.rtf
I think it's called a 'statement' lol.

Knuckles 08-31-2003 07:04 PM

Quote:

Originally posted by Spark910

...

NPC Code:

if(playertouchsme) {
sleep 2;
setplayerprop #c,Oh, it waited 2 seconds before it set this message!;
}


..This would pause the WHOLE script for 2 second, and then move on.

With timeout, it doesnt pause the script. It continues on.
NPC Code:

if(created) {
timeout = 3;
sleep 1; // 1st second
setplayerprop #c,1;
sleep 1; // This would be the 2nd second
setplayerprop #c,2;
}

if(timeout) setplayerprop #c,3; // On the 3rd second do this.


it will do the first 2 commands (setting playerprop, because those are done within the first 2 seconds. And then it will call the timeout, and do whatevers in there.)

I think I explained it right.. it might be alittle confusing though ^_^.

Tseng 09-14-2003 04:52 PM

I dislike reviving threads, but as this one is linked from GoZelda's signature, I felt it necessary to point this out:

In his 'tutorial', he is using => instead of >=. This is a bad thing to do, as it's wrong. The engine's too forgiving of bad code.

So, don't use =>. Use >=. It's pronounced "Greater than or equal to," surprisingly exactly like you learned how to pronounce it in a basic algebra (or even basic math) class!

GoZelda 09-14-2003 04:58 PM

Quote:

Originally posted by Tseng
I dislike reviving threads, but as this one is linked from GoZelda's signature, I felt it necessary to point this out:

In his 'tutorial', he is using => instead of >=. This is a bad thing to do, as it's wrong. The engine's too forgiving of bad code.

So, don't use =>. Use >=. It's pronounced "Greater than or equal to," surprisingly exactly like you learned how to pronounce it in a basic algebra (or even basic math) class!

Oh crap...
I always forget whether the > or the = goes first.

So, everyone out there, the > or < comes first!


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

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