Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 09-05-2017, 03:56 AM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,731
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
Script ignoring if statement conditions?

This is a small sample of a larger script I wrote.

PHP 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?
__________________
Save Classic!
Reply With Quote
  #2  
Old 09-05-2017, 03:53 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Random guess: in FindPrefix(), try wrapping the return in your else in curly braces (heh).
__________________
Reply With Quote
  #3  
Old 09-05-2017, 05:01 PM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,731
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
Quote:
Originally Posted by Crow View Post
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.
__________________
Save Classic!
Reply With Quote
  #4  
Old 09-05-2017, 11:21 PM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,731
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
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.
__________________
Save Classic!
Reply With Quote
  #5  
Old 09-05-2017, 11:39 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
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:

PHP 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);


Last edited by DustyPorViva; 09-06-2017 at 12:52 AM..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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