functions that return more than one value

0 favourites
  • 4 posts
From the Asset Store
Game "Little Dino Adventure Returns" with complete Source-Code (Construct3 / .c3p) + HTML5 Exported.
  • hi...how do you return several values with one function?

    thanks!

    Dan

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • to set multiple return values

    Function.setReturnValue( value & "," &value2 )

    to get return value

    var = tokenate(Function.returnValue, index, ",")

    C2 doesn't support multiple natural return values. I know some lanuages do. it's a nice feature. But C2 isn't one of them much like C/Java....

  • Put return values into a dictionary or an array.

  • Dictionary/Arrays could work but if the function is recursive or calls other functions which rely on the same Dictionary/Array you could accidentally overwrite existing return values (unless you create dict/array each time).

    I've used the string splitting option before for returning values.

    Another approach that I use is creating specific Return Objects. So for example, if I have a function that needs to return a IsStunned boolean, a Damage number variable, and a string TypeOfAttack, then I would create a Sprite object called RetAttack which would have IsStunned, TypeOfAttack, and Damage as instance variables.

    The function would then spawn a new instance of this object, assign the variable values, and the function's return value would be the UID of the RetAttack object.

    I like this approach since it provides strongly-typed returns that can be complex objects, and there is no unnecessary parsing/type conversions either (as would be the case if you joined all three in a single string).

    After returning, you would pick RetAttack by the ret UID and extract the return values (TypeOfAttack, IsStunned, Damage) then destroy the RetAttack object.

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