Is it possible to return multiple values from a function?

Get help using Construct 2

Post » Sun Jan 22, 2017 8:30 pm

I want to call a function from within another function and have it return multiple values back to it. Is that possible?
B
39
S
16
G
6
Posts: 542
Reputation: 7,617

Post » Sun Jan 22, 2017 9:32 pm

No. You have to store them in some way. You'd have to provide more details for some suggestions.
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

Post » Sun Jan 22, 2017 9:51 pm

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 :P
B
39
S
16
G
6
Posts: 542
Reputation: 7,617

Post » Mon Jan 23, 2017 9:07 pm

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)
Code: Select all
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)


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: Select all
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" ()


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: Select all
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)
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Mon Jan 23, 2017 10:31 pm

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.
Image
B
30
S
9
G
3
Posts: 171
Reputation: 3,329


Return to How do I....?

Who is online

Users browsing this forum: Elliott, kellehk, MadSpy, oosyrag, totoe, Yahoo [Bot] and 41 guests