Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Weapon Control (https://forums.graalonline.com/forums/showthread.php?t=134264233)

Emera 08-17-2011 12:03 AM

Weapon Control
 
1 Attachment(s)
I keep getting very annoyed of adding the same line to add my weapons at the top of my scripts.
PHP Code:

findplayer("Graal772919").addweapon(this.name); 

So I decided to knock me up a GUI to help me along. All it does is add and remove weapons from Your player only. I was wondering, since it does what it needs to do, and it helps me along, is there any way you guys can help me improve, shorten or simplify the script?

The Script (-System/WepCtrl)
PHP Code:

/* Uses two lists to show two categories of weapons. 
List one shows all weapons. Hidden (-Weapon/Name) and non-hidden (Weapon/Name)
List two shows only the non-hidden weapons (Weapon/Name)
To add or remove the weapons;
Type the name of the weapon into the add or remove box and hit the button.
Tap the button twice to update the list.
Opening the GUI is easy. Just say "/wepcontrol"
*/
function onActionServerSide() {
  if (
params[0] == "addweaponwep"addweapon(params[1]); //Add the weapon.
  
if (params[0] == "takeweaponwep"removeweapon(params[1]); //Take the weapon.
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat == "/wepcontrol") {
    
player.chat "";
    
WEP_Window1.visible true//Open the GUI
  
}
}

function 
onCreated() {
  new 
GuiWindowCtrl("WEP_Window1") {
    
profile GuiWindowProfile;
    
clientrelative true;
    
clientextent "260,385";
    
canmaximize false;
    
canmove true;
    
canresize true;
    
closequery false;
    
destroyonhide false;
    
text "WEP - Control";
    
413;
    
73;
    new 
GuiScrollCtrl("WEP_TextList5_Scroll") {
      
profile GuiScrollProfile;
      
height 307;
      
hscrollbar "alwaysOff";
      
vscrollbar "dynamic";
      
width 128;
      new 
GuiTextListCtrl("WEP_TextList5") {
        
profile GuiTextListProfile;
        
height 32;
        
horizsizing "width";
        
width 124;
      }
    }
    new 
GuiScrollCtrl("WEP_TextList6_Scroll") {
      
profile GuiScrollProfile;
      
height 307;
      
hscrollbar "alwaysOff";
      
vscrollbar "dynamic";
      
width 129;
      
131;
      new 
GuiTextListCtrl("WEP_TextList6") {
        
profile GuiTextListProfile;
        
height 32;
        
horizsizing "width";
        
width 125;
      }
    }
    new 
GuiButtonCtrl("WEP_Button1") {
      
profile GuiButtonProfile;
      
text "Remove";
      
width 80;
      
5;
      
349;
    }
    new 
GuiButtonCtrl("WEP_Button2") {
      
profile GuiButtonProfile;
      
text "Add";
      
width 80;
      
5;
      
314;
    }
    new 
GuiTextEditCtrl("WEP_TextEdit1") {
      
profile GuiTextEditProfile;
      
height 20;
      
width 163;
      
91;
      
319;
    }
    new 
GuiTextEditCtrl("WEP_TextEdit2") {
      
profile GuiTextEditProfile;
      
height 20;
      
width 163;
      
91;
      
354;
    }
  }
}

function 
WEP_Button1.onAction() { //Hit once to taje weapon, hit twice to update list.
  // Button "Remove" has been pressed
  
UpdateList1(); //Updates List
  
UpdateList2();
  
removechat(); //Sets Chat
  
triggerserver("gui"this.name"takeweaponwep"WEP_TextEdit2.text);
}

function 
WEP_Button2.onAction() { //Hit once to add weapon, hit twice to update list.
  // Button "Add" has been pressed
  
UpdateList1(); //Updates List
  
UpdateList2();
  
addchat(); //Sets Chat
  
triggerserver("gui"this.name"addweaponwep"WEP_TextEdit1.text);
}

function 
UpdateList1() {
  
WEP_TextList5.clearrows();
  for (
temp.weps 0temp.weps player.weapons.size(); temp.weps++) {
    
WEP_TextList5.addrow(temp.wepsplayer.weapons[temp.weps].name);
  }
}

function 
UpdateList2() {
  
WEP_TextList6.clearrows();
  for (
temp.weps 0temp.weps player.weapons.size(); temp.weps++) {
    if (!(
player.weapons[(@temp.weps)].name.starts("-"))) {
      
WEP_TextList6.addrow(temp.wepsplayer.weapons[temp.weps].name);
    }
  }
}

function 
RemoveChat() {
  if (!(
WEP_TextEdit2.text == NULL)) player.chat "Removed: " WEP_TextEdit2.text//Sets chat
}

function 
AddChat() {
  if (!(
WEP_TextEdit1.text == NULL)) player.chat "Added: " WEP_TextEdit1.text//Sets Chat


Graal PasteBin Version

Thank you to everybody who helps me out.

Styled using FowlPlay4's GS2 Beautifier

Crow 08-17-2011 12:10 AM

Do you want this to be based on GUI controls? I find commands to be more handy. I got !addw and !rmvw to add/remove weapon NPCs on the fly. Works like a charm.

Emera 08-17-2011 12:14 AM

Quote:

Originally Posted by Crow (Post 1663925)
Do you want this to be based on GUI controls? I find commands to be more handy. I got !addw and !rmvw to add/remove weapon NPCs on the fly. Works like a charm.

I normally use GUI's to make things look nicer, but yeah I could do that easily and it does sound more practical.
Thanks!

Maelstrom002 08-17-2011 12:21 AM

I would take the entire GUI Creation and move it into it's own function, versus having it in onCreated(), so that if you ever update the weapon, everyone who has it won't have it pop open on them!

Also, instead of using the two textEditCtrls, you could simply replace that with the text of your TextList, so you don't need to type in the name of the weapon, just select it from the list and hit add/remove.
Lastly I would say to add braces in your top set of lines, for your :


PHP Code:

function onActionServerSide() 

  if (
params[0] == "addweaponwep")
  {
    
addweapon(params[1]); //Add the weapon.  
  
}
  else if (
params[0] == "takeweaponwep")
  {
    
removeweapon(params[1]); //Take the weapon.
  
}



It's really a personal preference, but I think it's easier to read/update in the future
for others. Perhaps even come up with more unique/descriptive names for your ctrls
so down the road you can just look at it and see what they correspond to!

Emera 08-17-2011 12:48 AM

Quote:

lso, instead of using the two textEditCtrls, you could simply replace that with the text of your TextList, so you don't need to type in the name of the weapon, just select it from the list and hit add/remove.
Yeah lol I noticed that AFTER I put down the 2 texteditctrls but I never got around to removing them.
Quote:

Lastly I would say to add braces in your top set of lines.
I didn't know they where needed, but if you say so. I thought it makes it look neater that way.
Quote:

Perhaps even come up with more unique/descriptive names for your ctrls
so down the road you can just look at it and see what they correspond to!
Yeah I need to be a bit more "Creative" with my names. Thanks for helping me out!

Emera 08-17-2011 01:00 AM

I decided to take on board what crow said about how using the players chat is more efficient for adding weapons. I have decided to make a quick weapon adder using !add and !rem. Thank you crow <3
PHP Code:

function onActionServerSide() {
  if (
params[0] == "scriptwepadd") { //Adds the specified weapon
    
addweapon(params[1]);
  }
  if (
params[0] == "scriptwepremove") { //Removes the specified weapon
    
removeweapon(params[1]);
  }
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("!add ")) {
    
triggerserver("gui"this.name"scriptwepadd"player.chat.substring(5));
  }
  if (
player.chat.starts("!rem ")) {
    
triggerserver("gui"this.name"scriptwepremove"player.chat.substring(5));
  }


Obviously, if I wanted to I could branch off of that and expand it to add weapons to all players, certain players, a specified player etc.

Styled using FowlPlay4's GS2 Beautifier

Mark Sir Link 08-17-2011 01:04 AM

one of the first things you should do is verify who is actually sending this trigger to the server since it's incredibly insecure otherwise

and then while you're at it you should prefix addweapon and removeweapon with the proper object (player)

Emera 08-17-2011 01:07 AM

Quote:

Originally Posted by Mark Sir Link (Post 1663938)
one of the first things you should do is verify who is actually sending this trigger to the server since it's incredibly insecure otherwise

and then while you're at it you should prefix addweapon and removeweapon with the proper object (player)

Don't understand how to verify that. But here is it prefixed...
PHP Code:

function onActionServerSide() {
  if (
params[0] == "scriptwepadd") { //Adds the specified weapon
    
player.addweapon(params[1]); //Prefixed player.addweapon
  
}
  if (
params[0] == "scriptwepremove") { //Removes the specified weapon
    
player.removeweapon(params[1]); //Prefixed player.removeweapon
  
}
}

//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat.starts("!add ")) {
    
triggerserver("gui"this.name"scriptwepadd"player.chat.substring(5));
  }
  if (
player.chat.starts("!rem ")) {
    
triggerserver("gui"this.name"scriptwepremove"player.chat.substring(5));
  }



WhiteDragon 08-17-2011 01:10 AM

A small comment on the "Chat vs. GUI" topic:

I think it's good to have both. Chat for "power players" (those who frequently), and GUI for new players or those who don't player often enough to bother remember commands.

This is mainly the approach we take on Classic. But it become more critical to properly abstract things with this approach because otherwise you'll have duplicate code lying all over the place.

Emera 08-17-2011 01:14 AM

Quote:

Originally Posted by WhiteDragon (Post 1663941)
A small comment on the "Chat vs. GUI" topic:

I think it's good to have both. Chat for "power players" (those who frequently), and GUI for new players or those who don't player often enough to bother remember commands.

This is mainly the approach we take on Classic. But it become more critical to properly abstract things with this approach because otherwise you'll have duplicate code lying all over the place.

If I was to redo the GUI, not that I am going to, would it even be necessary to add the weapon list control or would I be better off without it and just have a text box with a button or two to add and delete?

WhiteDragon 08-17-2011 02:54 AM

Quote:

Originally Posted by Emera (Post 1663943)
If I was to redo the GUI, not that I am going to, would it even be necessary to add the weapon list control or would I be better off without it and just have a text box with a button or two to add and delete?

Well, in this specific case just commands are probably fine -- if there is a staff member who is not a power player then you clearly have some other problems to fix first :p.

But in the name of theory, if this was actually a public facing weapon, then I'd say have a weapon list, since just a text box isn't particularly friendly and is pretty much the exact same thing as a normal chat command.

Maelstrom002 08-17-2011 02:59 AM

I think the best option would be to incorporate both within the same weapon. Allow players to be able to access the GUI if need-be to see a list of what they could add to themselves, or what they already have.
Then also allow for the chat commands for those who know what they have/need and can quickly add/remove things without having to navigate a GUI.

Mark Sir Link 08-17-2011 03:33 AM

in the onActionServerside portion of the script you should be verifying that the player who sent the trigger is actually someone you expect it to be, IE

if(player.account in serveroptions.staff.tokenize(","))

Emera 08-17-2011 11:40 AM

Ah okiedokie thanks for that.

cbk1994 08-17-2011 01:16 PM

Quote:

Originally Posted by Mark Sir Link (Post 1663967)
in the onActionServerside portion of the script you should be verifying that the player who sent the trigger is actually someone you expect it to be, IE

if(player.account in serveroptions.staff.tokenize(","))

You should check player.communityname, not player.account, since for some reason Stefan decided that the new accounts have to have their community name in the staff list.

Emera 08-17-2011 01:52 PM

Quote:

Originally Posted by cbk1994 (Post 1664015)
You should check player.communityname, not player.account, since for some reason Stefan decided that the new accounts have to have their community name in the staff list.

Would it be practical to check for both?

callimuc 08-17-2011 03:37 PM

Quote:

Originally Posted by Emera (Post 1664016)
Would it be practical to check for both?

maybe since servers mostly still have the accounts.
PHP Code:

if(player.account in serveroptions.staff.tokenize(",") || player.communityname in serveroptions.staff.tokenize(",")) 


Mark Sir Link 08-17-2011 07:19 PM

Quote:

Originally Posted by callimuc (Post 1664027)
maybe since servers mostly still have the accounts.
PHP Code:

if(player.account in serveroptions.staff.tokenize(",") || player.communityname in serveroptions.staff.tokenize(",")) 


the only time a communityname wouldn't return the proper value (I think this is still the case) is if a new Graal####### account hasn't set a community name.

I guess you could do

if( ((player.communityname.length() > 0) ? player.communityname : player.account) in severoptions.staff.tokenize(","))

to prevent incorrectly formatted staff lists that might have an empty value somewhere from returning true for players that haven't set their community name.

cbk1994 08-17-2011 09:40 PM

Quote:

Originally Posted by Emera (Post 1664016)
Would it be practical to check for both?

There's no reason to. All you really need to check for is the community name. Players with old accounts will have their account as their community name.

Mark Sir Link 08-17-2011 09:53 PM

Quote:

Originally Posted by cbk1994 (Post 1664052)
There's no reason to. All you really need to check for is the community name. Players with old accounts will have their account as their community name.

on Unholy Nation I was checking exclusively for community names and stumbled into the same problem I posted about with an improperly formatted staff list.

Granted, I couldn't actually find any instances of an empty entry in the array but it was still returning true when I checked player.communityname in the array.

each array member -

PHP Code:

[Manager]
Absolut_Crono
[Admin]
Flares
[Dev_Admin]
Mark Sir Link
KevDoh
[NAT_Admin]
[
Scripting_Team]
Toxen
Deas_Voice
devilsknite1
MysticalDragon
thatdwarf
ApothiX
Hezzy002
Switch
zeldaguardian
[Graphics]
Blazeik
JeffNightVale
[Levels_Admin]
shyguy2
[Levels_Team]
javierkid
Dr_Doctor
Hostility
27Chris1
snoop413
HolySerenity
[FAQ]
Kamakaze
Samurai_X2689
Dread
ranmalrac1
Hosaun
[Gani_Team]
dark_mater_s
[Events_Team]
Jello
Elite3
four_swords
Unknown_Prophecy
KaidaChan
warcraft1111
maxi123
GracefulCharity
BrutaL
Timez
Battousai90290
larenaz19
babyx
[Graal_Police]
CertifiedGangsta252
Okilian
dubby230
nightslayer317
Hiro
taylor
ssgohan87
MooseInATree
weeway
[Developer]
Bane
SwimChao
Gamerkid7
[SFX]
[
Uploader]
Door
[Unholy_Radio]
[
The_Man]
_Zelph
[ET_Dev_Admin]
Starfire2001
[ET_Dev]
Akios
[Global]
Skyld
[ENDSTAFF


cbk1994 08-17-2011 10:19 PM

Quote:

Originally Posted by Mark Sir Link (Post 1664055)
on Unholy Nation I was checking exclusively for community names and stumbled into the same problem I posted about with an improperly formatted staff list.

Granted, I couldn't actually find any instances of an empty entry in the array but it was still returning true when I checked player.communityname in the array.

Good point. I wish Stefan would just fix community names.

Mark Sir Link 08-17-2011 11:23 PM

Quote:

Originally Posted by cbk1994 (Post 1664059)
Good point. I wish Stefan would just fix community names.

I guess servers could try to force it on players by disconnecting them if their community name is null and using the shared admin function thing but I doubt Stefan would approve or appreciate it.

I didn't even think it would be problematic initially until all of a sudden I see RC get flooded with players warning each other.


All times are GMT +2. The time now is 10:55 PM.

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