How to return from a function?

0 favourites
  • 10 posts
  • I have a fairly complex function where I want to return from multiple points, depending on logic. I *could* use Else blocks but I want the function to exit as soon as possible and it'd keep the logic cleaner too (I need to use extra local variables to keep track of some conditions when a <font face="Courier New, Courier, mono">return</font> would simply leave the function.)

    The <font face="Courier New, Courier, mono">Function</font> object doesn't have anything like this - is there a plugin that adds the ability? If not, is it possible to do this with a plugin? (I took a quick look at the SDK but it seems that plugin functionality is limited to functions being called from C2 but it doesn't seem possible to actually inject Javascript instructions into a C2 function at a given place.)

  • I'm not sure what you'd like to do that can't be done with functions, parameters and possibly a loop or a while event.

    Please explain further or give an example of what you mean.

  • If you can't make a code.. draw some kind of schema with more details of what you are trying to do.. I'm quite sure someone here will be able to fill in the blanks.

  • Thanks but I'm not sure I understand either reply.

    I simply asked if it's possible to use a plugin to inject a return statement into a function's logic in C2 since the Function object only allows setting a return value but doesn't terminate the function.

  • I don't think we're talking the same language.

    I'm talking in Construct2 event-system language.

    If you could explain in Construct2 language what you want or even in plain english, I might be able to help.

    What is a return-statement and what is it used for?

    Why should a function be terminated?

  • I don't have a solution for this.. would like to give it a try at explaining the issue.

    The "return" statement of usual programming / scripting languages allows the program to exit from a function call at any point in the function.

    A simple application of the return statement is in a search function, we would want to exit the function once we found the object that we are searching for. Since there's no "return" statement in Construct2's function object, a working but bulky solution would be to keep a variable for e.g. "done" to keep track of the completion of a task. The code becomes bulky, looks more complex the more points of exit there are. Forgetting one check for the "done" variable can mean a lot of time spent debugging.

    A pseudo code (in a script language like Javascript) of a function to illustrate the issue:

    var globalVar1 = 0;
    function foo(){
       ...
       while(someCondition ){
         ...
         if( someCondition ) 
            return something;
         while(someCondition ){
            ...
            if( someCondtion ){
                return something;
            }
            globalVar1++;
         }
       }
    }
    

    Pseudo code for the same function in Construct 2:

    global var globalVar1  = 0;
    function fooC2(){
      ...
       <font color=orange>local variable done = 0;</font>
       while(someCondition <font color=orange>AND done = 0</font>){
         ...
         if( someCondtion ){
             function -> set return value = something;
             <font color=orange>done = 1;</font>
         } 
         while( someCondition <font color=orange>AND done = 0</font>){
            ...
            if( someCondition ){
               function -> set return value = something;
               <font color=orange>done = 1;</font>
            }
            if(<font color=orange>done = 0</font>){
               globalVar1 = globalVar1 +1;
            }
         }
      }
    }
    

    Sorry that I couldn't think of a better example, but I remember having this issue too. It doesn't make it impossible to get the same output, but it makes the code bulky.

  • aruche, thanks for typing all that up. Since there were no replies to the thread (and since I couldn't find anything to the contracry), I assume the answer is no.

  • xxbbcc There is no Return statement in C2's Function object.

    You can set the return value of a function at any point, but that's it.

    One trick you can use is to add an event "if myLocaleReturnValue is still 0" to every event that shouldn't be triggered once the return has been decided, but that's it. It won't be clearer than using Else.

    In the last part of your initial post, you asked if there was a way to inject JS into C2. There is, and you don't need plugin for this. The Browser object (or the System, I'm not sure right now) has a "evalJS" property (to be used inline, will be evaluated and the return value will be sent back and used in the expression) or a callJS action (to be used as a complete event). So a solution to your problem would be to write a function in javascript that will return the value you wanted in C2. You will give it arguments through the evalJS function, transmitting the useful state data you need from C2, and will be able to use the return value in C2.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Guizmus: thank you, the use of the Browser object is a good idea, although I probably won't do it in this case - I assume that the overhead of "eval" is far more expensive than a few extra local variables and "else"-s.

  • Why not simply split it up into several functions?

    function foo(){
       ...
       call Anotherfunction(){
         ...
         if( returnvalue = 1) 
            return 1;
         else(call YetAnotherFunction ){
            ...
            if(Returnvalue = 2){
                return 2;
            }
    
         }
       }
    }
    [/code:2m08bv8y]
    
    To me it looks a bit like you try to just throw everything into one function where they might not be needed. Instead of just splitting them into several functions that does something more specific.
    
    Function Set car() : Set values of the car with UID
    
    Function Find car() : Return the UID of the car based on some input.
    
    Function Delete car() : Delete car with UID.
    
    etc...
    
    In your example it looks like you have a function, that should do something to something, and based on what it does it might have to do something else. But you are not really sure what it is suppose to do. But maybe i misunderstood what you are trying to do, or what exactly your problem is.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)