Posted June 5, 20205 yr Hello all! So i have a bit of a odd question. I have a Power generator that works all fine, but the power being all added in at the end of the burn time is not exactly what I want to do. Let's say i want to add... 2500 power units over 1600 ticks. How would i go about doing that when the power system only accepts Integers to add? I have tried int power = 2500 / 1600; addPower(power); but obviously this won't work because it rounds the result to a Integer, only generating 1600 and not 2500 power units. Any help on this would be great, thank you!
June 5, 20205 yr if addPower() accepts floats, just use a float I don't know how that power system works but int power = 2500/1600; float powerLeft = 2500; for(int i=1600; i>0; i--){ //make this run once every tick instead of this for loop, but remember to make i work the same power = powerLeft/i; addPower(power); powerLeft -= power; }
June 5, 20205 yr Author I'm using the built in ForgeEnergy system and the issue is that it only allows power as a int. So i'm trying to make it so power is added smoothly over time rather then all at once.
June 5, 20205 yr Disclaimer: I haven't used any energy system in my mods yet. private int ratePerTick = 20; // adjust if needed private int powerRemaining = 2500; // will be changed by other code public void tick() { if (powerRemaining > ratePerTick) { addPower(ratePerTick); powerRemaining -= ratePerTick; } else { if (powerRemaining > 0) { addPower(powerRemaining); } powerRemaining = 0; } }
June 5, 20205 yr Howdy The basic algorithm for doing things like this is to add a different amount each tick. For example, if you wanted to add 30 power units over 20 ticks, you would add 2 units on the first tick 1 unit on the second tick 2 units on the third tick 1 unit on the fourth tick (etc) for 20 ticks in total. You need to track the fractional part yourself eg from your example INITIALISING: float ratePerTick = 2500.0/1600.0; float remainder = 0.0; EVERY TICK: remainder += ratePerTick; int powerToAddThisTick = (int)remainder; remainder -= powerToAddThisTick; addPower(powerToAddThisTick) -TGG
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.