Someone gifted MetalSlayer69 supporter status!
We need you on the team, too.
Support Newgrounds and get tons of perks for just $2.99!
Create a Free Account and then..
Become a Supporter!Your intuition is correct! However, you need to modify the function just a little bit, to return the new calculated value:
var veggieCommence:Boolean = false; var veggieProg:Number = 0; var veggieTick:Number = 1; var meatsCommence:Boolean = false; var meatsProg:Number = 0; var meatsTick:Number = 1; function commenceProgress(booby:Boolean, proggy:Number, ticky:Number):Void { if(booby) { proggy += ticky; } return proggy; } onEnterFrame = function() { veggieProg = commenceProgress(veggieCommence, veggieProg, veggieTick); meatsProg = commenceProgress(meatsCommence, meatsProg, meatsTick); }
This is because, like in several high-level languages, there are two different ways of passing variables - by reference and by value. Numbers (int, uint, Number in as3), strings, booleans are primitive types whereas generic objects, class instances, and most everything else is not a primitive. Primitive types are passed by value, whereas non-primitives are passed by reference.
(If you already know how pass-by-value and pass-by-reference works, feel free to skip this section)
In short, when you pass primitives, their value is copied and the copy is modified within the function. It doesn't modify the original variable that you passed into it. If you need to change the original variable, you'll have to do that manually through an assignment.
When you pass non-primitives, their value is passed directly -- that is, the function parameter holds the same variable and this allows you to change the properties of that variable and have it be reflected in the original calling function / everywhere else that variable is used. For example:
// declare an object, which is a non-primitive type in as2 var obj = { a: 1, b: 2 }; function modifyProps( someObj ) { someObj.a = 34; } trace(obj.a) // 1 modifyProps(obj) trace(obj.a) // 34
However, note that reassigning the variable in the function doesn't change the value of the calling variable outside it. That is, you cannot do the following and expect it to work:
var obj = { a: 1, b: 2 }; function modifyObj ( someObj ) { someObj = { y: 5, z: 10 }; // a and be should be undefined, if this works } trace(obj.a) // 1 modifyObj(obj) trace(obj.a) // 1 -- still exists! trace(obj.y) // undefined -- does not exist!
That's because reassigning the variable someObj within the function modifyObj only causes you to modify whatever you now have assigned to that variable. The original variable still remains intact, because it is just a reference. I'm struggling to think of analogies at the moment, but there should be plenty if you search elsewhere for how pass-by-reference works.
Your intuition is correct! However, you need to modify the function just a little bit, to return the new calculated value:
var veggieCommence:Boolean = false; var veggieProg:Number = 0; var veggieTick:Number = 1; var meatsCommence:Boolean = false; var meatsProg:Number = 0; var meatsTick:Number = 1; function commenceProgress(booby:Boolean, proggy:Number, ticky:Number):Void { if(booby) { proggy += ticky; } return proggy; // returns new value if `booby == true`, else returns old value directly } onEnterFrame = function() { veggieProg = commenceProgress(veggieCommence, veggieProg, veggieTick); meatsProg = commenceProgress(meatsCommence, meatsProg, meatsTick); }
This is because, like in several high-level languages, there are two different ways of passing variables - by reference and by value. Numbers (int, uint, Number in as3), strings, booleans are primitive types whereas generic objects, class instances, and most everything else is not a primitive. Primitive types are passed by value, whereas non-primitives are passed by reference.
(If you already know how pass-by-value and pass-by-reference works, feel free to skip this section)
In short, when you pass primitives, their value is copied and the copy is modified within the function. It doesn't modify the original variable that you passed into it. If you need to change the original variable, you'll have to do that manually through an assignment.
When you pass non-primitives, their value is passed directly -- that is, the function parameter holds the same variable and this allows you to change the properties of that variable and have it be reflected in the original calling function / everywhere else that variable is used. For example:
// declare an object, which is a non-primitive type in as2 var obj = { a: 1, b: 2 }; function modifyProps( someObj ) { someObj.a = 34; } trace(obj.a) // 1 modifyProps(obj) trace(obj.a) // 34
However, note that reassigning the variable in the function doesn't change the value of the calling variable outside it. That is, you cannot do the following and expect it to work:
var obj = { a: 1, b: 2 }; function modifyObj ( someObj ) { someObj = { y: 5, z: 10 }; // a and be should be undefined, if this works } trace(obj.a) // 1 modifyObj(obj) trace(obj.a) // 1 -- still exists! trace(obj.y) // undefined -- does not exist!
That's because reassigning the variable someObj within the function modifyObj only causes you to modify whatever you now have assigned to that variable. The original variable still remains intact, because it is just a reference. I'm struggling to think of analogies at the moment, but there should be plenty if you search elsewhere for how pass-by-reference works.