Results 1 to 17 of 17

Thread: Is there a way to return to a function within a loop?

  1. #1
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default Is there a way to return to a function within a loop?

    As title states. I don't have the code in front of me as i'm at work but here's an example.

    MainLoop is set to repeat until false.

    Simba Code:
    procedure mainLoop;
    Proc1;
    if Proc1() then
    Proc2;
    if Proc2() then
    Proc3;
    if Proc3() then
    Proc4;


    Simba Code:
    function proc3: Boolean;
    begin
    //code to result in true
    //code to result in false
    if result := false then
    //return back to mainloop to Proc2()
    end;
    Last edited by Clutch; 03-01-2016 at 05:48 PM.

  2. #2
    Join Date
    Jun 2014
    Posts
    463
    Mentioned
    27 Post(s)
    Quoted
    229 Post(s)

    Default

    well if proc 2 just sends you to proc 3 why not put a repeat until result inside of proc3 o.o

    e: not sure if this is what you mean, but the scan and react method? https://villavu.com/forum/showthread...549&highlight=
    Tsunami

  3. #3
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by Lucidity View Post
    well if proc 2 just sends you to proc 3 why not put a repeat until result inside of proc3 o.o

    e: not sure if this is what you mean, but the scan and react method? https://villavu.com/forum/showthread...549&highlight=
    I'll talk to you tonight about it. I usually do scan and react but I haven't been able to wrap my head around a way to make this work correctly and efficiently. Right now I just have them going in order. I don't want it to loop back through - basically it hits each process only once. Although that just brought up an idea.

    Edit: This is hard to explain without the actual code. Sorry probably makes no sense.
    Basically it's unlocking lodestones - so I'm thinking on a true result just to use an inc() and at the beginning of the process if the inc >=1 I can just exit and have it continue looping through, that way if it does fail out, it'll retry on the one's it missed.

  4. #4
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    Quote Originally Posted by Clutch View Post
    As title states. I don't have the code in front of me as i'm at work but here's an example.

    MainLoop is set to repeat until false.

    Simba Code:
    procedure mainLoop;
    Proc1;
    if Proc1() then
    Proc2;
    if Proc2() then
    Proc3;
    if Proc3() then
    Proc4;
    is this it what you mean? you dont have to actually use 'Proc1;' the code will just execute in an if statement.
    Simba Code:
    if (Proc1()) then
      if (Proc2()) then
        if (Proc3()) then
          Proc4();

  5. #5
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

    Default

    write your code differently, or use gotos.

    But dont acutally use gotos.

  6. #6
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Use loop states with fall-through switch cases.

    Simba Code:
    case (state) of
      somestate:
        proc1();
      somesecondstate:
        proc2();
    end;

    Change the state in each proc when it completes successfully
    Last edited by tls; 03-01-2016 at 10:14 PM.

  7. #7
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by hoodz View Post
    is this it what you mean? you dont have to actually use 'Proc1;' the code will just execute in an if statement.
    Simba Code:
    if (Proc1()) then
      if (Proc2()) then
        if (Proc3()) then
          Proc4();
    Yeah, but also so that it doesn't repeat anything. So if Proc2 were to fail for example - it'd loop back to 1 and then 2 then 3. I'm trying to avoid that which is why scan react seems the way to go, but implementing that is going to take some thinking. I'll post the actual code tonight but I'm thinking I need to just implement my walk feature with unlocking as well. So I can use like @tls; mentioned below to scan if it's picking up something to then react accordingly. Which if I have everything within a record will be able to make everything short and simple.

    Quote Originally Posted by tls View Post
    Use loop states with fall-through switch cases.

    Simba Code:
    case (state) of
      somestate:
        proc1();
      somesecondstate:
        proc2();
    end;

    Change the state in each proc when it completes successfully
    I've never used anything like this - wouldn't this be another method of the scan react method?
    Last edited by Clutch; 03-01-2016 at 10:33 PM.

  8. #8
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Quote Originally Posted by Clutch View Post
    I've never used anything like this - wouldn't this be another method of the scan react method?
    That's called a FSM or finite-state machine

    iirc the scan and react method (haven't looked at the thread) is just FSM without states
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  9. #9
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by KeepBotting View Post
    That's called a FSM or finite-state machine

    iirc the scan and react method (haven't looked at the thread) is just FSM without states
    From my understanding then this is just using a case where as the thread uses a chain of else if statements.

  10. #10
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Quote Originally Posted by Clutch View Post
    From my understanding then this is just using a case where as the thread uses a chain of else if statements.
    Essentially yes

    FSM will either set the state every time it leaves the previous routine, or use a 'decider' function which is called every time the state needs to be determined

    People sometimes say FSM is 'better' than traditional loop-based scripts but they'll function the same if written well

    FSM is, imo, a better way to write a script. Helps the logic flow more smoothly, keeps you from making mistakes. Has very little bearing on how the script runs, FSM and loop-based scripts will be just as efficient as each other if they're written properly

    FSM done right can get even cooler with custom types and pointers to procedures/functions, https://villavu.com/forum/showthread.php?t=58300
    Last edited by KeepBotting; 03-02-2016 at 12:12 AM.
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  11. #11
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    What I posted isn't true FSM. True FSM should break after every state.

  12. #12
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by tls View Post
    What I posted isn't true FSM. True FSM should break after every state.
    Just googled that and it seems to be making sense, albeit going to be a mess and very time consuming to make it "perfect" as there's a large number of variables and failsafes that would need to be considered.

    Anywho - what is the difference between break and exit?

  13. #13
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Quote Originally Posted by Clutch View Post
    Just googled that and it seems to be making sense, albeit going to be a mess and very time consuming to make it "perfect" as there's a large number of variables and failsafes that would need to be considered.

    Anywho - what is the difference between break and exit?
    break breaks out of a loop, exit exits the function entirely
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  14. #14
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by KeepBotting View Post
    break breaks out of a loop, exit exits the function entirely
    So is a case of considered a loop? I think not, but want verification.

  15. #15
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by Clutch View Post
    So is a case of considered a loop? I think not, but want verification.
    No it is not a loop. Break stops executing a control structure such as a loop or switch.

    Edit: Turns out cases in lape automatically break. So it is true FSM.
    Last edited by tls; 03-02-2016 at 01:16 AM.

  16. #16
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Quote Originally Posted by tls View Post
    No it is not a loop. Break stops executing a control structure such as a loop or switch.

    Edit: Turns out cases in lape automatically break. So it is true FSM.
    Yeah I wanted to say that but I wasn't completely sure what you were meaning

    i.e. in Java if you don't break after a case, it'll continue all the way to the last case regardless of which one was actually satisfied. But in Lape as soon as a case is satisfied, it breaks

    Quote Originally Posted by Clutch View Post
    So is a case of considered a loop? I think not, but want verification.
    it's not, no
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  17. #17
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

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
  •