00:00
00:00
Newgrounds Background Image Theme

Burning78Omen34 just joined the crew!

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!

AS2 Addition Function Thing

72 Views | 3 Replies
New Topic Respond to this Topic

Dumb question time. So say I have two different sets of variables like below. In the onEnterFrame function, the variables that are ticks are added to the variables that are progs if a Boolean is true.


var veggieCommence:Boolean = false;
var veggieProg:Number = 0;
var veggieTick:Number = 1;

var meatsCommence:Boolean = false;
var meatsProg:Number = 0;
var meatsTick:Number = 1;

onEnterFrame = function() {

  if(veggieCommence) {
      veggieProg += veggieTick;
  }

  if(meatsCommence) {
       meatsProg += meatsTick;
  }
       
}

I'm curious if (instead of writing two if statements) could I just put them in one function like this?

function commenceProgress(booby:Boolean, proggy:Number, ticky:Number):Void {

  if(booby) {
    proggy += ticky;
  }
      
}


If not, should I just bite the bullet and write everything in one function? Did I miss something in the language reference? Apparently numbers don't let you do a method similar to points like return prog.add(tick) or something like that.


Also, Happy New Year.


[1] - [2]


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.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to AS2 Addition Function Thing 2024-01-02 23:52:08


At 1/1/24 05:58 AM, Gimmick wrote: Your intuition is correct! However, you need to modify the function just a little bit, to return the new calculated value:
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:
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:
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.


Please rest assured that anything related to programming languages or computers that you explain to me is always going be completely new to me. Thank you again for pointing out stuff I've never thought of before, lol.


[1] - [2]

Response to AS2 Addition Function Thing 2024-01-03 03:06:13


At 1/2/24 11:52 PM, Nabella wrote: Please rest assured that anything related to programming languages or computers that you explain to me is always going be completely new to me. Thank you again for pointing out stuff I've never thought of before, lol.


No worries! I mentioned that bit mainly because it felt like I'd written about pass-by-value and pass-by-reference at some point in the past, but wasn't sure whether it was towards someone else or even on this site lol.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature