How to return from a function?

Get help using Construct 2

Post » Tue Feb 11, 2014 5:15 am

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 return would simply leave the function.)

The Function 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.)
B
7
S
3
Posts: 9
Reputation: 653

Post » Tue Feb 11, 2014 7:47 am

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.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Feb 11, 2014 12:00 pm

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.
Need help? Upload .capx file with your question.
B
16
S
4
G
2
Posts: 233
Reputation: 2,560

Post » Tue Feb 11, 2014 2:22 pm

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.
B
7
S
3
Posts: 9
Reputation: 653

Post » Tue Feb 11, 2014 2:40 pm

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 told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Feb 11, 2014 4:15 pm

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:
[CODE]
var globalVar1 = 0;
function foo(){
   ...
   while(someCondition ){
     ...
     if( someCondition )
        return something;
     while(someCondition ){
        ...
        if( someCondtion ){
            return something;
        }
        globalVar1++;
     }
   }
}
[/CODE]


Pseudo code for the same function in Construct 2:
[CODE]
global var globalVar1 = 0;
function fooC2(){
...
   local variable done = 0;
   while(someCondition AND done = 0){
     ...
     if( someCondtion ){
        function -> set return value = something;
        done = 1;
     }
     while( someCondition AND done = 0){
        ...
        if( someCondition ){
           function -> set return value = something;
           done = 1;
        }
        if(done = 0){
           globalVar1 = globalVar1 +1;
        }
     }
}
}
[/CODE]
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.aruche2014-02-11 16:17:57
B
163
S
120
G
9732
Posts: 42
Reputation: 4,875,331

Post » Tue Feb 18, 2014 5:08 am

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.
B
7
S
3
Posts: 9
Reputation: 653

Post » Tue Feb 18, 2014 8:29 am

@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.
B
17
S
8
G
4
Posts: 473
Reputation: 6,191

Post » Fri Feb 21, 2014 6:23 am

@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.
B
7
S
3
Posts: 9
Reputation: 653

Post » Fri Feb 21, 2014 1:26 pm

Why not simply split it up into several functions?

Code: Select all
function foo(){
   ...
   call Anotherfunction(){
     ...
     if( returnvalue = 1)
        return 1;
     else(call YetAnotherFunction ){
        ...
        if(Returnvalue = 2){
            return 2;
        }

     }
   }
}


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. :)
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Next

Return to How do I....?

Who is online

Users browsing this forum: Blendi1223, kellehk, TRMG, Yahoo [Bot] and 25 guests