Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Gah, dereferencing (https://forums.graalonline.com/forums/showthread.php?t=63242)

jake13jake 01-05-2006 09:00 PM

Gah, dereferencing
 
So, what I'm trying to do is something like this

this.arr = {"0==1","1==1"};

but then later extract {false,true} out of that in another event. I know how to reference to variable names, but how do you... uh... get this problem done?

napo_p2p 01-05-2006 09:23 PM

I am not sure if this is what you want, but I'll give you what I think you are wanting.

Right now, you have this.arr containing two string values, but you want it to contain two boolean types. In that case, just put:
PHP Code:

this.arr = {== 1== 1}; 

Is that what you want, or do you want to convert the string types into boolean types?

Fry 01-05-2006 09:26 PM

eval() has been disabled, the only way you can execute code inside a string is to use putnpc2, so there is no real way to do what you want.

jake13jake 01-05-2006 10:53 PM

Well, I want to make a more secure warplink class where the boolean values it looks for when onPlayerTouchsMe() occurs are stored within the NPC containing the class. However, it would be more redundant and less user-friendly (like for tilers with limited scripting knowledge) if I required an extra name-specific function in every NPC containing the class. Otherwise you could just check if the boolean string array exists. Any other way wouldn't be even close to as efficient. I'm personally hoping that Stefan finds his way to this thread, because there's probably some way to do it, unless somebody else knows too. Otherwise, this should definitely be a requested feature.

ApothiX 01-06-2006 01:29 AM

Couldn't you tokenize the string, and do your own checks?

Yen 01-06-2006 01:40 AM

I'm tired and confused, but if you're trying to get the NPC to put booleans into an array, what napo said should have worked.


PHP Code:

echo("Boolean:" SPC (== 1SPC (== 1)); 

This returns 0 and 1.

If I'm way off on the subject, just ignore me ._.

jake13jake 01-06-2006 02:17 AM

Well, seems like everyone's way off the subject.
PHP Code:

function onCreated() {
  
//you can't test clientr.vars in onCreated serverside, 
  //so this would be out of the question
  
this.reqflags = {clientr.quest_dungeon[0],clientr.quest_dungeon[1] == 2}
}

function 
onPlayerChats() {
  
//not to mention, something that changes in 
  //the level could change the variable that you're looking for.
  
clientr.quest_dungeon[0] == 1;
}

function 
onPlayerTouchsMe() {
  
//I want to do all of my boolean checks here,
  //but indicate the booleans that I want to check in onCreated() function.
  //Make it easier for the users of the class.
  
if (!0 in this.reqflags) {
    
setlevel2 this.level[0],this.newx[0],this.newy[0];
  }


Now, do you see my point?

napo_p2p 01-06-2006 02:36 AM

Try:
PHP Code:

if (!(0 in this.reqflags)) 


jake13jake 01-06-2006 05:40 AM

gah, you're still not getting the point. I'll post the class.
PHP Code:

//this.w: width of link (in tiles)
//this.h: height of link (in tiles)
//this.reqdir: required dir to pass the link 
//-1,0,1,2,3 : superfluous, up, left, down, right

//--------NOT DONE
//this.reqflags[]: two-dimensional array representing booleans 
//to be evaluated
//if no false in the boolean representation of this.reqflags[0], 
//it warps to this.level[0],this.newx[0],this.newy[0]
//level[]: array of levels for which is the first case this.reqflags is true
//newx[]: array of newxs parallel to level[]
//newy[]: array of newys parallel to level[]


function onCreated() {
  
setshape(1,this.w*16,this.h*16);
}

function 
onPlayerTouchsMe() {
  for (
i=0i<this.reqflags.size(); i++) {
    
/*------THIS IS WHERE I NEED TO EFFING EVALUATE THE BOOLEANS---*/
    
if (!0 in this.reqflags[i]) {
      if (
this.reqdir in {player.dir,-1}) { 
        
setlevel2(this.level[0],this.newx[0],this.newy[0]);
      }
    }
  }


Here would be an NPC ex.
PHP Code:

join("warplink");
function 
onCreated() {
  
this.2;
  
this.1;
  
this.reqdir = -1;
  
/*---IF I DIDN'T STRESS ENOUGH, YOU CAN'T EVALUATE THESE YET---*/
  
this.reqflags = {"!clientr.quest_dungeon[0]","clientr.quest_dungeon[0]"};
  
this.level = {"quest_dungeon_15.nw","quest_dungeon_15_flooded.nw"};
  
this.newx = {12.5,12.5};
  
this.newy = {30,30};



ZeLpH_MyStiK 01-06-2006 06:23 AM

Why can't you just set this.reqflags on playertouchsme so that it'll just evaluate then?

jake13jake 01-06-2006 07:44 AM

Quote:

Originally Posted by ZeLpH_MyStiK
Why can't you just set this.reqflags on playertouchsme so that it'll just evaluate then?

that would temporarily solve the problem, but I'd still want to be able to initialize them on created. My scripting philosophy is set everything you want to keep the same on created, so it would really be going against it, but I could do it :(

Skyld 01-06-2006 03:21 PM

I am not sure I entirely understand what you are trying to say, but is this what you mean?
PHP Code:

for (temp.0temp.this.reqflags.size(); temp.++)
{
  if (!(
0 in (@ this.reqflags[temp.i])))
  {
    
player.setLevel2(this.level[temp.i], this.newx[temp.i], this.newy[temp.i]);
  }


Edit: I do agree with just evaluating it on onPlayerTouchsMe. I think you are making this quite complicated for yourself trying to stick to a philosophy that won't easily (or at all) work everywhere.

prozacboy666 01-06-2006 05:53 PM

Quote:

Originally Posted by jake13jake
So, what I'm trying to do is something like this

this.arr = {"0==1","1==1"};

but then later extract {false,true} out of that in another event. I know how to reference to variable names, but how do you... uh... get this problem done?


Well do an if statement to see if it is a one, if it is = true, if not = false?
Or create a 2d array, and then do this.arr[][1]= true; else this.arr[][0]=false,
*edit: forgot to add this*
then do an if (this.arr[][1]=="true"){ do this;}

I dont know GS2 so I dont know how to put it in code for you.
If that doesn't help, well will talk it in depth later.

Skyld 01-06-2006 06:06 PM

Quote:

Originally Posted by prozacboy666
Well do an if statement to see if it is a one, if it is = true, if not = false?
Or create a 2d array, and then do this.arr[][1]= true; else this.arr[][0]=false,
*edit: forgot to add this*
then do an if (this.arr[][1]=="true"){ do this;}

I dont know GS2 so I dont know how to put it in code for you.
If that doesn't help, well will talk it in depth later.

I fear that you have no idea what you are talking about.

That isn't what he's trying to do.

jake13jake 01-06-2006 11:54 PM

Yea, just evaluating onPlayerTouchsMe is the best idea, but limiting myself to knowing only one way is a bad idea :(

Admins 01-07-2006 12:25 AM

There is also the possibility to use makevar() which doesn't accept array brackets though, nothing really dynamic except the name of the variable.

jake13jake 01-07-2006 01:03 AM

Quote:

Originally Posted by Stefan
There is also the possibility to use makevar() which doesn't accept array brackets though, nothing really dynamic except the name of the variable.

so I could run through a for loop and use makevar()? I'll try that. Hmmm... actually, this creates a challenge... scattered arrays on top of multidimensional.

Yea, I'll test this, don't know if temp. is local to the block of code only though.

PHP Code:

function evaluate(arr) {
  
temp.myBools = new[arr.size()];
  for (
i:arr)
    if (
arr[i].size 1)
      
temp.myBools[i] = evaluate(arr[i]);
    else 
      
temp.myBools[i] = makevar(arr[i]);
  return 
temp.myBools;


or wait, i don't think I understand this at all anymore.

Inverness 01-07-2006 01:31 AM

Quote:

Originally Posted by Stefan
There is also the possibility to use makevar() which doesn't accept array brackets though, nothing really dynamic except the name of the variable.

I would prefer it if you could somehow reenable eval() and put some kind of safety on it to prevent bad stuff that I hear about it, or enable for Hosted servers maybe, since i've believe they have passed the level where they would abuse something like that?
I want to attach conditions to npc topics in the database but can't since it is not possible to read conditions from a string, which is what is limiting me at the moment.

jake13jake 01-07-2006 01:49 AM

Quote:

Originally Posted by Inverness
I would prefer it if you could somehow reenable eval() and put some kind of safety on it to prevent bad stuff that I hear about it, or enable for Hosted servers maybe, since i've believe they have passed the level where they would abuse something like that?
I want to attach conditions to npc topics in the database but can't since it is not possible to read conditions from a string, which is what is limiting me at the moment.

Well, what I've seen of eval(), I would disagree. There are other approaches. However, it would be very very nice if there were a function that would accept a string something like "1 + 3" or "0 == 0" or "0==0 + 3" and return a value. Like anything the assignment operator would take. Actually, would be quite nice for a simple calculator script, not only this script. Also, while on string stuff, does tokenize() have the ability to return delimiters in gscript2? I haven't played with it at that extent much at all.

ApothiX 01-07-2006 04:00 AM

Quote:

Originally Posted by Inverness
I would prefer it if you could somehow reenable eval() and put some kind of safety on it to prevent bad stuff that I hear about it, or enable for Hosted servers maybe, since i've believe they have passed the level where they would abuse something like that?
I want to attach conditions to npc topics in the database but can't since it is not possible to read conditions from a string, which is what is limiting me at the moment.

I think he's more worried about the classic servers abusing eval(), than he is about hidden servers that have no playerbase to annoy/make them complain.

Admins 01-08-2006 05:26 AM

There has never been an eval() function.
I don't think that adding such type of function would be really good, it would be much better to think of better ways of doing what you want to do. It would only be a hack.
Why not letting the joining npc provide a function which is called by your code? Since level makers are doing copy&paste anyway it shouldn't be a problem. I don't think providing a boolean evaluation inside the onCreated function is easier than to copy the check-function.

jake13jake 01-08-2006 06:45 AM

Quote:

Originally Posted by Stefan
There has never been an eval() function.
I don't think that adding such type of function would be really good, it would be much better to think of better ways of doing what you want to do. It would only be a hack.
Why not letting the joining npc provide a function which is called by your code? Since level makers are doing copy&paste anyway it shouldn't be a problem. I don't think providing a boolean evaluation inside the onCreated function is easier than to copy the check-function.

geez stefan, you're making this hard. now you have me wanting to make my own custom tokenizers for parsing calculation scripts in gscript and everything. btw, pls give tokenize() the option to return the delimiters. it would increase my love for you tenfold. Perhaps even having whole string delimiters rather than simply characters.

ApothiX 01-08-2006 12:32 PM

Quote:

Originally Posted by jake13jake
geez stefan, you're making this hard. now you have me wanting to make my own custom tokenizers for parsing calculation scripts in gscript and everything. btw, pls give tokenize() the option to return the delimiters. it would increase my love for you tenfold. Perhaps even having whole string delimiters rather than simply characters.

Return the delimiters? Why would you want that? Just add them manually to whatever you're doing if you need them that badly.

I, for one, would rather not have commas and other crap in my tokens to make them look ugly :|

jake13jake 01-08-2006 01:15 PM

Quote:

Originally Posted by ApothiX
Return the delimiters? Why would you want that? Just add them manually to whatever you're doing if you need them that badly.

I, for one, would rather not have commas and other crap in my tokens to make them look ugly :|

you seriously underestimate their usefulness... although, i did make a function that does it in a really really ugly sense... kindof taking advantage of how string lists are interpreted as arrays.

PHP Code:

function tokenizeWithDelims(string,delims) {
  
temp.list = "";
  for (
i=0i<string.length(); i++) {
    if (
delims.pos(string.charat(i)) == -1)
      
temp.list @= string.charat(i);
    else {
      if (
!= 0)
        
temp.list @= ",";
      
temp.list @= string.charat(i);
      if (
!= string.length()-1)
        
temp.list @= ",";
    }
  }
  return 
temp.list;



Skyld 01-08-2006 07:22 PM

Quote:

Originally Posted by jake13jake
you seriously underestimate their usefulness... although, i did make a function that does it in a really really ugly sense... kindof taking advantage of how string lists are interpreted as arrays.

PHP Code:

function tokenizeWithDelims(string,delims) {
  
temp.list = "";
  for (
i=0i<string.length(); i++) {
    if (
delims.pos(string.charat(i)) == -1)
      
temp.list @= string.charat(i);
    else {
      if (
!= 0)
        
temp.list @= ",";
      
temp.list @= string.charat(i);
      if (
!= string.length()-1)
        
temp.list @= ",";
    }
  }
  return 
temp.list;



Hold on a second.

Why don't you just do string.tokenize(delimiter)?

Admins 01-08-2006 09:46 PM

Yes just pass the delimiters to the tokenize function, if you don't pass any delimiters then it is using the default ones (comma).

jake13jake 01-08-2006 09:48 PM

omg, nobody understands the concept of returning delims, where am i?

Skyld 01-08-2006 10:17 PM

Quote:

Originally Posted by jake13jake
omg, nobody understands the concept of returning delims, where am i?

I understand the concept, but the function you wrote practically does what you require. Therefore, why is it worth the time changing the way tokenize() works?

I'm not sure I can think of one application where having the delimiter automatically returned with each token is of any benefit.

Admins 01-09-2006 03:32 AM

Well I could imagine that it could be useful for some cases to get an array of the separate tokens plus the delimiters, if you have more than one delimiter and want to restore the original string afterwards. The tokenizeWithDelims function should add to the temp.list though (use it as array) and not append the string with comma etc.

jake13jake 01-09-2006 07:00 AM

Quote:

Originally Posted by Stefan
Well I could imagine that it could be useful for some cases to get an array of the separate tokens plus the delimiters, if you have more than one delimiter and want to restore the original string afterwards. The tokenizeWithDelims function should add to the temp.list though (use it as array) and not append the string with comma etc.

yea, i'm just being lazy because I counting the number of tokens would be too much work, lol. Although sure, I'll do it. Still would be better built into tokenize somehow though. Maybe you could make an optional parameter. It's most useful function though, would be if you wanted to separate different kinds of data and use the delimiter as functionality to that data. This example wouldn't be good in graal, but let's say you had
PHP Code:

string="/deposit 20g";
tokens string.tokenizewithdelims("/");
if (
tokens[0] == "/") {
  switch (
tokens[1].trim()) {
     case 
"deposit":
        
moretokens tokens[2].tokenizewithdelims("gt$");
        if (
moretokens[0].type() == int) {
           switch (
moretokens[1]) {
             case 
"g"depositingelat(moretokens[0]); break;
             case 
"t"depositintickets(moretokens[0]); break;
             case 
"$"depositindollars(moretokens[0]); break;
        }
     case 
"withdraw"blah; break;
     case 
"transfer"blah; break;
  }


probably not the best example. a better one might be
PHP Code:

string="2+2";
string.tokenizewithdelims(+-*/);
switch (
tokens[1]) {
  case 
"+": return tokens[0]+tokens[2];
  case 
"-": return tokens[0]-tokens[2];
  case 
"*": return tokens[0]*tokens[2];
  case 
"/": return tokens[0]/tokens[2];


well yea, the .trim() in the first one kindof makes it seem to me now, that the best way to put this functionality into gscript might be
string.tokenize(ignoreddelims,returneddelims);
where string.tokenize() is an overloaded function wherever it comes from in the script.

Would also be cool if you could base tokens off of strings, but it's easier to get around that than with single character delims.


All times are GMT +2. The time now is 12:54 AM.

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