Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Function not returning a value (https://forums.graalonline.com/forums/showthread.php?t=134268589)

baseman101 08-05-2013 09:11 AM

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 :)

callimuc 08-05-2013 11:02 AM

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 :/

cbk1994 08-05-2013 12:02 PM

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.

fowlplay4 08-05-2013 03:33 PM

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.

baseman101 08-05-2013 05:51 PM

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.

baseman101 08-06-2013 07:59 AM

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 :D

cbk1994 08-06-2013 10:47 AM

Quote:

Originally Posted by baseman101 (Post 1721537)
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

baseman101 08-07-2013 05:27 AM

Quote:

Originally Posted by cbk1994 (Post 1721538)
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!


All times are GMT +2. The time now is 10:43 PM.

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