Function syntax: Calling a Return Value

Get help using Construct 2

Post » Wed Jul 08, 2015 12:40 am

Image
I think I've got a good chunk of Functions figured out, but I'm having trouble working out how to pull the values from the parameters and plug them back into the instance variables they initially reference.

I have four different variables which are all checked in the same way to make sure they stay within bounds.
When a power-up is collected, the EXP variable is increased. When it reaches 100+, the LVL variable goes up by 1 and the EXP variable is reset to the [amount]-100. And, the LVL can't go higher than 4. (I forgot to add a check when the EXP is greater than 100 at LVL 4...I'll fix that later.)

As I understand it so far, the Function action "Set Return Value" does just that: sets the Return Value for that parameter. But I see that there is also a Return expression, and I believe that is how I would pull up the value that was set by "Set Return Value". However, I am unsure where to plug it in at - in the Function instructions or after the Function call?

This way, whether it's the speed, the fire rate, etc, that gets put into each of the two parameters upon the call to the Function, each instance variable is updated as if I created four sets of identical events for each variable (which...is rather what Functions help with, so...duh lol).

Thanks for the help!
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 791
Reputation: 15,148

Post » Wed Jul 08, 2015 12:46 am

There is only a single return value. It has nothing to do with any parameters. You either call it inline with Function.Call() or use Function.ReturnValue immediately after the Function action.
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Wed Jul 08, 2015 1:51 am

@blackhornet - I think I understand.
So I would use Function.ReturnValue or Function.Call immediately AFTER the "Set Return Value..." to ensure that the instance variable associated with that parameter in that action line gets properly set?
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 791
Reputation: 15,148

Post » Wed Jul 08, 2015 2:56 am

Neither. When you call a function only numbers or text is passed. The function has no knowledge of what variables were used.

Consider your function call:
Call "weaponleveling" (ship.bulletexpspeed, ship.bulletlvlspeed)

If those two variables were 100 and 15 respectfully then this call would be identical:
Call "weaponleveling" (100,15)

If you want to modify a variable of a certain instance with a function then you need to pass that instance's uid as one of the parameters and then pick by uid in the function to modify the variables.

Also as blackhornet stated a function has only one return value. if you set the return variable multiple times then only the last value will be used.

The main purpose of a return value is when you call a function from an expression. Like this:

On function "add"
--- set return value to function.param(0)+function.param(1)

Global number sum=0

Start of layout
--- set sum to function.call(100,15)
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Wed Jul 08, 2015 2:40 pm

Thank you, @R0J0hound. I'll see if that clicked in my head as well as I believe it did.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 791
Reputation: 15,148

Post » Thu Jul 09, 2015 12:55 am

Okay...I'm NOT understanding...
Even if I pick the UID within the Function, I'm still left with how to set the value of the parameter back to variable originally sourced as the parameter.

Here's where I'm getting lost on how to set this up...
Object Ship has 8 instance variables - an EXP and a LVL each for Speed, Rate, Calibre, and Spread.

Whenever a power-up is collected, I want to run the "WeaponLeveling" Function which will check for the value of the EXP and LVL variables.
So I'll have something like this...

On Collision with PowerupSpeed... Call Function "WeaponLeveling" (SpeedEXP, SpeedLVL)

On Collision with PowerupRate... Call Function "WeaponLeveling" (RateEXP, RateLVL)

On Collision with PowerupCalibre... Call Function "WeaponLeveling" (CalibreEXP, CalibreLVL)

On Collision with PowerupSpread... Call Function "WeaponLeveling" (SpreadEXP, SpreadLVL)

My problem here is how to reference the respective original variables (as defined by the instance variables plugged into each Function call). Even if I use Ship.UID with the Function, I'm left confused on how to single out and plug in the right values back to the right variables without creating more sub-events/actions within the Function which is the entire point of creating the Function in the first place (to be able to reuse the same events with a single call and just swapping out variable references).

If I can only use the one action - ReturnValue - then I'm left with what seems no recourse but to make specific references to each of the 8 variables in question...which, again, is what the parameters are for, so I thought. I'm ultimately lost on the syntax...where do I PLACE this information? In the Function event, itself? In the Function call event line?

I really just need someone to spell it out for me because I'm not seeing it...
Thanks again for the help.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 791
Reputation: 15,148

Post » Thu Jul 09, 2015 1:14 am

Typically if you need to change an object, you pass in the UID, do a Pick by UID to get the object back, and then change your instance variables right there. If you need to support different objects, put them in a Family with the instance variables you need, and do it that way.
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Thu Jul 09, 2015 3:18 am

What you're trying to do is what is called "pass by reference" in other programming languages, which just passes the variable to modify to the function. C2 functions can't do that, it only does "pass by value", so the function has no idea where the values came from.

So like you said you can do a function for each or re-work your function.

On function "weapon leveling"
Pick ship by uid function.param(0)
--- function param 1= "rate"
------ events to level up rate
--- function param 1="speed"
------ events to level up speed
....

Then call the function with
Call function "weapon leveling" (ship.uid, "speed")

You'll still be duplicating events but they'll all be in that function.
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Thu Jul 09, 2015 11:53 am

Aaaah. I sorta understand. A bit more than before. Basically, using a Function here isn't going to help me as I thought since I still need a way to pass specific information BACK to the variables being set to the parameters..."pass by reference", as you said. (Or do I have that backwards? ...wouldn't surprise me.)

At any rate, @Ashley, would it be possible to add this feature to future C2 Functions or perhaps to C3 as you progress on that? It really would be awesome to be able to cut down on events and just specify WHICH detail to affect when each detail is addressed in the same way and the only conditions are which detail gets used at any given time.

Anyway, thank you both! At least now I know my hangup is much less on my understanding of Functions and more on the fact that Functions can't do (in C2) what I was trying to attempt.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 791
Reputation: 15,148

Post » Thu Jul 09, 2015 12:02 pm

I'm not sure exactly what you need, or why there needs to be a new feature to do it. I don't see why you can't just have an event like:

Ship: set instance variable "experience" to Function.call("calculateExperience", Ship.experience)

So you retrieve the experience, call a function, return a value and assign the returned value back to the instance variable all in one action.
Scirra Founder
B
400
S
236
G
89
Posts: 24,549
Reputation: 195,475

Next

Return to How do I....?

Who is online

Users browsing this forum: agg1401, ahmetkahveci and 24 guests