functions that return more than one value

Get help using Construct 2

Post » Tue Dec 10, 2013 4:37 am

hi...how do you return several values with one function?

thanks!
Dan
B
8
S
3
G
1
Posts: 87
Reputation: 1,493

Post » Tue Dec 10, 2013 4:42 am


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....
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Tue Dec 10, 2013 4:53 am

Put return values into a dictionary or an array.
B
108
S
26
G
268
Posts: 4,456
Reputation: 150,247

Post » Tue Dec 10, 2013 3:57 pm

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. cacotigon2013-12-10 16:01:59
B
31
S
7
G
8
Posts: 232
Reputation: 6,254


Return to How do I....?

Who is online

Users browsing this forum: Google [Bot] and 20 guests