PDA

View Full Version : Triggering a function


Imperialistic
07-19-2008, 07:28 PM
This is my first script that actually operates triggeraction.. I'm getting good at GS2 so step back stefan/skyld lmao!


function onActionServerside(){
if(params[0] == "kick"){
if (player.account == "Imperialistic"){
temp.tokens = player.chat.tokenize();
with(findplayer(temp.tokens[1]).setlevel2("graalcitybank_inside_01.nw",30,30));
}
}
}

//#CLIENTSIDE
function onPlayerChats(){
if (player.chat.starts("/kick")){
triggerserver("gui",this.name,"kick");
}
}

cbk1994
07-19-2008, 08:36 PM
You should style your code better, or at least make it consistent.

Here's how you should have styled it, following what you've already done.


function onActionServerside(){
if (params[0] == "kick"){
if (player.account == "Imperialistic"){
temp.tokens = player.chat.tokenize();
findplayer(temp.tokens[1]).setlevel2("graalcitybank_inside_01.nw",30,30);
}
}
}
//#CLIENTSIDE
function onPlayerChats(){
if (player.chat.starts("/kick")){
triggerserver("gui",this.name,"kick");
}
}


You were doing

with(findplayer(temp.tokens[1]).setlevel2("graalcitybank_inside_01.nw",30,30));


This can be shortened to

findPlayer(temp.tokens[1]).setlevel2("graalcitybank_inside_01.nw",30,30);

This is because findPlayer( pl ) becomes that player.

Also, it's a good idea to use findPlayerByCommunityName( pl ) instead. Otherwise, you'll have to use the Graal####### names to kick people.

When you sent the trigger, you didn't send any params and instead tokenized the chat on the serverside part of the script. With a script like this, it's better to tokenize the player's chat and send what you need serverside, in case the players chat changes.


//#CLIENTSIDE
function onPlayerChats()
{
if ( player.chat.starts( "/kick" ) )
{
temp.tokens = player.chat.tokenize();
triggerserver( "gui", name, "kick", tokens[1] );
}
}

(just put that in whatever formatting you normally use)

A few things that don't really make any difference, but can be changed:
- You don't need the temp. prefix in front of a variable if you've already defined this; see how I used that in the above example with temp.tokens.
- When sending the trigger, you don't need to use this.name. You can just use name
- You can give the 'params' names at the start of a function. Here's an example:

function onActionServerSide( cmd, pl )
{
echo( "I received command" SPC cmd SPC "for" SPC pl @ "." );

if ( cmd == "kick" )
{
findPlayerByCommunityName( pl ).setlevel2( "graalcitybank)_inside_01.nw", 30, 30 );
}
}

If you don't know what echo() does, it just puts that text in RC chat.

Hope this helps you a bit, like I said, ignore my styling and use whatever you prefer. If I were to rewrite your script using what I said, it would look like this:


function onActionServerSide( cmd, pl )
{
if ( cmd == "kick" )
{
findPlayerByCommunityName( pl ).setlevel2( "graalcitybank_inside_01.nw", 30, 30 );
}
}
//#CLIENTSIDE
function onPlayerChats()
{
if ( player.chat.starts( "/kick" ) )
{
temp.tokens = player.chat.tokenize();
triggerserver( "gui", name, "kick", tokens[1] );
}
}

07-19-2008, 09:22 PM
I'd do this, personally. However, atleast you're understanding params! Great :):)

function onActionServerSide(temp.option, temp.player) {
if (player.account != "Imperialistic") return false;
switch(option) {
case "kick": findPlayer(player).setlevel2("graal.nw", 30, 30); break;
case "summon": findPlayer(player).setlevel2(player.level.name, player.x, player.y); break;
}
}

//#CLIENTSIDE
function onPlayerChats() {
for (temp.i: ({"kick", "summon"})) {
if (!player.chat.starts("/" @ i)) continue;
triggerserver("weapon", this.name, i, player.chat.substring(i.length() + 1),trim());
break;
}
}


I made a similar code along time ago, please excuse the structure though!
http://forums.graalonline.com/forums/showthread.php?t=73761

Imperialistic
07-19-2008, 10:25 PM
Thanks guys, I'm just starting to understand GS2, the way you guys script is another level for me, But I will study it..

Thanks