Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-05-2013, 09:11 AM
baseman101 baseman101 is offline
Scripter
baseman101's Avatar
Join Date: Nov 2012
Location: Purcellville, VA
Posts: 76
baseman101 will become famous soon enough
Function not returning a value

Hello,

I have made a pretty neat function that replaces a value from inside of a string to another value. This would have worked great, but for some reason, it's not returning the correct value. I have echoed the value before it is returned, and that value is correct, but it will not work for the return.

PHP Code:
function onCreated() {
    echo(
replaceVal("Testing-123-456","-""huh"));
}

function 
replaceVal(temp.stringtemp.oldvaltemp.newval) {
  
temp.xa 0;
  for(
temp.val 0temp.val temp.string.length(); temp.val++) {
    
temp.arr null;
    
temp.arr.add(temp.string.substring(temp.valtemp.oldval.length()));
    for(
temp.val2 0temp.val2 temp.arr.size(); temp.val2++) {
      if(
temp.arr[temp.val2] == temp.oldval) {
        
temp.newstring temp.string.substring(0temp.val) @ temp.newval temp.string.substring(temp.val temp.oldval.length());
        
temp.xa++;
      }
    }
  }
  if(
temp.xa 1) {
    
replaceVal(temp.newstringtemp.oldvaltemp.newval);
  }
  else {
    echo(
temp.newstring);  //Correctly echoes Testinghuh123huh456
    
return temp.newstring;  //Does not return Testinghuh123huh456
  
}

This -should- have returned Testinghuh123huh456. It echoes correctly as I said in a comment in my script too.

Thanks in advance for helping me
Reply With Quote
  #2  
Old 08-05-2013, 11:02 AM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
you might want to check this replaceText() function by Tigairus as it is requiring less loops.

I'm not really sure about your problem though :/
__________________
MEEP!
Reply With Quote
  #3  
Old 08-05-2013, 12:02 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Without spending much time trying to understand your script, it's clear that your recursion is flawed. replaceVal(temp.newstring, temp.oldval, temp.newval); may or may not return anything, but it doesn't matter because you don't return that value, so the original call to the function won't see it. In other words, if the if-statement evaluates to true, which I'm guessing it does in the first call, there will never be a return value.

The echo you are seeing is actually from the deepest function call, and the value is returned correctly, but then you don't do anything with the return value when it is passed back to the same function. My guess is that simply adding return in front of the recursive call will fix it, but I'm not in a position to test it right now.
__________________
Reply With Quote
  #4  
Old 08-05-2013, 03:33 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Learn to recurse:

PHP Code:
function onCreated() {
  echo(
count(10));
}

function 
count(from) {
  if (
from 0) {
    echo(
from);
    
from from 1;
    return 
count(from);
  }
  echo(
0);
  return 
"Done";

Also don't actually use your function, use the one callimuc linked instead.
__________________
Quote:
Reply With Quote
  #5  
Old 08-05-2013, 05:51 PM
baseman101 baseman101 is offline
Scripter
baseman101's Avatar
Join Date: Nov 2012
Location: Purcellville, VA
Posts: 76
baseman101 will become famous soon enough
I knew my script was overkill :P. Anyway, thanks for linking Tig's script. I wasn't able to find any function to do this for me anyway.
Reply With Quote
  #6  
Old 08-06-2013, 07:59 AM
baseman101 baseman101 is offline
Scripter
baseman101's Avatar
Join Date: Nov 2012
Location: Purcellville, VA
Posts: 76
baseman101 will become famous soon enough
I don't want to spam all the forums with my threads, so here's another question. Is it possible to get the path of the selected node in a GuiTreeViewCtrl? For example, if Hey.txt is selected and that value is inside Hello, then it would return Hello/Hey.txt.

If there is not a current way to do this, a function like getSelectedNodePath() would be excellent
Reply With Quote
  #7  
Old 08-06-2013, 10:47 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by baseman101 View Post
I don't want to spam all the forums with my threads, so here's another question. Is it possible to get the path of the selected node in a GuiTreeViewCtrl? For example, if Hey.txt is selected and that value is inside Hello, then it would return Hello/Hey.txt.
Ideally you would create a separate thread for each issue so that it is easier for people with the same problem to find when searching the forum.

The wiki suggests that the following event exists for GuiTreeViewCtrl:

Quote:
onSelect(node,nodeslashpath,nodedotpath) - a node has been selected
__________________
Reply With Quote
  #8  
Old 08-07-2013, 05:27 AM
baseman101 baseman101 is offline
Scripter
baseman101's Avatar
Join Date: Nov 2012
Location: Purcellville, VA
Posts: 76
baseman101 will become famous soon enough
Quote:
Originally Posted by cbk1994 View Post
Ideally you would create a separate thread for each issue so that it is easier for people with the same problem to find when searching the forum.

The wiki suggests that the following event exists for GuiTreeViewCtrl:
Ah, I didn't look at the event functions. Thanks!
Reply With Quote
Reply


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 08:06 AM.


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