Question by
Hathakas · Oct 07, 2019 at 10:18 PM ·
scripting problemscripting beginneradding
Is it possible to "Subscribe" and unsubscribe to a SUM calculation based on bool?
Hey guys.
I'm working on a game that involves using energy/power, some items drain power at different rates.
I'm trying to find the best way to sum up the total power consumption from many gameobjects.
Is there an easy way to code something along the lines of:
If this gameobjects is currently consuming energy, subscribe to "Total energy consumption"?
This is what I have so far, which works but I feel like there would be a better way. Thanks a bunch.
if (!shieldsCharged) // if shields are not at 100% charge
{
if (!shieldsPooled) // if shields energy consumption has not been pooled
{
minOperatingEnergy += shieldsEnergyConsumptionPerTick; //minOperatingEnergy is the total energy consumption rate
shieldsPooled = true; //Shield consumption has been pooled
}
if (energyShutDown) //ignore
{
}
else if (!energyShutDown)//ignore
{
if (currentShields < maxShields)//if shield charge is less than max shield charge
{
shieldsConsumingEnergy = true;
currentShields += shieldsEnergyConsumptionPerTick * Time.deltaTime;//charge shields
currentEnergy -= shieldsEnergyConsumptionPerTick * Time.deltaTime; //consume energy
}
}
if (currentShields >= maxShields) // if shield reached max shield charge
{
shieldsCharged = true;
shieldsConsumingEnergy = false; //no longer needs to consume energy as it's charged
minOperatingEnergy -= shieldsEnergyConsumptionPerTick; //un-subscribe from pool/sum
shieldsPooled = false;
}
}
Comment