0 Favourites

[Solved] Trouble operating ReturnValue

  • So, I have a project with two levels of function:

    "One" is some basic math operation, here I want it to arbitrary return 10

    "Two" 's are different fashion of reusing the formula, here I want it to arbitrary return twice the previous result, so 20

    I set the return value of "Two" only once in the code, and yet, it does not return the same value at different point of the code:

    • If I call the function from the Outside, it will indeed return 20
    • If I request the return value from the Inside of the function, it returns me the same value as "One"? Why?!

    Even if put multiple "set returnvalue", the simple fact of requesting a result from another function locks the "inner" returnvalue to the result of that function. If I'd use raw numbers, then requesting the returnvalue from the inside would give me the right result.

  • Return 10

    Return 20

  • Why is it working this way?! Why does waiting nothing suddenly makes it work?

  • The problem is the way C2 returns the value. It doesn't really return it, it sets a (global) variable. So the code is the equivalent of:

    ReturnValue = 2*ReturnValue

    Which of coarse gets confused.

    Just use a local variable to store the first return value and set the new return value from it.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • korbaach

    So, you mean that if being returned the returnvalue of "One" instead of "Two", it's because the returnvalue of two isn't yet calculated when I request it?

    But then :

    Shouldn't I be returned 3 in the second case as well, instead of 10 ?

    So the code is the equivalent of:

    ReturnValue = 2*ReturnValue

    Which of coarse gets confused.

    Why does it confuse Construct? It's a thing I use all the time and again, for as long as it doesn't involve a function it works perfectly fine:

    -Set return value to 10

    -Set return value to 2*returnvalue

    will properly work, it's

    -Set return value to somefunction

    -Set return value to 2*returnvalue

    that messes up.

    I don't really see the relation here.

    ---

    To be clear I have worked around this in my project, at this point what I want is understanding why it behaves in such a weird way.

  • The ReturnValue is not really set on that exact moment that the action is used. The ReturnValue is set on the moment of 'leaving' the function. And available outside the function, it is then by defenition the return of the last called function. It is zero when the Set ReturnValue in the last called function is not used.

    So, again by definition, ReturnValue in 'its own function' is never refering to its 'own' ReturnValue.

    As blackhornet suggested, use a local variable to evaluate.

  • Instances

    Thanks. I had completely misunderstood how Function.ReturnValue was supposed to work.

    Somehow I thought it was meant to work inside the function and that it couldn't work outside of it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)