Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Global Trigger Weapons (https://forums.graalonline.com/forums/showthread.php?t=134257162)

Codein 12-02-2009 02:23 AM

Global Trigger Weapons
 
Why?

I admit, it's rather simple but if you ever run into needing something like this then atleast you won't have to reinvent the wheel :P

Serverside Global Weapon Trigger

Methods:
void GlobalEvents.triggerAll( string eventName, array parameters[] );
This triggers an event on every weapon serverside. GlobalEvents should be replaced by your NPC name.

PHP Code:

function onCreated() {
  
//IMPORTANT - ASSIGN THE PERCENTSIGN
  
temp.percentSymbol "percent";

  
this.weapons NULL;
  
  
temp.charMap = {
                  {
temp.percentSymbol "045""-"},
                  {
temp.percentSymbol "042""*"},
                  {
temp.percentSymbol "047""/"},
                  {
temp.percentSymbol "032"" "}
                 }; 
  
  
temp.weaponsFolder.loadfolder("weapons/*.txt"1);
  
  for (
temp.weapontemp.weaponsFolder) {
    
temp.weapon temp.weapon.substring);
    
    for ( 
temp.chartemp.charMap )
      
temp.weapon replacetexttemp.weapontemp.char[0], temp.char[1] );
   
    
this.weapons.addtemp.weapon.substring0temp.weapon.length() - ) );
  }
}

public function 
triggerAlleventNameparameters ) {
  for ( 
temp.weaponthis.weapons ) {
    ( @ 
temp.weapon ).triggereventNameparameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9] );
  }
}

//Thanks to Dusty
function replacetext(txt,a,b) {
  if (
txt.pos(a)<0) return txt;
  
temp.txtpos txt.positions(a);
  
temp.newtxt txt.substring(0,txtpos[0]);
  for (
temp.i=0;i<txtpos.size();i++) {
    
newtxt @= b;
    
newtxt @= txt.substring(txtpos[i]+a.length(),txt.substring(txtpos[i]+a.length()).pos(a));
  }
  return 
newtxt;


Clientside Global Weapon Trigger

Same syntax as serverside. This is actually rather simple but it's one less tediuous task for yourself :)

PHP Code:

function onPlayerLoginpl ) {
  
findPlayerpl ).addWeaponthis.name );
}

//#CLIENTSIDE

function onCreated() {
  
GlobalEvents this;
}

public function 
triggerAlleventNameparameters ) {
   for ( 
temp.weaponplayer.weapons ) {
    
temp.weapon.triggereventNameparameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9] );
  }


I'll be posting the functionality for NPCs soon - I should have been working on an Events System instead of this, haha.

Thanks for reading! :)

Kyranki 12-02-2009 05:11 AM

Hey, works. Rep for you.

Chompy 12-02-2009 05:30 PM

Would be nice if there was some way to detect files outside of levels/ being updated, as it could then auto update list. So, only suggestion I got atm is some way to update the list, ie. RC Command, a timer (deprecated though, first thing I said would be much better, but I guess we have to wait?)

edit; http://forums.graalonline.com/forums...62#post1541962

Codein 12-02-2009 10:24 PM

Thanks for the advice Chompy. I was actually thinking to myself in Maths today: "Crap, I forgot to add functionality to update the list! Damn...", lol.

Anyway, enjoy!

EDIT: Holy crap, I didn't expect the percentage symbols to work!

Instructions & Code
Add this to Control-NPC - if event exists, just copy the function call and do whatever you wish.

PHP Code:

function onRCChat() {
  if ( 
params[0] == "updateweaponlist" ) ( @ globalWeaponTrigger ).updateWeaponList();


This is the updated code:

PHP Code:

function onCreated() {
  
updateWeaponList();
  
globalTriggerName this.name;
}

public function 
triggerAlleventNameparameters ) {
  for ( 
temp.weaponthis.weapons ) {
    ( @ 
temp.weapon ).triggereventNameparameters );
  }
}

public function 
updateWeaponList() {
  
this.weapons NULL;
  
  
temp.charMap = {
                  {
"%045""-"},
                  {
"%042""*"},
                  {
"%047""/"},
                  {
"%032"" "}
                 }; 
  
  
temp.weaponsFolder.loadfolder"weapons/*.txt");
  
  for (
temp.weapontemp.weaponsFolder) {
    
removeescapesfromfilenametemp.weapon );
    
this.weapons.addtemp.weapon.substring6temp.weapon.length() - ) );
  }
  
  echo( 
this.name ": weapon list updated." );



Chompy 12-02-2009 11:50 PM

The NPCServer also need R(ead) rights to the weapons/ folder for this to work ;)

