Solved Drawsprite Issue with Reduced Lifebar

Question that is answered or resolved.

maxman

Well-known member
I have a lifebar script with flowing reduction set for players in ondrawscript. But my problem is that when I use entityvar, along with drawsprite, that is in conjunction with globalvar, I cannot run the game because I use drawsprite. What I want is to draw the sprite to the screen where the player's icon is. The reduced lifebar set in entityvars only works without using drawsprite in globalvars.

C:
void main(){
    playerHealth();
}

void playerHealth(){
    void self = getlocalvar("self");

    // PLAYER SIDE
    int P1 = getplayerproperty(0, "entity");
    int P2 = getplayerproperty(1, "entity");
    void P1Name = getentityproperty(P1, "defaultname");
    void P2Name = getentityproperty(P2, "defaultname");
    int P1HP = getentityproperty(P1, "health");
    int P2HP = getentityproperty(P2, "health");
    int P1MaxHP = getentityproperty(P1, "maxhealth");
    int P2MaxHP = getentityproperty(P2, "maxhealth");

    int P1Health = 93*P1HP/P1MaxHP;
    int P2Health = 93*P2HP/P2MaxHP;

    if(getentityvar(self, "playerOne") == NULL()){setentityvar(self, "playerOne", P1Health);} // SET FOR PLAYER'S HEALTH BAR SMOOTH REDUCTION INDEPENDENTLY
    if(getentityvar(self, "playerTwo") == NULL()){setentityvar(self, "playerTwo", P2Health);}

    void Bar = getglobalvar("Bar");

    if(P1 != NULL()){
        drawbox(26,9,getentityvar(self, "playerOne"),10,999,rgbcolor(56,160,248),0);
        drawsprite(Bar, 1, 8, 2500);
    }

    if(P2 != NULL()){
        drawbox(156,9,getentityvar(self, "playerTwo"),10,999,rgbcolor(56,160,248),0);
        drawsprite(Bar, 131, 8, 2500);
    }



    if(P1Health < getentityvar(self, "playerOne")){
        if(getentityvar(self,1)==NULL()){
            setentityvar(self,1,0);
        }
        setentityvar(self,1,getentityvar(self,1)+1);
        if(getentityvar(self,1) > 7){
            setentityvar(self, "playerOne",getentityvar(self, "playerOne")-1);
            setentityvar(self,1,0);
        }
    }

    if(P2Health < getentityvar(self, "playerTwo")){
        if(getentityvar(self,1)==NULL()){
            setentityvar(self,1,0);
        }
        setentityvar(self,1,getentityvar(self,1)+1);
        if(getentityvar(self,1) > 7){
            setentityvar(self, "playerTwo",getentityvar(self, "playerTwo")-1);
            setentityvar(self,1,0);
        }
    }
    
}

void oncreate(){
    void Bar;

    if(!Bar){
        Bar = loadsprite("data/sprites/bar.gif");
        setglobalvar("Bar", Bar);
    }
}

void ondestroy(){
    void Bar = getglobalvar("Bar");

    if(Bar){
        free(Bar);
        setglobalvar("Bar", NULL());
    }
}

I'm using @msmalik681's HP Bar script as a reference for reducing HP bar while allowing the red bar to decrease flowingly. I'm trying to make the HP bar decrease flowingly like the red bar. It's like Street Fighter 2, Super Double Dragon, and some others that have those kind in the way the life bars are reduced.

Reference:
C:
void main()
{
if(getplayerproperty(0, "entity") == NULL() && getplayerproperty(1, "entity") == NULL()){
}else{
void self = getlocalvar("self");        //get calling entity
float x = getentityproperty(self, "x") - openborvariant("xpos") - 25;    //get caller x value minus 25
float z = getentityproperty(self, "z");        //get caller z value
float a = getentityproperty(self, "a");        //get caller a value
float y = z - a - openborvariant("ypos") - getentityproperty(self, "maxmp");    // get caller y position and move it up by entity max mp value
int maxhp = getentityproperty(self,"maxhealth");    // get caller max hp
int hp = getentityproperty(self,"health");        // get caller current hp
int health = hp / 0.01 / maxhp / 2;            // work out current hp percentage and half it
if(getentityvar(self,0)==NULL()){setentityvar(self,0, health);}    //set enity var to 0 if no value (null)


drawbox(x,y,health,5,z+2,rgbcolor(223,214,73),0);        //draw hp box using x and y cords also z value is +2 so its on top of other 2 boxes
drawbox(x,y,getentityvar(self,0),5,z+1,rgbcolor(181,26,0),0);    //second box to show damage with a z+1 value so its under hp bar and entity var 0 controls its size
drawbox(x,y,50,5,z,0,0);                    //black box in background to show how much original life bar was


if(health<getentityvar(self,0))        // if hp box smaller then damage box
   {
if(getentityvar(self,1)==NULL()){setentityvar(self,1,0);}    //set entity var 1 to zero if null this is a timer
setentityvar(self,1,getentityvar(self,1)+1);            //add 1 to timer this will run every game tic        
 if(getentityvar(self,1) > 50)                    //if timer over 50
    {
 setentityvar(self,0,getentityvar(self,0)-1);    //reduce displayed damage by 1
 setentityvar(self,1,0);            //reset timer to zero this will cause a loop untill damage is the same as hp
  }
 }
}
}

I get this crash for running drawsprite and entityvar in an update script (ondrawscript/update.c/updatescript). Maybe it's got to do with using drawsprite.

Code:
Function requires a valid sprite handle 3 integer values, 5th integer value is optional: drawsprite(sprite, int x, int y, int z, int sortid)
Script function 'drawsprite' returned an exception, check the manual for details.
 parameters: <VT_EMPTY>   Unitialized, 30, 218, 2500,

I see how the lifebar increases and decreases flowingly without the cut in @Kratus's Streets of Rage X game. I edited @msmalik681's HP bar for the actual health bar and the result turns out very nice. But upon adding (load/draw)sprite into the code, I have problem.

How can I have a sprite for a specific player drawn on screen along with entityvars set without a problem?

My last question is this: Can't entityvars be used in update.c?
 
Solution
I finally got this solved after I looked at my old project. I decided to do the latter of separating Bar sprites as @DCurrent suggested in one of his posts which I didn't look through. Thanks to him, I got what I wanted.

C:
void main(){
  playerHP();
}

void playerHP(){ // MAIN: START
  if(openborvariant("in_level") == 1){ // IN_LEVEL: START
    int P1 = getplayerproperty(0, "entity");
    int P2 = getplayerproperty(1, "entity");

    void P1Name;
    void P2Name;
    int HP1; int HP2;

    void Bar = getglobalvar("Bar");
    void Bar2 = getglobalvar("Bar2");

    if(!Bar){
      Bar = loadsprite("data/sprites/bar.gif");
      setglobalvar("Bar", Bar);
    }

    if(!Bar2){
      Bar2 = loadsprite("data/sprites/bar2.gif")...
I finally got this solved after I looked at my old project. I decided to do the latter of separating Bar sprites as @DCurrent suggested in one of his posts which I didn't look through. Thanks to him, I got what I wanted.

C:
void main(){
  playerHP();
}

void playerHP(){ // MAIN: START
  if(openborvariant("in_level") == 1){ // IN_LEVEL: START
    int P1 = getplayerproperty(0, "entity");
    int P2 = getplayerproperty(1, "entity");

    void P1Name;
    void P2Name;
    int HP1; int HP2;

    void Bar = getglobalvar("Bar");
    void Bar2 = getglobalvar("Bar2");

    if(!Bar){
      Bar = loadsprite("data/sprites/bar.gif");
      setglobalvar("Bar", Bar);
    }

    if(!Bar2){
      Bar2 = loadsprite("data/sprites/bar2.gif");
      setglobalvar("Bar2", Bar2);
    }


    if(P1){
      P1Name = getentityproperty(P1, "defaultname");
      HP1 = getentityproperty(P1, "health");
      if(getentityvar(P1, "playerOne") == NULL()){setentityvar(P1, "playerOne", HP1);}

      if(HP1 < getentityvar(P1, "playerOne")){ //If HP box smaller than damage box
          if(getentityvar(P1, 1) == NULL()){
           setentityvar(P1, 1, 0); //Set entity var 1 to zero if null this is a timer
          }
          setentityvar(P1, 1, getentityvar(P1, 1)+1); //Add 1 to timer; this will run every game tick
          if(getentityvar(P1, 1) > 7){ //If timer over 7
           setentityvar(P1, "playerOne", getentityvar(P1, "playerOne")-1); //Reduce displayed damage by 1
           setentityvar(P1, 1, 0); //Reset timer to zero; this will cause a loop until damage is the same as hp
          }
      }

      if(P1Name == "Billy"){
        drawbox(26, 9, getentityvar(P1, "playerOne")-3, 10, 999, rgbcolor(56,160,248), 0);
        drawsprite(Bar, 1, 8, 2500);
      }else if(P1Name == "Jimmy"){
        drawbox(26, 9, getentityvar(P1, "playerOne")-3, 10, 999, rgbcolor(246,130,130), 0);
        drawsprite(Bar2, 1, 8, 2500);
      }

     
    } // End of P1
    if(P2){
      P2Name = getentityproperty(P2, "defaultname");
      HP2 = getentityproperty(P2, "health");
      if(getentityvar(P2, "playerTwo") == NULL()){setentityvar(P2, "playerTwo", HP2);}

      if(HP2 < getentityvar(P2, "playerTwo")){
          if(getentityvar(P2, 1) == NULL()){
           setentityvar(P2, 1, 0);
          }
          setentityvar(P2, 1, getentityvar(P2, 1)+1);
          if(getentityvar(P2, 1) > 7){
           setentityvar(P2, "playerTwo", getentityvar(P2, "playerTwo")-1);
           setentityvar(P2, 1, 0);
          }
      }

      if(P2Name == "Billy"){
        drawbox(156, 9, getentityvar(P2, "playerTwo")-3, 10, 999, rgbcolor(56,160,248), 0);
        drawsprite(Bar, 131, 8, 2500);
      }else if(P2Name == "Jimmy"){
        drawbox(156, 9, getentityvar(P2, "playerTwo")-3, 10, 999, rgbcolor(246,130,130), 0);
        drawsprite(Bar2, 131, 8, 2500);
      }
    } // End of P2
  } //IN_LEVEL: END
} // MAIN: END

void oncreate()
{
    void Bar;
    void Bar2;

    if(!Bar){
      Bar = loadsprite("data/sprites/bar.gif");
      Bar2 = loadsprite("data/sprites/bar2.gif");
      setglobalvar("Bar", Bar);
      setglobalvar("Bar2", Bar2);
    }
}
void ondestroy(){
    void Bar = getglobalvar("Bar");
    void Bar2 = getglobalvar("Bar2");
    if(Bar){
      free(Bar);
      free(Bar2);
      setglobalvar("Bar", NULL());
      setglobalvar("Bar2", NULL());
    }
}

Or... health points in percentage.

C:
void main(){
  playerHP();
}

void playerHP(){ // MAIN: START
  if(openborvariant("in_level") == 1){ // IN_LEVEL: START
    int P1 = getplayerproperty(0, "entity");
    int P2 = getplayerproperty(1, "entity");

    void Bar = getglobalvar("Bar");
    void Bar2 = getglobalvar("Bar2");

    if(!Bar){
      Bar = loadsprite("data/sprites/bar.gif");
      setglobalvar("Bar", Bar);
    }

    if(!Bar2){
      Bar2 = loadsprite("data/sprites/bar2.gif");
      setglobalvar("Bar2", Bar2);
    }


    if(P1){
      void P1Name = getentityproperty(P1, "defaultname"); // Player 1's default name
      int HP1 = getentityproperty(P1, "health"); // Player 1's actual health point
      int HP1Max = getentityproperty(P1, "maxhealth"); // Player 1's maximum health point
      int P1Health = 97*HP1/HP1Max; // For the size of width as player 1's health bar
      if(getentityvar(P1, "playerOne") == NULL()){setentityvar(P1, "playerOne", P1Health);}

      if(P1Health < getentityvar(P1, "playerOne")){ //If HP box smaller than damage box
          if(getentityvar(P1, 1) == NULL()){
           setentityvar(P1, 1, 0); //Set entity var 1 to zero if null this is a timer
          }
          setentityvar(P1, 1, getentityvar(P1, 1)+1); //Add 1 to timer; this will run every game tick
          if(getentityvar(P1, 1) > 7){ //If timer over 7
           setentityvar(P1, "playerOne", getentityvar(P1, "playerOne")-1); //Reduce displayed damage by 1
           setentityvar(P1, 1, 0); //Reset timer to zero; this will cause a loop until damage is the same as hp
          }
      }

      if(P1Name == "Billy"){
        drawbox(26, 9, getentityvar(P1, "playerOne"), 10, 999, rgbcolor(56,160,248), 0);
        drawsprite(Bar, 1, 8, 2500);
      }else if(P1Name == "Jimmy"){
        drawbox(26, 9, getentityvar(P1, "playerOne"), 10, 999, rgbcolor(246,130,130), 0);
        drawsprite(Bar2, 1, 8, 2500);
      }

     
    } // End of P1
    if(P2){
      void P2Name = getentityproperty(P2, "defaultname");
      int HP2 = getentityproperty(P2, "health");
      int HP2Max = getentityproperty(P2, "maxhealth");
      int P2Health = 97*HP2/HP2Max;
      if(getentityvar(P2, "playerTwo") == NULL()){setentityvar(P2, "playerTwo", P2Health);}

      if(P2Health < getentityvar(P2, "playerTwo")){
          if(getentityvar(P2, 1) == NULL()){
           setentityvar(P2, 1, 0);
          }
          setentityvar(P2, 1, getentityvar(P2, 1)+1);
          if(getentityvar(P2, 1) > 7){
           setentityvar(P2, "playerTwo", getentityvar(P2, "playerTwo")-1);
           setentityvar(P2, 1, 0);
          }
      }

      if(P2Name == "Billy"){
        drawbox(156, 9, getentityvar(P2, "playerTwo"), 10, 999, rgbcolor(56,160,248), 0);
        drawsprite(Bar, 131, 8, 2500);
      }else if(P2Name == "Jimmy"){
        drawbox(156, 9, getentityvar(P2, "playerTwo"), 10, 999, rgbcolor(246,130,130), 0);
        drawsprite(Bar2, 131, 8, 2500);
      }
    } // End of P2
  } //IN_LEVEL: END
} // MAIN: END

void oncreate()
{
    void Bar;
    void Bar2;

    if(!Bar){
      Bar = loadsprite("data/sprites/bar.gif");
      Bar2 = loadsprite("data/sprites/bar2.gif");
      setglobalvar("Bar", Bar);
      setglobalvar("Bar2", Bar2);
    }
}
void ondestroy(){
    void Bar = getglobalvar("Bar");
    void Bar2 = getglobalvar("Bar2");
    if(Bar){
      free(Bar);
      free(Bar2);
      setglobalvar("Bar", NULL());
      setglobalvar("Bar2", NULL());
    }
}

Do I need to keep both ondestroy() and oncreate() in the code or do I need to delete them?

Thank you, guys, so much for your help!
 
Last edited:
Solution
Back
Top Bottom