Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   AP Changer Script :) (https://forums.graalonline.com/forums/showthread.php?t=134267011)

Bleachlover551 08-19-2012 01:59 AM

AP Changer Script :)
 
Hope You Like ^^

PHP Code:

//Script By Toshi!
//#CLIENTSIDE
function onCreated() {
  new 
GuiTextCtrl("MyGUI_TextEdit1") {
    
profile GuiBlueTextProfile;
    
81;
    
5;
    
height 20;
    
text "";
  }
}
function 
onMouseDown(mode) {
  if (
mode == "Left") {
    for (
plplayers) {
      if ( 
mousex in pl.0.5pl.2.5| && mousey in pl.ypl.3|) {
        if (
pl != NULL) {
           new 
GuiWindowCtrl("MyGUI_Window1") {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "115,126";

    
canmaximize false;
    
canminimize false;
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "AP Changer";
    
100;

    new 
GuiButtonCtrl("MyGUI_Button1") {
      
profile GuiBlueButtonProfile;
      
text "Ap = 0";
      
width 115;
    }
    new 
GuiButtonCtrl("MyGUI_Button2") {
      
profile GuiBlueButtonProfile;
      
text "Ap = 50";
      
width 115;
      
29;
    }
    new 
GuiButtonCtrl("MyGUI_Button3") {
      
profile GuiBlueButtonProfile;
      
text "Ap = 100";
      
width 115;
      
58;
    }
    new 
GuiTextEditCtrl("MyGUI_TextEdit1") {
      
profile GuiBlueTextEditProfile;
      
height 20;
      
width 115;
      
0;
      
106;
      
MyGUI_TextEdit1.text player.nick;
    }
      new 
GuiTextEditCtrl("MyGUI_TextEdit2") {
      
profile GuiBlueTextEditProfile;
      
height 20;
      
width 115;
      
0;
      
88;
      
MyGUI_TextEdit2.text player.ap;
    }
  }
}
}
function 
MyGUI_Button1.onAction() {
player.ap "0";
}

function 
MyGUI_Button2.onAction() {
player.ap "50";
}

function 
MyGUI_Button3.onAction() {
player.ap "100";
}
        }
      }
    } 


PeterGraal 08-19-2012 02:02 AM

<3 Thanks. Is this the default one, or your own version?

ffcmike 08-19-2012 02:08 AM

This is completely wrong, you're modifying the normal player object rather than the target player. AP is not possible to properly modify Clientside, you need to trigger to Serverside or else it will not save or appear to other players.

Bleachlover551 08-19-2012 02:23 AM

ah i see hangs on....wait....so would look like something like

Example: This?

triggerserver("gui",this.name,"ap=50",MyGUI_TextEd it1.text)

Quote:

Originally Posted by ffcmike (Post 1702072)
This is completely wrong, you're modifying the normal player object rather than the target player. AP is not possible to properly modify Clientside, you need to trigger to Serverside or else it will not save or appear to other players.


cbk1994 08-19-2012 02:29 AM

Quote:

Originally Posted by Bleachlover551 (Post 1702075)
ah i see hangs on....wait....so would look like something like

Example: This?

triggerserver("gui",this.name,"ap=50",MyGUI_TextEd it1.text)

No:

Quote:

Originally Posted by cbk1994 (Post 1579584)
Ehh...not really.

There are essentially two different ways you script: on serverside, or on clientside. If a script is serverside, it means that the NPC-server interprets and performs the code. The advantage to this is that you know the code can't be tamperered with. If the code is clientside, it means that it is ran on the computer of the player. This has the advantage of being faster and not requiring communication with the server, but has the disadvantage of being less secure.

Since serverside is not performed for a specific player it cannot display interface graphics (HUDs, etc). It also can't handle player chat events, among others.

To communicate between the server and the client you use something called a trigger. In the past this was accomplished with the triggerAction function, but now it's standard practice to use triggerServer and triggerClient. Simply enough, triggerServer is used to send a "trigger", or a message, from clientside (a player's computer) to the NPC-server. Similarly, triggerClient is used to send a "trigger" from the NPC-server to a player's computer.

The simplest trigger from clientside to server would work like so:

PHP Code:

function onActionServerSide() {
  echo(
"Ping received!");
}

//#CLIENTSIDE
function onCreated() {
  
triggerServer("gui"this.namenull);


In your case you would want to send the player's chat serverside. The onPlayerChats event is called on clientside whenever the player chats.

PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  
// code


You can then check if the player's chat starts with "/copy" by using the string function str.starts(str). This would be "true" if the string starts with the parameter given.

PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
// code
  
}


Now, you need to send a trigger to the server with the triggerServer command. triggerServer and triggerClient both have three parameters (two on the latest beta of Graal or for triggerClient): script type, script name, param1[, param2...].]

The script types that you can trigger are "gui" or "weapon", which both trigger a weapon script. Scripters have their own personal preference on which to use, but it doesn't really matter. The other script type is "npc", which triggers a database NPC. If you don't know what that is, don't worry about it yet.

