Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Search array name (https://forums.graalonline.com/forums/showthread.php?t=87874)

sssssssssss 09-12-2009 08:04 PM

Search array name
 
Is there a way to search and pull up all array names that would start with certain letters? Like there are the array Graalmember1, Graalmember2, Graalmember3, Cutfish, Bombs.
And I want to show the arrays that start with Graal in a gui?

cbk1994 09-12-2009 08:15 PM

Not a built-in way, but it's easy to script

PHP Code:

function searchArray(array, start) {
  
temp.new = null;
  
  for (
temp.: array) {
    if (
a.starts(start)) {
      new.
add(a);
    }
  }

  return new;



sssssssssss 09-12-2009 08:23 PM

I leave the array part as array correct? being that there are many different array names?
and can call that function like:
searchArray(array, start);

correct?

bleh, to be more specific.
searching through the arrays ingraal1, ingraal2, ingraal3, graal4, cuddlefish, bombs, arrows, penguins

to pull up only the ones that have "ingraal" in the array name, then display the number after "ingraal".
so it finds ingraal1, ingraal2, ingraal3, then displays
1
2
3

Cloven 09-12-2009 10:20 PM

Hint: using .substring

cbk1994 09-12-2009 10:32 PM

Something like this I guess

PHP Code:

function searchArray(array, start) {
  
temp.new = null;
  
  for (
temp.: array) {
    if (
a.starts(start)) {
      new.
add(a.substring(start.length()));
    }
  }

  return new;



sssssssssss 09-12-2009 10:34 PM

Can you explain that though please? and how to call it correctly? I also dont wannt just copy, i wanna understand

xXziroXx 09-12-2009 10:41 PM

Quote:

Originally Posted by sssssssssss (Post 1522623)
Can you explain that though please? and how to call it correctly? I also dont wannt just copy, i wanna understand

Examples:

PHP Code:

echo(searchArray({ "ingraal1""ingraal2""ingraal3""graal4""cuddlefish""bombs""arrows""penguins" }, "ingraal"));

this.nameOfMyArray = { "fooBar1""barFoo1""baz""fooBar2" };
echo(
searchArray(this.nameOfMyArray"foo")); 


sssssssssss 09-12-2009 10:55 PM

the problem is, that doesnt seem it will work. Im not trying to search for values in an array. Ill set it up more clear.
Array
ingraal1 {hi,how,he}
ingraal2 {k,m,c,b,d}
ingraal3 {op,asd,g,a}
cuddlefish {2,5,213,2,0}
bombs {sjd,23,6,a}
arrows {s,d,e,gf,a}

Trying to search the NAME of the array, not the names inside the array.
So i want to pull out the array names that have "ingraal" in them, which would pull ingraal1, ingraal2, ingraal3. Then, i was wanting to print out what is after "ingraal" so it would show up
1
2
3
from the "ingraal(1-3)" array names.

Cloven 09-12-2009 10:59 PM

I meant that .substring functions essentially as #e did in gscript in this case. You can use that to compare clusters of letters to entries found within your array and then extract desired information from the matches.

It is .substring(start,len) after all. :)

sssssssssss 09-12-2009 11:12 PM

That completely went over my head. Please explain in more simple words :)

Mark Sir Link 09-12-2009 11:16 PM

if you have an array called this.arr, this.arr.name will return arr.

However, I can't see any useful implementation of what you're asking for since searching through the arrays to see what begins with "ingraal" requires you already knowing exactly where they are and searching each one manually.

If you were to pass an array of arrays to the function and check through all the arrays in there, this.collectedarrays[0].name returns nothing.

sssssssssss 09-12-2009 11:24 PM

Ive been just using examples. What im actually doing is i have guilds register on a server to have and give rights to members for certain things.
when it creates the array it creates it as
registeredguildsGUILDNAME={acct name, acct name}
so i need to search through the array names that are registeredguilds then find the GUILDNAME, so i can print out the entire list of the GUILDNAMES that are registered on my server in a gui.

any other ideas on how to go about that, please let me know.

xXziroXx 09-12-2009 11:32 PM

I'd recommend using TStaticVar's.

PHP Code:

this.registeredguilds = new TStaticVar();
this.registeredguilds.GUILDNAME1.members = { "acc1""acc2""acc3" };
this.registeredguilds.GUILDNAME2.members = { "acc1""acc2""acc3" };
this.registeredguilds.GUILDNAME3.members = { "acc1""acc2""acc3" };

// to read all guilds, use...
this.registeredguilds.getDynamicVarNames()
// to read guild members of a string defined guild, use...
temp.guildName "foobar";
this.registeredguilds.(@temp.guildName).members 


sssssssssss 09-13-2009 12:37 AM

can that be used with server.var?
rather not use this.var b/c if the level is updated every single guild has to reregister.

Mark Sir Link 09-13-2009 12:46 AM

use a database NPC

sssssssssss 09-13-2009 12:54 AM

Been a while since i had a playerworld, it was right when npc servers came out, how you set up a db npc

fowlplay4 09-13-2009 01:25 AM

Click the Green Dragon DB-NPC button, then click Add in the NPCs window that pops up, fill in the appropriate information in the next window that pops up and click Apply.

sssssssssss 09-13-2009 01:59 AM

The level i add it on to start, is that the level it works on?
I dont want it to work everywhere.
If not, of if i guess, how do i "call" it in a level npc?

Mark Sir Link 09-13-2009 02:03 AM

Quote:

Originally Posted by sssssssssss (Post 1522698)
The level i add it on to start, is that the level it works on?
I dont want it to work everywhere.
If not, of if i guess, how do i "call" it in a level npc?

it is accessible anywhere serverside, but to call it from a level NPC you can do something like

findnpc(dbnpcname).function()

or if you plan on calling the NPC a few times, store the npc in a var (cont = findnpc(dbnpcname) )

sssssssssss 09-13-2009 02:18 AM

I read some stuff on graal.net, but still dont really understand this.
If i have this script in a db npc, how is the results going to show up on my gui in a level?

actually worded better:
how am i going to take this.vars from the db npc, and then pick out parts of the name, then show it up on a gui in a level npc.

Switch 09-13-2009 03:10 AM

Quote:

Originally Posted by fowlplay4 (Post 1522686)
Green Dragon

BIG GREEN MONSTER AHHHH!
Quote:

Originally Posted by sssssssssss (Post 1522698)
The level i add it on to start, is that the level it works on?
I dont want it to work everywhere.
If not, of if i guess, how do i "call" it in a level npc?

It works kind of like a level NPC except it can be accessed through findNPC("Database Name") serverside in any script.

sssssssssss 09-13-2009 03:35 AM

so i can go
PHP Code:

findNPC("Database Name"

then pretend like the entire script of the DB npc is in the level npc.

so i could do
PHP Code:

say2(this.registeredguildsSDE); 

in the level npc after referring to the db npc and it would work?

sssssssssss 09-13-2009 03:43 AM

Actually, doesnt seem to be working. heres my script:

PHP Code:

function onCreated() {
  
this.setshape(13232);
  
this.registeredguilds = new TStaticVar();
}
function 
onActionRegistersdeServer(leader){
  
makevar("this.registeredguilds." player.guild ".member") = {leader};
  
say2("Guild " player.guild " is registered #b
  by " 
leader "!");
}
function 
onActionAddMemRegGuildServer(guildzzmember){
  
this.(@"registeredguilds." guildzz ".member").add(member);
  
say2("Rights of " member " have been added #b
  to " 
guildzz "!");
}
function 
onActionDelMemRegGuildServer(guildzremove){
  
this.(@"registeredguilds." guildz ".member").remove(remove);
  
say2("Rights of " remove " have been removed #b
  from " 
guildz "!");
}
function 
onActionSetTagServer(tag){
  
player.guild tag;
}

//#CLIENTSIDE
function onCreated() {
  
this.setshape(13232);
}

function 
onPlayerChats() {
  if (
player.chat == "registerguild"){
    if (
player.guild == ""){
      
say2("Put on guild tag first!");
    }else{
      
temp.checkregistry this.(@"registeredguilds." player.guild ".member");
      if (!(
checkregistry == "")){
        
say2("This guild is already registered");
      }else{
        
temp.owner player.account;
        
triggeraction(this.0.5this.0.5"RegistersdeServer"temp.owner);
      }
    }
  }
  if (
player.chat.starts("addmemberaccess")){
    if (
player.guild == ""){
      
say2("Put on guild tag first!");
    }else{
      
temp.checkregistry this.(@"registeredguilds." player.guild ".member");
      if (!(
player.account in checkregistry)){
        
say2("You do not have rights!");
      }else{
        
temp.addmemg player.guild;
        if (
player.guild == temp.addmemg){
          
temp.addmem player.chat.tokenize()[1];
          
triggeraction(this.0.5this.0.5"AddMemRegGuildServer"temp.addmemgtemp.addmem);
        }
      }
    }
  }
  if (
player.chat.starts("removememberaccess"))
    {
    if (
player.guild == ""){
      
say2("Put on guild tag first!");
    }else{
      
temp.checkregistry this.(@"registeredguilds." player.guild ".member");
      if (!(
player.account in checkregistry)){
        
say2("You do not have rights!");
      }else{
        
temp.delmemg player.guild;
        if (
player.guild == temp.delmemg){
          
temp.del player.chat.tokenize()[1];
          
triggeraction(this.0.5this.0.5"DelMemRegGuildServer"temp.delmemgtemp.del);
        }
      }
    }
  }
  if (
player.chat.starts("settag")){
    
temp.tag player.chat.tokenize()[1];
    
triggeraction(this.0.5this.0.5"SetTagServer"temp.tag);

  }


ignore the settag stuff, just to test other guilds registry besides my own. The starting level is not where the level npc is at, didnt think that should matter.

And i used this on a level npc:
PHP Code:

function onCreated() {
  
this.setshape(13232);
findNPC("db_registeredguilds")

}

//#CLIENTSIDE
function onCreated() {
  
this.setshape(13232);
}
function 
onPlayerTouchsMe(){
  
temp.checkregistry this.(@"registeredguilds." player.guild ".members");
  if (
player.account in checkregistry){
    
this.chat "YO";
  }


produces no results. when i used serverr.var all this worked fine.

cbk1994 09-13-2009 05:21 AM

Why are you still using clientside for chat commands?

sssssssssss 09-13-2009 05:33 AM

I did take it out, thats actually a bit old, same script just minus //#CLIENTSIDE

Mark Sir Link 09-13-2009 05:40 AM

simply finding the NPC on it's own in your script will not do anything.

You need to have a var point to it after returning it.

Also, you cannot read this.vars from server to client like that.

My suggestions are to move nearly everything out of the clientside, store the few things you want accessible by GUI into the DB NPC's attribute array, copy the attribute array to the level NPC (although a weapon would probably be better for guild GUI related things), then display on clientside.

Example -

PHP Code:

function onCreated()
{
  
this.cont findnpc("mydbnpc");
  
copyAttrs();
}

function 
copyAttrs()
{
  for(
temp.030i++)
  {
    
this.attr[i] = this.cont.attr[i];
  }
}
//#CLIENTSIDE
function onCreated()
{
  
//GUI stuff here, use attrs as you please to sort things out...



sssssssssss 09-13-2009 06:12 AM

The thing is, player has to chat to set it. Do i include that in the npc db too? see my script and what its doing. thats why i used a serverr.flag, its less of a pain, but if TStaticVar() is the only way to do what i need to do, compared to the script i posed before that one, then i guess i have to.
I guess Im lost enough to ask someone to show me, at the least, a full example. :/

The thing is, if you read the code i posted, its not just gui. That entire code sets a register server.flag for each guild registered, so playerchats and everything is involved. On a seperate npc i was using a gui to list the guilds that were registered. Im lost, and just getting confused even more now.

napo_p2p 09-13-2009 07:52 PM

I worked with Cyril today, and got something working. He's currently working on interfacing with a GUI, so he might come up with some more questions. Here are the scripts, for future reference:

DB NPC (db_registeredguilds)
PHP Code:

function onInitialized() {
  
onCreated();
}

function 
onCreated() {
  if (
this.guilds.type() != 2) {
    
this.guilds = new TStaticVar();
  }
}

public function 
RegistersdeServer() {
  if (
player.guild == "") {
    
say2("Put on guild tag first!");
    return;
  }
  if (
this.guilds.(@player.guild) == null) {
    
this.guilds.(@player.guild) = {player.account};
    
say2("Guild " player.guild " is registered #bby " player.account "!");
  }
  else {
    
say2("This guild is already registered");
  }
}

public function 
AddMemRegGuildServer(member) {
  if (
player.guild == "") {
    
say2("Put on guild tag first!");
    return;
  }
  
  if (
hasAccess()) {
    if (!(
temp.member in this.guilds.(@player.guild))) {
      
this.guilds.(@player.guild).add(temp.member);
      
say2(temp.member "#badded to#b" player.guild);
    }
    else {
      
say2(temp.member "#bis already in#b" player.guild);
    }
  }
  else {
    
say2("You do not have rights!");
  }
}

public function 
DelMemRegGuildServer(member) {
  if (
player.guild == "") {
    
say2("Put on guild tag first!");
    return;
  }
  
  if (
hasAccess()) {
    
this.guilds.(@player.guild).remove(temp.member);
    
say2(temp.member "#bremoved from#b" player.guild);
  }
  else {
    
say2("You do not have rights!");
  }
}

public function 
hasAccess() {
  return (
player.account in this.guilds.(@player.guild));
}

public function 
getGuildList() {
  return 
this.guilds.getdynamicvarnames();


Level Script:
PHP Code:

function onCreated() {
  
this.db findnpc("db_registeredguilds");
}

function 
onPlayerChats() {
  if (
player.chat == "registerguild") {
    
this.db.RegistersdeServer();
  }
  if (
player.chat.starts("addmemberaccess")) {
    
this.db.AddMemRegGuildServer(player.chat.tokenize()[1]);
  }
  if (
player.chat.starts("removememberaccess")) {
    
this.db.DelMemRegGuildServer(player.chat.tokenize()[1]);
  }


There's a million other ways to do this, but I also wanted to show him how public functions worked. We'll leave SQL for another day ;).

cbk1994 09-13-2009 08:16 PM

Why are you using findNPC? There's no need to, you can simply reference it as an object.

napo_p2p 09-13-2009 08:50 PM

Quote:

Originally Posted by cbk1994 (Post 1522911)
Why are you using findNPC? There's no need to, you can simply reference it as an object.

I like doing it that way, so that if the DB changes for whatever reason, I just have to edit it in one place.

Crow 09-13-2009 08:58 PM

Quote:

Originally Posted by cbk1994 (Post 1522911)
Why are you using findNPC? There's no need to, you can simply reference it as an object.

Preference.

cbk1994 09-13-2009 08:58 PM

Quote:

Originally Posted by napo_p2p (Post 1522916)
I like doing it that way, so that if the DB changes for whatever reason, I just have to edit it in one place.

Just do

PHP Code:

this.db db_registeredguilds


fowlplay4 09-13-2009 09:24 PM

I personally don't trust DB-NPCs enough to do that.

xXziroXx 09-14-2009 04:30 PM

Quote:

Originally Posted by cbk1994 (Post 1522911)
Why are you using findNPC? There's no need to, you can simply reference it as an object.

Why do you have to bring up that in every single thread and push your opinion down everyone else's throat? It is personal preference, and nothing you can say about it changes that.

salesman 09-14-2009 04:48 PM

Wouldn't findNPC() be less efficient because you're calling a function to get the object instead of just referencing it by name? Especially when people use findNPC(npcname) over and over again instead of assigning the npc to some variable.

I have no idea, but it just seems like it would be.

fowlplay4 09-14-2009 06:56 PM

Quote:

Originally Posted by salesman (Post 1523042)
Wouldn't findNPC() be less efficient because you're calling a function to get the object instead of just referencing it by name? Especially when people use findNPC(npcname) over and over again instead of assigning the npc to some variable.

I have no idea, but it just seems like it would be.

I wondered this as well so..

PHP Code:

function benchmark() {
  
// Iterations
  
temp.max 100000;
  
// Direct
  
temp.init timevar2;
  for (
temp.0temp.temp.maxtemp.i++) {
    
db_test.variable temp.i;
  }
  echo(
"dNPC" SPC timevar2 temp.init);
  
// findNPC
  
temp.init timevar2;
  for (
temp.0temp.temp.maxtemp.i++) {
    
findnpc("db_test").variable temp.i;
  }
  echo(
"fNPC" SPC timevar2 temp.init);
  
// Variable findNPC
  
temp.init timevar2;
  
temp.db findnpc("db_test");
  for (
temp.0temp.temp.maxtemp.i++) {
    
temp.db.variable temp.i;
  }
  echo(
"vNPC" SPC timevar2 temp.init);


Results:

NPC Code:

JerretNPC: benchmark()
dNPC 0.069311141 seconds
fNPC 0.10556817 seconds
vNPC 0.061683177 seconds



Specifying findNPC every time appears to take twice as long (in my benchmark anyway) compared to variable and direct methods.

Gambet 09-14-2009 07:59 PM

Quote:

Originally Posted by fowlplay4 (Post 1523064)
Specifying findNPC every time appears to take twice as long (in my benchmark anyway) compared to variable and direct methods.

According to Stefan, it's similar to how it works when you check boolean values.

This:

PHP Code:

function onCreated() {
  
temp.bool true;
  if (
bool == true) {
    
//Stuff
  
}


Is supposed to take longer to process than this:

PHP Code:

function onCreated() {
  
temp.bool true;
  if (bool) {
    
//Stuff
  
}



Mark Sir Link 09-14-2009 08:27 PM

Quote:

Originally Posted by sssssssssss (Post 1522647)
Ive been just using examples. What im actually doing is i have guilds register on a server to have and give rights to members for certain things.
when it creates the array it creates it as
registeredguildsGUILDNAME={acct name, acct name}
so i need to search through the array names that are registeredguilds then find the GUILDNAME, so i can print out the entire list of the GUILDNAMES that are registered on my server in a gui.

any other ideas on how to go about that, please let me know.

for starters, don't use examples when you're asking for help. If you want help, ask what you want help for.

Like I said in a post previously, what you are requesting assistance for seems dubious at best. I don't believe there is any way to collect all the server variables at once by function, and since there isn't, you would have to know the names of the variables before hand. That being the case, you aren't really yielding any benefit by doubly checking them to see if registeredguilds is a part of the name.

I don't know how to explain this any better.

If there is a way to return all the server flags at once, as was said, you could iterate through that array and check if the array at whatever index in the for loop's name starts with or contains whatever string (in this case, regsistered guilds).

DustyPorViva 09-14-2009 08:57 PM

temp.servervars = getstringkeys("server.");

may work.

Skyld 09-14-2009 09:18 PM

Quote:

Originally Posted by Gambet (Post 1523074)
According to Stefan, it's similar to how it works when you check boolean values.

This:

PHP Code:

function onCreated() {
  
temp.bool true;
  if (
bool == true) {
    
//Stuff
  
}


Is supposed to take longer to process than this:

PHP Code:

function onCreated() {
  
temp.bool true;
  if (bool) {
    
//Stuff
  
}



This is, in fact, because an if () statement is eventually evaluating down to true.
PHP Code:

if (bool) 

... is saying "Is bool true?", whereas
PHP Code:

if (bool == true

... is saying "Is bool == true true?" (two evaluations instead of one). The difference is not huge though and it is not worth complaining about.


All times are GMT +2. The time now is 07:58 PM.

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