Function syntax: Calling a Return Value

0 favourites
  • 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 ( rather what Functions help with, so...duh lol).

    Thanks for the help!

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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?

  • 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,15)

  • Thank you, I'll see if that clicked in my head as well as I believe it did.

  • 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.

  • 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.

  • 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.

  • 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 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"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.

  • 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...


    --> 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.


    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.


  • A CAPX is worth a thousand words:

  • 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



    That would simulate reference passing, and most likely work as you intended.

  • Or as mentioned, you pass in the UID, so you can do a group of events: ... e_UID.capx

  • 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.

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