Results 1 to 7 of 7

Thread: Having a func or proc call itself.

  1. #1
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default Having a func or proc call itself.

    Is it possible to have a function or procedure call itself? If not I can just use a loop, but i get an access violation having my function call itself. Just wondering if there's a workaround or something. I recall reading about having a function call itself as a form of loop but forget if it works with simba.

    Thanks
    Non-RS scripter, if you need a hand with scripting let me know. I have a decent amount of experience scripting for non-RS games.

    How to add text messaging to any script: http://villavu.com/forum/showthread....98#post1151998

  2. #2
    Join Date
    Nov 2012
    Posts
    2,351
    Mentioned
    55 Post(s)
    Quoted
    603 Post(s)

    Default

    Have you tried using

    Code:
    Continue;
    I believe that starts the current Begin/End loop again, works for everything I've had to do


    Programming is like trying keep a wall of shifting sand up, you fix one thing but somewhere else starts crumbling

  3. #3
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    What you're talking about is recursion. If you write your function correctly it is very possible. You could also try the loop and use the continue keyword to go to the top and the break keyword to break out of it.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  4. #4
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    are you using proper recursive method implementation?

    ex. (from web - translated to ps)
    Simba Code:
    program Factorial;

    function fact(n: integer): string;
    begin
      if (n = 1) then
      begin
        result := '1';
        exit;
      end else
      begin
        result := IntToStr(n) + ' * ' + (fact(n - 1));
        exit;
      end;
    end;

    begin
      writeln(fact(10));
    end.

  5. #5
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Quote Originally Posted by DannyRS View Post
    Have you tried using

    Code:
    Continue;
    I believe that starts the current Begin/End loop again, works for everything I've had to do
    how would that work inside an If statement?

    here's the code trying to call itself and failing: what i've tried so far lol.
    Simba Code:
    Function SelectFromMiningOverView(x: integer): boolean;
    begin
    if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
    begin
    SelectFromMiningOverView(x+1);
    end else
    begin
    SelectFromOverView(x);
    end;
    end;

    I know I could just put the if statement inside a loop and have Exit; right after it gets into the second part of the If. But I try to mix things up and learn new things. You can just yell at me if i'm being stupid though.
    Last edited by xdarkshadowx; 01-05-2013 at 08:53 AM.
    Non-RS scripter, if you need a hand with scripting let me know. I have a decent amount of experience scripting for non-RS games.

    How to add text messaging to any script: http://villavu.com/forum/showthread....98#post1151998

  6. #6
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Using a loop is much simpler and safer than making a recursive function, in fact i don't think recursive functions should be used in PS due to some memory bugs?

    Simba Code:
    procedure SelectFromMiningOverView(x: integer);
    begin
      while True do
        if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
          Inc(x)
        else
        begin
          SelectFromOverView(x);
          Exit;
        end;
    end;

  7. #7
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    Using a loop is much simpler and safer than making a recursive function, in fact i don't think recursive functions should be used in PS due to some memory bugs?

    Simba Code:
    procedure SelectFromMiningOverView(x: integer);
    begin
      while True do
        if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
          Inc(x)
        else
        begin
          SelectFromOverView(x);
          Exit;
        end;
    end;
    Yeah, I was trying to mess around with it just now but it seems my Access violation error is due to this line:
    Simba Code:
    if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then

    no clue why that's invalid though. hm.
    Non-RS scripter, if you need a hand with scripting let me know. I have a decent amount of experience scripting for non-RS games.

    How to add text messaging to any script: http://villavu.com/forum/showthread....98#post1151998

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •