00:00
00:00
Newgrounds Background Image Theme

TwistSSD 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 Refreshing Variables Question

485 Views | 4 Replies
New Topic Respond to this Topic

AS2 Refreshing Variables Question 2016-01-25 17:56:13


Hi, I'm working on a game which involves a "budget" which has a bunch of variables on it which you can increase or decrease, the problem I noticed is that when I raise or lower one of the variables, it doesn't change the overall balance. It still displays the original budget balance, as opposed to the new one when I've modified sections of the budget. I created a function which re-declares the budget balance variable whenever you modify a section of the budget, and now it updates normally. While this is fine and dandy, there are a bunch of variables in my game which are just a combination of other variables added up and such, so the function gets bigger and bigger as I make more progress on the game. I assume there must be a less cpu intensive way (Not that it lags, but its always good to be efficient with your code) than recalculating a bunch of variables anytime you change one value.

My question is: Does anyone know of a better way to refresh variables like I've done with this function? To clarify, the script works and everything, I just want to know if there is a more efficient way of doing what I want. The function I use to refresh the variables gets bigger whenever I add a new variable combination (a variable which is just a bunch of other variables added up). I was thinking of using an onClipEvent(EnterFrame) handler when declaring them so that the variable is just constantly declaring itself, but that might be even more cpu intensive than just redeclaring it anytime you modify the budget (which is really the only time it needs to be declared/refreshed).

Example of what I mean:
(In one as file)

var budg_agriculture:Number = -450;
var budg_manufacturing:Number = -250;
var budg_balance:Number = _root.budg_agriculture + _root.budg_manufacturing;

If someone changes the budg_agriculture value, the budg_balance doesnt update, so i've created:
(In another file)

function VarRefresh(){
_root.budg_balance = _root.budg_agriculture + _root.budg_manufacturing;
}

And I just call VarRefresh(); everytime a variable is modified. It works, but again, it seems inefficient and I need to add new variables to the "VarRefresh" function anytime I add one, to make sure it updates properly.


AS: Main || AS3: Main || <-- Useful links.

Response to AS2 Refreshing Variables Question 2016-01-25 18:13:07


You have to assign a new value to your budg_balance if you want it to change; there is simply no other possible way of achieving that effect. When you're assigning it _root.budg_agriculture + _root.budg_manufacturing, you're not telling the compiler or run-time environment to perform that calculation each time the budg_balance is read, you're simply telling them to perform that calculation once and store the result in budg_balance.

As for how you're doing it: I don't see any problems with having a function that will update the values of all the variables that are dependent on the values of other variables.

At 1/25/16 05:56 PM, TehBoss wrote: (Not that it lags, but its always good to be efficient with your code)

It is good to be efficient, but you're heading into premature optimisation territory. Don't worry about optimising until you have a demonstrable reason to.

Response to AS2 Refreshing Variables Question 2016-01-31 00:30:51


If you want to make it more efficient, put the function on a separate movie clip, and call it from other places by referencing that MC. At least then you'll run it on a separate AS thread and won't slow down the main code.

Best you can do given that its AS2.

Response to AS2 Refreshing Variables Question 2016-01-31 02:33:44


At 1/31/16 12:30 AM, C0DEM0NKEY wrote: If you want to make it more efficient, put the function on a separate movie clip, and call it from other places by referencing that MC. At least then you'll run it on a separate AS thread and won't slow down the main code.

Best you can do given that its AS2.

Actionscript does not have separate threads; all AS code is run in a single thread, save for Workers (AS3) and possibly asynchronous events. I don't think it'd make a big difference where the code is called; it'd take roughly the same time anyways.


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

"Sit look rub panda" - Alan Davies

BBS Signature

Response to AS2 Refreshing Variables Question 2016-01-31 15:29:34


At 1/31/16 12:30 AM, C0DEM0NKEY wrote: If you want to make it more efficient, put the function on a separate movie clip, and call it from other places by referencing that MC. At least then you'll run it on a separate AS thread and won't slow down the main code.

Best you can do given that its AS2.

Even if threads could be used, which they can't, as Gimmick pointed out, they wouldn't be any more efficient. The variable in question that needs to be updated is depended upon by the code that would be in the other thread, so any changes made to said variable would need to be used with a mutex, which would cause the main thread to sleep at any point when the variable is being read and written to at the same time—the result would be it running with the same level as performance, or quite possibly even worse performance.