Adding the trigger code would look like so:

PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
triggerServer("gui"this.namenull);
  }


this.name is simply referring to the current name of the weapon. It's generally preferred to use this instead of typing the weapon name in case the name were to change.

Then, you would need to "tokenize" the player's chat. This is the same as the explode function found in PHP and other languages. It splits the string into different parts at the "delimiter". You can specify a custom delimiter for str.tokenize(str delimiter), but in this case there is no need to.

PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
temp.tokens player.chat.tokenize();
    
triggerServer("gui"this.nametokens);
  }


Now you just need to build the serverside portion of the script. This part of the script will need to identify the command given, identify the player wanted, and the property to copy. You would start by adding the onActionServerSide() event. This is always called when using triggerServer. The parameter you specified in the trigger ("tokens") will be in the first parameter for that function.

PHP Code:

function onActionServerSide(tokens) {
  
// code
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
temp.tokens player.chat.tokenize();
    
triggerServer("gui"this.nametokens);
  }


You can then check for the command and find the player

PHP Code:

function onActionServerSide(tokens) {
  if (
tokens[0] == "/copy") {
    
temp.pl findPlayerByCommunityName(tokens[1]);
  }
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
temp.tokens player.chat.tokenize();
    
triggerServer("gui"this.nametokens);
  }


Now all you need to do is identify the property which is to be copied.

PHP Code:

function onActionServerSide(tokens) {
  if (
tokens[0] == "/copy") {
    
temp.pl findPlayerByCommunityName(tokens[1]);
    
    if (
tokens[2] == "body") {
      
player.body pl.body;
    }
  }
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("/copy")) {
    
temp.tokens player.chat.tokenize();
    
triggerServer("gui"this.nametokens);
  }


There's a better way to do that involving dynamic variables but I feel like I've already thoroughly confused you with this post. As I said before, it seems like you're jumping in too fast and you're just going to get lost. Focus on the basics first.

Quote:

Originally Posted by cbk1994 (Post 1555017)
You can trigger either way back and forth as much as you want. You can create an infinite loop if you want:

PHP Code:

function onActionServerSide(cmd) {
  if (
cmd == "ping") {
    
triggerClient("gui"this.name"ping");
  }
}

//#CLIENTSIDE
function onCreated() {
  
triggerServer("gui"this.name"ping"); // start the loop
}

function 
onActionClientSide(cmd) {
  if (
cmd == "ping") {
    
this.counter ++;
    
player.chat "Pings: " this.counter;
    
triggerServer("gui"this.name"ping");
  }


Though obviously you wouldn't want to do something like that in practical scripting :p

You can also trigger weapons of other players.

PHP Code:

function onActionServerSide(cmdtargetmsg) {
  if (
cmd == "tell") {
    
temp.pl findPlayerByCommunityName(target);
    
    if (
pl == null) {
      return 
player.chat "(player not found)";
    }
    
    
pl.triggerClient("gui"this.name"tell"player.communitynamemsg);
  }
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("tell")) { // format is: tell account "message here"
    
temp.tokens player.chat.tokenize();
    
    
temp.acc tokens[1];
    
temp.msg tokens[2];
    
    
triggerServer("gui"this.name"tell"accmsg);
  }
}

