Is it possible to return multiple values from a function?

0 favourites
  • 5 posts
From the Asset Store
Be quick and choose the right answer for the shown equation.
  • I want to call a function from within another function and have it return multiple values back to it. Is that possible?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No. You have to store them in some way. You'd have to provide more details for some suggestions.

  • Okay, thanks. I'm using some container objects to hold the values I need, seems to work fine for now.

    Here's the situation:

    I have this big movement function that I decided to break up into several to make it all a bit less unwieldy. The main function have a few local variables that are used to calculate how much this object will push other objects around. At the end of the main function, the push variables get passed along to other objects to modify their movement rates. Since I decided to break the main function up however, I need to pass those locals into separate functions where they get modified, and then pass the updated values back to the main function. Dunno how clear that is, I'm not good at explaining this kinda stuff

  • I've tried a few ideas. One is to use a couple globals. For example i've used some thing like this to add three vectors together. (1,0), (2,3) and (5,5)

    global number rx=0
    global number ry=0
    on function "add"
    --- set rx to function.param(0)+function.param(2)
    --- set ry to function.param(1)+function.param(3)
    
    start of layout
    --- function: call "add" (1,0, 2,3)
    --- function: call "add" (rx, ry, 5,5)[/code:1bgzt7vv]
    
    I've also put things in a group and using static variables in cases where only functions in that group needs access. Here's a contrived example:
    [code:1bgzt7vv]group: "number print"
    		static number ret0=0
    		static number ret1=0
    		static number ret2=0
    
    		on function "print2"
    		--- function: call "3rand" ()
    		--- browser: console log : ret0&"."&ret1&ret2
    		--- function: call "3digits" ()
    		--- browser: console log : ret0&"."&ret1&ret2
    
    		on function "3rand"
    		--- set ret0 to int(random(10))
    		--- set ret1 to int(random(10))
    		--- set ret2 to int(random(10))
    
    		on function "3digits"
    		--- set ret0 to 3
    		--- set ret1 to 1
    		--- set ret2 to 4
    
    start of layout
    --- function: call "print2" ()[/code:1bgzt7vv]
    
    Another idea would be to implement multiple returns via an array used as a stack.  It would mirror how you'd do it in assembly language.  On possible example. The cleanup could prove tedious, but it probably could be abstracted away with more functions.
    
    [code:1bgzt7vv]on function "get 10 numbers" 
    repeat 10 times
    --- array: push random(1) to front
    --- function: set return to 10
    
    start of layout
    --- function: call "get 10 numbers"
    --- console log: "number of returned numbers: "&function.returnvalue
    --- console log: "first number: "&array.at(array.width-9)
    --- console log: "second number: "&array.at(array.width-8)
    --- console log: "10th number: "&array.at(array.width)
    --- console log: "cleanup"
    --- array: set size to (array.width-10, 1, 1)[/code:1bgzt7vv]
  • If the multiple return values are just used by an object, it might be cleaner to pass the object to the function(s) instead.

    If you really need multiple values, what R0J0hound said. You could also return multiple values inside a string "value 1, value 2, value, 3..." and parse it on return.

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