Jump to content


Photo

Health Bar can't subtract or add via variable


  • Please log in to reply
7 replies to this topic

#1 goldsonic46

goldsonic46

    GMC Member

  • GMC Member
  • 581 posts

Posted 04 July 2011 - 12:23 AM

In order to regulate the amount of health added or subtracted every second, I'm using a change variable that records and subtracts two more variable. If the health is between 0 and a max(but not at the max or zero) and I'm changing the amount via a variable, it works. I can also add or subtract by an immediate value at any range including zero and a max value. However, my issue is that I can't subtract via variable when the health value is maxed, or add when it's at zero.

I'm using the health bars based off of Fede-lasse's Fighting style health bars. here

I'm handling the action through a step of the Controller object. In addition, I'm creating objects on the fly to control them through the Controller object. My partial code from the step event:

   //Sums of resources
   energy_change=energy_get+energy_lose;
   metal_change=metal_get+metal_lose;
   energy_time+=1
   metal_time+=1
   //Regulating resources
   if (energy_time >= 30)
   {
      if (inst.hp < inst.maximum) && (inst.hp >= 0)
      {
      inst.hp +=energy_change;
      }
      energy_time=0;
      if(inst.hp == 0)
      {
      inst.hp += energy_get;
      }
   }
   if (metal_time >= 30)
   {
      if (inst.hp < inst.maximum) && (inst.hp >= 0)
      {
      inst1.hp +=metal_change;
      }
      if(inst1.hp == 0)
      {
      inst1.hp += energy_get;
      }
      metal_time=0;
   }
What's wrong with my code(that is, if it's my code)?

Edited by goldsonic46, 04 July 2011 - 12:24 AM.

  • 0

#2 eyeCube

eyeCube

    GMC Member

  • GMC Member
  • 206 posts

Posted 04 July 2011 - 01:07 AM

It is my observation that this code is incredibly over-complicated. I can hardly make sense of it without studying it. I would start from scratch if I were you, but only if you know enough about GML, I suppose.

Edit: maybe change inst.hp < inst.maximum

to inst.hp <= inst.maximum

Edited by eyeCube, 04 July 2011 - 01:08 AM.

  • 0

#3 Titanium

Titanium

    the perfectionist

  • GMC Member
  • 1105 posts

Posted 04 July 2011 - 01:28 AM

Alright, it's really really inefficient, to do it that way. Why don't you just keep a variable that stores how much health you have, and subtract to that variable every time you need the health to go down.

Subtracting 0 from your health is just ridiculously inefficient and too complicated.
  • 0

#4 goldsonic46

goldsonic46

    GMC Member

  • GMC Member
  • 581 posts

Posted 04 July 2011 - 01:51 AM

It is my observation that this code is incredibly over-complicated. I can hardly make sense of it without studying it. I would start from scratch if I were you, but only if you know enough about GML, I suppose.

Edit: maybe change inst.hp < inst.maximum

to inst.hp <= inst.maximum


Over-complicated? Did I not provide enough information? It's my usual M.O. However, the only thing I merged was the example that I mentioned. Unless I misunderstood you, why would I redo everything if it's only subtracting/adding from/to the health bar that's my issue?

Subtracting 0 from your health is just ridiculously inefficient and too complicated.


Subtracting 0 from my health? Can you explain? I have no clue what that means.

Thanks
  • 0

#5 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 04 July 2011 - 02:10 AM

However, my issue is that I can't subtract via variable when the health value is maxed,

That is because you subtract it only when health is smaller (but not equal to) the maximum.

if (inst.hp < inst.maximum) && (inst.hp >= 0)
{
inst.hp +=energy_change;
}

If you intended to ensure that the health doesn't go above maximum or below 0, then the above code doesn't handle it anyway. You'd better clip the value after addition and subtraction rather than checking it before modification.
inst.hp += energy_change;
if (inst.hp < 0) inst.hp = 0;
if (inst.hp > inst.maximum) inst.hp = inst.maximum;
or in one-liner:
inst.hp = max(0, min(inst.maximum, inst.hp + energy_change));
// median(0, inst.hp + energy_change, inst.maximum) will also work if you have GM7 or later

or add when it's at zero.

I'm not entirely sure what causes it, but it may have something to do with the fact that you add energy_get back when inst.hp is equal to 0. (Or hp was in fact a negative value when you thought it was 0.)
  • 1

#6 goldsonic46

goldsonic46

    GMC Member

  • GMC Member
  • 581 posts

Posted 04 July 2011 - 02:45 AM

Thanks, now it works.

Can you also help me on another problem right here?



  • 0

#7 aaabbbccc

aaabbbccc

    GMC Member

  • GMC Member
  • 521 posts

Posted 05 July 2011 - 05:29 AM

Usually when you use > or < and =, it causes an error. at least too me, try removing the >= and make them just >
  • 0

#8 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 05 July 2011 - 05:48 AM

Usually when you use > or < and =, it causes an error. at least too me, try removing the >= and make them just >

That's not true.

-MoK
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users