Thread: wiki.graal.net/
View Single Post
  #11  
Old 09-07-2006, 08:10 PM
excaliber7388 excaliber7388 is offline
Banned
excaliber7388's Avatar
Join Date: Jul 2005
Location: US
Posts: 5,229
excaliber7388 can only hope to improve
Send a message via AIM to excaliber7388
NPC Code:


Some more detail on actions:
An action is an effective way to use a portion of script more than once, have one script act on another script, more script to serverside from clientside (and the other way) and more.
So how do you use them? Here's the format:
<pre>
triggeraction(x,y,"params[0]","params[1]");
</pre>
You must have two parameters, but you can have as many as you want a quick example:
<pre>
function onCreated()
{
setshape 1,32,32;
}
function onActiontrigger()
{
player.chat="Account: " @params[1]@ " Sword " @params[2]@ " head: " @params[3];
}
//#CLIENTSIDE
function onPlayerchats()
{
if(player.chat=="Who am I?") //amnesia? multiple identities?
{
triggeraction(x,y,"trigger",player.account,player. swordimg,player.headimg);
}
}
</pre>
See? You can make more than one parameter.
Now, there are a few differences between a level NPC with a triggeraction, and a weapon with a triggeraction. Weapons need their location (serverside or clientside) as the first parameter, params[0] is the one after it. Here, you can have just one parameter, and the x and y is always 0 (because the npc server is at 0,0), and you need to include the name of the weapon in the action.
here's an example:
<pre>
//weapon script with a triggeraction
//Weapon name: Fire Ball
function onActionserverside()
{
if(params[0]=="mp")
{
playermp-=10
}
}
//#CLIENTSIDE
function onWeaponfired()
{
if(playermp>=10)
{
shootfireball(playerdir);
triggeraction(0,0,"serverside","Fireball","mp");
}
else
{
player.chat="Not enough mp!";
}
}
</pre>
This nifty little weapon will cost 10 mp per use, and fires a fireball in the direction the player is facing. Hopefully, you can learn to make many weapons of your own!
Triggeractions can be used in shops as well (and these are the scripts people ask for the most).
<pre>
//a shop script, yay!
function onCreated()
{
setshape 1,32,32;
}
function onActionbuy()
{
if(playerrupees>=100)
{
addweapon Fire Ball;
playerrupees-=100;
}
}
//#CLIENTSIDE
function onPlayerchats()
{
if(player.chat=="buy fire ball")
{
triggeraction(x,y,"buy","buy");
//the level npc triggeraction type
}
}
</pre>
There, now you can make a little shop that sells a Fire Ball weapon. Have fun with it, search the commands, there's plenty of easy weapons you can make, and the weapon script is easy to follow and modify as well. Hopefully, you know enough about parameters to make more than one weapon for sale in the same NPC, just make the weapon name part of the action, then check for that parameter before they can buy.
Now there are other types of loops. One I commonly see is a timeout loop. You should understand why. Back with GS1, it was a bit easier to tell what was happening in the script. In GS1 the script for function onTimeout() would be displayed as if(timeout). It was a bit easier to tell that when a timeout was done, it set a flag that if(timeout) would check. function onTimeout does the same thing, however, for new users this may be harder to tell. here's an example of an NPC that uses a timeout loop:
<pre>
//#CLIENTSIDE
function onCreated()
{
settimer(1);
}
function onTimeout()
{
player.chat="Counter: " @this.time;
this.time++;
settimer(1);
//after 1 second, the timeout loop will restart
}
</pre>
This would cause the player to count up every second.
You may have noticed that the variables often start of with client. or this. There are actually a few prefixes, each does a different thing:
<pre>
client. variable is saved to the client, it can be modified on the clientside or serverside, and read from either.
clientr. variables can be rad from server or clientside, but only written from serverside. This is more secure, but only if you use it right.
server. variables are saved to the server, can only be written on the serverside, read on either side
serverr. more secure version of server. read only on clientside
this. is a variable that only functions in the NPC it is in. This prevents NPCs from morifying each others variables
temp. only exists in between the brackets of a function
</pre>
To use scripts effectivly, you should know how to use each of these.
I will add more over time.

Some important commands can be found here:
page:http://wiki.graal.net/index.php/Crea...e#Introduction
Which is also helpful for learning how to script. Enjoy!

--[[User:Excaliber7388|Excaliber7388]] 00:13, 8 May 2006 (CEST)

Reply With Quote