- Home /
Totalling powerup values
I'm counting how many powerups a given player collects in a game via a static variable that increments per powerup.
What would be the best way to take a value from each powerup that was collected and gain a total to then adjust some player based stats? If it helps the object is destroyed once collected.
I have three ideas currently and would imagine the first one to be better?
1) I do something similar to foreach(powerup) {stat+40;}
2) I just multiply the number of powerups by a set ammount and add that value onto the stat affected - possible in update()?
3) Add the stat immediately on interaction of the object itself (at the time of the object being destroyed - in the powerup script itself)
Thank you in anticipation.
I always use the third, that way a single type of power up can have separate values for each instance. if its always a set number you could multiply the total count, that would need to be stored just like the actual value would...so id probly just use the 3rd solution.
The question you need to ask yourself is: "how adaptable do I want this system to be". If your powerups add a static amount each time you can just go with the third option and increase the player stat. if however you'd like a varaible amount to be added by each powerup or add different kinds of powerups you're better of with a more robust and generic system. This also applies if the power-up only last for a short amount of time. You can do this by writing a class for your stats: This class would have a base amount and perhaps a list of increments with a timer attached to each one. You can then just ask this class for the current value of a stat. Your powerups would then add an increment to a certain stat using the interface provided by the class. It's the stats own responsibility to keep track of the TTL (time-to-live) of each powerup it receives. This ensures a high degree of cohesion in your code base.
Hope that makes sense :)
In general, you shouldn't be using static variables for this (or for ANYTHING). Specifically, the player should keep track of the powerups they have collected, and deter$$anonymous$$e their total stats based on the total strength of all the powerups they own. This way, individual powerups can be added and removed easily, without having to remember specifics about each powerup.
Your answer
Follow this Question
Related Questions
Resouce script help 3 Answers
GuiText Score On Mouse Click 1 Answer
FPS + Time.time not accurate on iPhone5 1 Answer
Count Variable Acting Strange 1 Answer
Counting colors and keeping it in memory (javascript) 0 Answers