Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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!

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;
}

  • 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.

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;
    }
}

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.