Setting max/min Int amount and increasing amount

Started by jamesreg, Wed 30/01/2019 16:11:51

Previous topic - Next topic

jamesreg

Ok,

I know this is simple but I am missing a few things I need to do with this.
I have a ship with phasers which the power level can be charged/drained etc.

I have a label on a button that tells the power level at the time.

Global variables I have set are:
Phaser1Charged (int set to 100 at start)

Labels are:
Lphaser1health

I have this code in my global execute always script

Code: ags
Lphaser1health.Text=String.Format("%d",Phaser1Charged); // to update label
 
 
  if(IsTimerExpired(1)){
  Phaser1Charged += 5; 
  SetTimer(1, 600);
  
 }
}


My label text does indeed say 100 at start I tested it by creating a by creating a button that drops Phaser1Charged to 0 when pushed and this works my label text then says zero. But after sitting around awhile the numbers never go back up or show a change in the label.
What am I missing or still need to do?  Also how do i take an Int  and define that the min amount I want it to ever be is 0 and the max amount to never go past 100

Khris

IsTimerExpired() code needs to go inside repeatedly_execute (the if block you have needs to be called again and again for it to work, it doesn't extend into the future).

As for min and max values, you need to do the check right after changing the value.

Code: ags
// inside repeatedly_execute 
  if (IsTimerExpired(1)) {
    Phaser1Charged += 5;
    if (Phaser1Charged > 100) Phaser1Charged = 100; 
    SetTimer(1, 600);
  }

jamesreg

#2
Thank you very much. I see what I did now. That worked how would I go about getting it to only start recharging when I hit 0.
Right now say I take 50 damage it starts recharging but I only want it to recharge if it is all the way to 0


Matti

So what you want is to have the phaser only recharge when it drops to zero, and then continue until it's at 100, right?

Then you could use a bool for that:
Code: ags

bool charging = false;

  if (Phaser1Charged < 1 && !charging) {
    SetTimer(1, 600);
    charging = true;
  }

  if (IsTimerExpired(1)) {
    Phaser1Charged += 5;
    if (Phaser1Charged > 100) {
      Phaser1Charged = 100; 
      charging = false;
    }
    else SetTimer(1, 600);
  }

Khris

That shouldn't be necessary; all you need is

Code: ags
  // damage
  Phaser1Charged -= 50;
  if (Phaser1Charged <= 0) {
    Phaser1Charged = 0;
    SetTimer(1, 600);
  }

jamesreg

Quote from: Khris on Wed 30/01/2019 20:12:38
That shouldn't be necessary; all you need is

Code: ags
  // damage
  Phaser1Charged -= 50;
  if (Phaser1Charged <= 0) {
    Phaser1Charged = 0;
    SetTimer(1, 600);
  }


So I would keep the other code and put this in when i script commands for damage to occur?

Khris

Yes, SetTimer(1, 600); starts the recharging.
Using a bool to store game state is a tried and true technique, but not necessary here as far as I can see.

Matti

Correct me if I'm wrong, but wouldn't the phaser recharge endlessly after the timer is set once?

Snarky

What should happen if you try to fire while it is charging?

Khris

I originally had code that would only call SetTimer again if the value was below 100 but decided against it because it doesn't really matter.
I figure the phaser is supposed to automatically go up to 100 whenever it is less than 100, and there's no harm in having a timer fire every 15 seconds that does nothing.

jamesreg

To
Quote from: Snarky on Thu 31/01/2019 15:05:25
What should happen if you try to fire while it is charging?

Yeah I got it working now.

To answer your question is the way I have it set up is you have two phaser banks.
Your go to a arming screen and arm them. Once armed they will charge up once
charged you can fire them at enemies and such and each fire will drain the power a little.
Once fully drained they will lose charge and disarm and you must wait for them to recharge
in order to use them again. If you attempt to fire while charging a message will be given
saying phasers offline. I also have a damage system here which can knock them offline
as well. Differences with the damage is the charging and rearming will not be automatic.
You actually have to have the engineer repair them which will take time or instantly repair
them by diverting power from other resources at a cost of sacrificing those other resources
such as engine speed or shields etc.

SMF spam blocked by CleanTalk