Graal Forums

Graal Forums (http://forums.graalonline.com/forums/index.php)
-   NPC Scripting (http://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Script ignoring if statement conditions? (http://forums.graalonline.com/forums/showthread.php?t=134270546)

maximus_asinus 09-05-2017 04:56 AM

Script ignoring if statement conditions?
 
This is a small sample of a larger script I wrote.

Graal Script Code:

if (this.item in db_quests.items3) {
  
temp.prefix FindPrefix();
  if (
temp.prefix == -1) {
    echo(
"Level not found in quest database");
  }
  else {
    
// CODE
  
}
}

// find NPC level prefix then check if it is in the quest database
function FindPrefix() {
  for (
temp.a=0;temp.a<=this.level.length();temp.a++) {
    
temp.search this.level.substring(temp.a,1);
    if (
temp.search == "_") {
      
temp.checkprefix this.level.substring(0,temp.a);
      if (
temp.checkprefix in db_quests.quests) {
        return 
temp.checkprefix;
      }
      else return -
1;
    }
  }


The problem is the if statement temp.prefix == -1 in the first block of code. Despite findPrefix returning a value not equal to -1 (verified by echo) it will activate regardless and the else statement will be ignored. Any ideas as to why?

Crow 09-05-2017 04:53 PM

Random guess: in FindPrefix(), try wrapping the return in your else in curly braces (heh).

maximus_asinus 09-05-2017 06:01 PM

Quote:

Originally Posted by Crow (Post 1741902)
Random guess: in FindPrefix(), try wrapping the return in your else in curly braces (heh).

I've further debugged the script and completely removed the return -1 else statement in FindPrefix, yet the if statement still returns as if it is true. Now I have absolutely no idea how that is happening.

maximus_asinus 09-06-2017 12:21 AM

I modified the script to return a string instead of a numerical value which has corrected the problem. I'm interest in understanding why I couldn't mix a these two value types in a comparison.

DustyPorViva 09-06-2017 12:39 AM

You should use this.level.name instead of this.level. While this.level will work, it's actually giving the level object and not a string name of the level. But because of how typecasting in Graal works it can still work in most situations but you can sometimes encounter oddities.

Also, why not just return null or false? Again, typecasting can be a bit weird here but unless "0" is a possible prefix null/boolean would be much more logical.

But some more tidbits of information:

You can use this.level.name.charat(temp.a) instead of substring if you just want a single character and not a substring. Also, you can use this.level.name.pos("_") to find the first instance of string within a string. -1 will be returned if the string is not found(since 0 can be returned if the character is the first character in the string). So you can do something like this, which is much more efficient:

Graal Script Code:

if (this.item in db_quests.items3) {
  
temp.prefix findPrefix(this.level.name);
  if (
temp.prefix == null) {
    echo(
"Level not found in quest database");
  } else {
    
// CODE
  
}


function 
findPrefix(temp.string) {
  
temp.pos temp.string.pos("_");
  if (
temp.pos 0) return null;
  return 
temp.string.substring(0,temp.pos);




All times are GMT +2. The time now is 05:42 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.