function 
onActionClientSide(cmdsendermsg) {
  if (
cmd == "tell") {
    
player.chat sender ": " msg;
  }


To use this you would say: tell cbk1994 "Hi there ugly!"

That would trigger serverside, try to find a player with the community name 'cbk1994', and if found, trigger clientside on the same weapon for that player. Once it receives the trigger clientside, it would set my chat to 'Engine: Hi there ugly!'.

This is a bit of a silly example since you can set players' chat serverside, but it could be used, for example, if you have some kind of GUI window displaying the message.

I'm pretty sure I've pointed you towards these before, and I know I've pointed you toward Jer's scripting guide before.

Also, name your GUI controls properly.

ffcmike 08-19-2012 02:29 AM

Quote:

Originally Posted by Bleachlover551 (Post 1702075)
Example: This?

triggerserver("gui",this.name,"ap=50",MyGUI_TextEd it1.text)

Assigning variables doesn't work like this, you just need to pass 50 as a parameter, and then do something like:

PHP Code:

function onActionServerSide(temp.aptemp.account){
  
temp.pl findplayer(temp.account);
  if(
temp.pl != NULL){
    
temp.pl.ap temp.ap;
  }



Tim_Rocks 08-19-2012 11:45 AM

I would try using a slider in this case, which would allow having more AP options without having to create multiple buttons, then trigger server to actually set it as Chris stated.
PHP Code:

new GuiSliderCtrl("AP_Slider") {
  
profile GuiBlueSliderProfile;
  
10;
  
10;
  
width 160;
  
height 20;
  
range = {0,100};
  
value 30;
  
ticks 10;



Gunderak 08-21-2012 08:48 AM

Quote:

Originally Posted by Tim_Rocks (Post 1702094)
I would try using a slider in this case, which would allow having more AP options without having to create multiple buttons, then trigger server to actually set it as Chris stated.
PHP Code:

new GuiSliderCtrl("AP_Slider") {
  
profile GuiBlueSliderProfile;
  
10;
  
10;
  
width 160;
  
height 20;
  
range = {0,100};
  
value 30;
  
ticks 10;



Good idea.
Also I know it's been pointed out already, but changing AP clientside doesn't work.
If you want a simple method of a triggerserver already done then here ya go.
On the clientside when you set the ap.
Which means below //#CLIENTSIDE
PHP Code:

triggerserver("weapon"this"AP"AP_Slider.value); 

Then above //#CLIENTSIDE put this.
PHP Code:

function onActionServerSide(){
  if(
params[0] == "AP"){
    
player.ap params[1];
  }


Probably should of used switch and case but meur.

cbk1994 08-21-2012 10:01 AM

Quote:

Originally Posted by Gunderak (Post 1702234)
Probably should of used switch and case but meur.

There's no reason to use a switch statement here.

Emera 08-21-2012 12:29 PM

Quote:

Originally Posted by cbk1994 (Post 1702239)
There's no reason to use a switch statement here.

Personal preference and that's about it <3

xXziroXx 08-21-2012 12:38 PM

Using a switch for params is always a great idea. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. Has very little to do with personal preference, and more to do with optimization.

Crow 08-21-2012 01:58 PM

Quote:

Originally Posted by xXziroXx (Post 1702245)
Using a switch for params is always a great idea. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. Has very little to do with personal preference, and more to do with optimization.

Not going to use a switch statement for only one or two possibilities.

Emera 08-21-2012 02:20 PM

Quote:

Originally Posted by Crow (Post 1702247)
Not going to use a switch statement for only one or two possibilities.

Personally, I always use it regardless of how many possibilities I have <3 It just looks cleaner to me.

ffcmike 08-21-2012 07:42 PM

PHP Code:

triggerserver("weapon"this"AP"AP_Slider.value); 

While using 'this' works, it is incorrect as it's a reference to the weapon object rather than the name of the weapon, it should be 'this.name'.

cbk1994 08-21-2012 10:18 PM

Quote:

Originally Posted by xXziroXx (Post 1702245)
Using a switch for params is always a great idea. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. Has very little to do with personal preference, and more to do with optimization.

Personal preference and code readability (clarity) are the only things that matter here. The difference in performance between a switch statement and an if-statement for a couple of conditions is going to be ridiculously trivial and totally negligible.

With that said, I have nothing against switch statements, I just didn't want OP to feel that they were needed every time you need to check a condition.

edit: in fact, at 100,000 iterations, an if loop just barely wins (this is repeatable):

Quote:

if: 0.023976087
switch: 0.026674032
PHP Code:

this.maxlooplimit 100000;
temp.foo "foo";

// test if
temp.start timevar2;

for (
temp.0temp.this.maxlooplimittemp.++) {
  if (
temp.foo == "bar") {
    
// bar
  
} else if (temp.foo == "foo") {
    
// foo
  
} else {
    
// neither
  
}
}

echo(
"if: " @ (timevar2 temp.start));

// test switch
temp.start timevar2;

for (
temp.0temp.this.maxlooplimittemp.++) {
  switch (
temp.foo) {
    case 
"bar":
      
// bar
    
break;
    
    case 
"foo":
      
// foo
    
break;
    
    default:
      
// neither
    
break;
  }
}

echo(
"switch: " @ (timevar2 temp.start)); 

...but again, these differences are completely negligable.

Gunderak 08-24-2012 08:12 AM

Lol, that was actually quite a clever idea to do a test.
Good job, but how do we know that when performing the test the CPU usage was exactly the same?
The slightest inconsistency could render the results useless.

cbk1994 08-24-2012 01:09 PM

Quote:

Originally Posted by Gunderak (Post 1702444)
The slightest inconsistency could render the results useless.

Which is why I said that such a small difference is totally negligible and that the only factors you should take into account when choosing between a switch statement and an if-statement are things like code maintainability and readability. Neither an if-statement nor a switch statement are going to be your bottleneck.

Gunderak 08-27-2012 11:24 AM

Fair enough.
Yay neg rep :D
Thanks whoever did it aha
I'm hoping to get two bars of neg rep ahah

Gunderak 08-29-2012 12:35 AM

Accidental bump...
But still, whoever. Neg repped "Your wish is my command", thankyou!
That actually made me laugh.

cbk1994 08-29-2012 12:39 AM

Quote:

Originally Posted by Gunderak (Post 1702752)
Accidental bump...
But still, whoever. Neg repped "Your wish is my command", thankyou!
That actually made me laugh.

Stop spamming the development forums. Nobody cares about your rep. All these kind of posts do is clutter up these threads and make it harder for people to find what they're looking for.

Gunderak 08-29-2012 12:41 AM

But it will also get me more neg rep. Which is my aim 0.o
I honestly don't even play graal anymore lol.


All times are GMT +2. The time now is 04:00 PM.

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