Riot 12-03-2009 12:25 AM

As a note, you can use the "removeescapesfromfilename" function to removing the escaping used in filenames (rather than the charMap you have).

removeescapesfromfilename(str) - returns string

echo(removeescapesfromfilename("weapon%045Test.txt "));
outputs: weapon-Test.txt

cbk1994 12-03-2009 12:32 AM

Quote:

Originally Posted by Chompy (Post 1542014)
The NPCServer also need R(ead) rights to the weapons/ folder for this to work ;)

Nope, you can loadFolder() even without read rights.

Quote:

Originally Posted by Riot (Post 1542033)
As a note, you can use the "removeescapesfromfilename" function to removing the escaping used in filenames (rather than the charMap you have).

removeescapesfromfilename(str) - returns string

echo(removeescapesfromfilename("weapon%045Test.txt "));
outputs: weapon-Test.txt

I've never seen that before, nice find :).

Chompy 12-03-2009 12:41 AM

Quote:

Originally Posted by cbk1994 (Post 1542037)
Nope, you can loadFolder() even without read rights.

Interesting. Thought all functions having to do with the filebrowser required rights, but I guess there are exceptions.

coreys 12-03-2009 06:28 AM

Well, I guess it kinda makes sense since loadFolder() really isn't doing much but getting a list of file names.

Codein 12-03-2009 02:21 PM

Quote:

Originally Posted by Riot (Post 1542033)
As a note, you can use the "removeescapesfromfilename" function to removing the escaping used in filenames (rather than the charMap you have).

removeescapesfromfilename(str) - returns string

echo(removeescapesfromfilename("weapon%045Test.txt "));
outputs: weapon-Test.txt

Thanks :)

Updated the latest post.

Switch 12-05-2009 01:55 AM

Quote:

Originally Posted by Codein (Post 1542001)
PHP Code:

this.weapons.addtemp.weapon.substring6temp.weapon.length() - ) ); 


Wouldn't it need to be temp.weapon.length()-11?

Codein 12-05-2009 11:36 AM

Quote:

Originally Posted by Switch (Post 1542527)
Wouldn't it need to be temp.weapon.length()-11?

To take out the "weapon" prefix and the ".txt" suffix?

cbk1994 12-05-2009 09:14 PM

Why are you doing this?

PHP Code:

globalTriggerName this.name

It's probably better to just do this:

PHP Code:

globalTrigger this

then you can just do

PHP Code:

globalTrigger.function(); 


Codein 12-05-2009 09:56 PM

Quote:

Originally Posted by cbk1994 (Post 1542702)
Why are you doing this?

PHP Code:

globalTriggerName this.name

It's probably better to just do this:

PHP Code:

globalTrigger this

then you can just do

PHP Code:

globalTrigger.function(); 


Ah yeah, should have done that. thanks.

Switch 12-05-2009 10:35 PM

Quote:

Originally Posted by Codein (Post 1542636)
To take out the "weapon" prefix and the ".txt" suffix?

Yes since it'd still include "weapon", right? But I get what you were trying to do.

Codein 12-05-2009 10:40 PM

Quote:

Originally Posted by Switch (Post 1542712)
Yes since it'd still include "weapon", right? But I get what you were trying to do.

o_O

Start = 6, so ommitting the 'weapon' prefix.
End = length - 4; omitting the '.txt' suffix.

Switch 12-05-2009 11:52 PM

Quote:

Originally Posted by Codein (Post 1542715)
Start = 6, so ommitting the 'weapon' prefix.
End = length - 4; omitting the '.txt' suffix.

Never knew the second param in substring() took off whatever the first param didn't include.

Chompy 12-06-2009 02:54 PM

Quote:

Originally Posted by Switch (Post 1542730)
Never knew the second param in substring() took off whatever the first param didn't include.

How substring works:

"testingfoobarbaz".substring(3, 5)

It starts at 3, so it excludes the red

tes
tingfoobarbaz


Then it will extract the text between starting index 3 and the length from this index.

tes
tingfoobarbaz


The text inbetween the colored text is what you end up with

Switch 12-06-2009 06:38 PM

Quote:

Originally Posted by Chompy (Post 1542886)
explanation

Yes I know how substrings work, I just didn't know if you had something like
PHP Code:

temp.string "Hi my name is Frank!";
echo(
temp.string.substring(14temp.string.length()-1); 

Would return "Frank" and not "Frank!" (not sure if there'd be 0's or spaces on the end, either) since the full length is 20 but it gets reduced down to 6, then taking 1 from that gives 5, which is enough for "Frank" without the "!"


All times are GMT +2. The time now is 05:55 AM.

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