Solved Carry over player HP value to next stage

Question that is answered or resolved.

Aerisetta

Active member
Hihi, I would like to be able to carry over player's current HP to the next stage.

Actually to me, it's the same stage, it's just the next map, but i'm using next stage to do this. Would be good to know if I can switch maps without changing stages too.

However, most importantly, I would like to make sure the HP value is carried over during the map switch.
 
Solution
Well, there's no line in the script which acquires player 1 last's health.

In my metroidvanias, I always have this line in endlevel.c to do that.
C:
void main()
{
  void P1 = getplayerproperty(0, "entity"); //Get player 1
  int HP1;

  if(P1){
    HP1 = getentityproperty(P1,"health");
    setglobalvar("HPs", HP1);
  }
}

The stored last health is latter reapplied in player's SPAWN animation like this:
C:
anim    spawn
@script
    if(frame==1){
      void self = getlocalvar("self"); //Get calling entity
      int HP = getglobalvar("HP");
      int HPs = getglobalvar("HPs");

      if(HPs==NULL()){
        HPs = getentityproperty(self,"health") + HP;
      }

      changeentityproperty(self,"maxhealth", 30 + HP)...
  1. Record HP into a globalvar as the previous stage ends for each player.
  2. When a stage starts, get the globalvar values and set player's HP.
  3. Delete the globals.

It's really that simple, but there's at least half a dozen ways to implement the code. In most cases the simplest way is to use a levelscript to apply the values and endlevelscript to save them. Both of these go into the level text.

Code:
levelscript data/..../yourscriptfile.c
endlevelscript data/..../yourscriptfile.c
 
  1. Record HP into a globalvar as the previous stage ends for each player.
  2. When a stage starts, get the globalvar values and set player's HP.
  3. Delete the globals.

It's really that simple, but there's at least half a dozen ways to implement the code. In most cases the simplest way is to use a levelscript to apply the values and endlevelscript to save them. Both of these go into the level text.

Code:
levelscript data/..../yourscriptfile.c
endlevelscript data/..../yourscriptfile.c
Thanks a lot!

To my great surprise, I made 2 scripts that actually worked very quickly :P sharing here if anyone else needs and hope more experienced members will review if it is good (my game is only single player)

level end save hp script
Code:
void main()
{
    getglobalvar("HP"); // player's HP
}

level start retrieve hp script
Code:
void main()
{
    setglobalvar("HP", 1); // player's HP
    clearglobalvar();
}
 
Well, there's no line in the script which acquires player 1 last's health.

In my metroidvanias, I always have this line in endlevel.c to do that.
C:
void main()
{
  void P1 = getplayerproperty(0, "entity"); //Get player 1
  int HP1;

  if(P1){
    HP1 = getentityproperty(P1,"health");
    setglobalvar("HPs", HP1);
  }
}

The stored last health is latter reapplied in player's SPAWN animation like this:
C:
anim    spawn
@script
    if(frame==1){
      void self = getlocalvar("self"); //Get calling entity
      int HP = getglobalvar("HP");
      int HPs = getglobalvar("HPs");

      if(HPs==NULL()){
        HPs = getentityproperty(self,"health") + HP;
      }

      changeentityproperty(self,"maxhealth", 30 + HP);
      changeentityproperty(self,"health", HPs); // reapply last health from previous level
      setglobalvar("HPs", NULL());
    }
@end_script
...
 
Solution
Back
Top Bottom