Function syntax: Calling a Return Value

Get help using Construct 2

Post » Thu Jul 09, 2015 2:48 pm

@Ashley - Thank you for responding. :)
Well, see, this is where I am having so much trouble with Functions - on one hand I get them in concept and on the other I'm baffled about the application and such... So if I sound confusing, it's because I'm genuinely confused and trying to work this out, myself, and figure out the limitations and abilities of Functions within C2.

So...based on what you showed there in your example, let me see if I can spell this out to confirm if I've got the deal straight in my head...

EVENT CONDITION X IS TRUE:
--> Set Ship.Experience to Function.Call("CalculateExperience", Ship.Experience)

The Function is called and processed (as I have displayed in my screenshot above). In the actual Function instructions, it's told to process info for parameter 0, which, in this example, is Ship.Experience. The data is processed, and somewhere in my actual Function instructions I tell it push the parameter 0 value back to the Ship.Experience parameter.

Correct?

My question is then what happens when the parameter 0 is a different variable? Can I tell the Function to push the value back to variable declared in the parameter 0 at the time of the Function call during the specific runtime of the Function without having to specific each instance variable of the Ship within the Function instructions?

If that still doesn't make sense, I'll try to draw up a visual diagram of my thoughts because I tend to be visual on these things anyway.
Thanks!
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
141
S
23
G
8
Posts: 790
Reputation: 15,017

Post » Thu Jul 09, 2015 3:31 pm

ImageImageImage
B
71
S
22
G
242
Posts: 3,734
Reputation: 134,994

Post » Thu Jul 09, 2015 3:39 pm

Maybe you could use a Dictionary along with the ship in a container (or if the ship is a unique instance, just a plain Dictionary).

On collision with PowUpSpeed
--- Call "WeaponLeveling" ("Speed")

On function "WeaponLeveling"
\_Dictionary Key Function.Param(0) & "XP" >= 100
---Set Dictionary Key Function.Param(0) & "XP" to Dictionary.Get(Function.Param(0) & "XP") - 100
---Set Dictionary Key Function.Param(0) & "Level" to Dictionary.Get(Function.Param(0) & "Level") + 1
...
etc.

That would simulate reference passing, and most likely work as you intended.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Thu Jul 09, 2015 3:45 pm

Or as mentioned, you pass in the UID, so you can do a group of events:
http://www.blackhornettechnologies.com/ ... e_UID.capx
ImageImageImage
B
71
S
22
G
242
Posts: 3,734
Reputation: 134,994

Post » Thu Jul 09, 2015 3:54 pm

A more general way to look at it would be:

Set X to Function.Call("func", Y)

This would be run in the following order:

1. Call "func" with parameter 0 being Y
2. Run events for "func", which set a return value
3. Set X to the return value

The function has no idea what happens to its return value. It's used by whatever called the function.

So in my original example, both X and Y are the "experience" variable. If you wanted the return value to go somewhere else, you'd use a different action and cut and paste the Function.Call(...) expression in to one of its parameters.
Scirra Founder
B
398
S
236
G
88
Posts: 24,441
Reputation: 194,681

Post » Thu Jul 09, 2015 3:56 pm

@blackhornet However, he'll need a group of events per weapon type, and that's what he wanted to avoid with his function.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Thu Jul 09, 2015 10:38 pm

First, thank you, EVERYONE.

And @Magistross has it right.
What you all have shared makes enough sense so far in as much as a single variable is concerned. But I'm dealing with four distinct groups of two variables that come into play each time my Ship collides with the related power-up object. Depending on which power-up is hit, that will determine which pair of variables are called and put into the parameters section. So the trick/problem I face is how to pass the ReturnValue BACK to the appropriate variables AS defined in the parameters during the Function call.

I will check out Hornet's examples and see if that doesn't answer my questions further.
Thanks again!
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
141
S
23
G
8
Posts: 790
Reputation: 15,017

Post » Thu Jul 09, 2015 10:45 pm

As Magistross points out, you still need to identify which group. You could pass in another parameter specifying which group to set. It's up to you to decide which mechanism makes the most sense, to you.
ImageImageImage
B
71
S
22
G
242
Posts: 3,734
Reputation: 134,994

Post » Thu Jul 09, 2015 10:54 pm

@blackhornet - Excellent examples. THANK YOU! That made things so much clearer.
I don't think the UID example will work for my purposes but the Function.Call() example I think I can use.

@Ashley, @Magistross, @blackhornet - You three have been very help. Thank you for your patience with me. :)
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
141
S
23
G
8
Posts: 790
Reputation: 15,017

Previous

Return to How do I....?

Who is online

Users browsing this forum: 25games, dand, Google [Bot], oosyrag and 32 guests