Page 1 of 1

Good health meter Tutorial

PostPosted: Wed Apr 01, 2015 5:40 pm
by bat78
Let's make together a hi-tech health meter as for some sort of a space ship game.

  • Create new canvas actor and drag its edges to make it thin and long.
    pic1.png

  • Yet, create another regular actor "gui_meter" and add it some futuristic animation. This will be one of our decorations for the health meter that will be centered.
    I'll be using this.
  • Optionally, create one more decorative GUI actor for the outer design (borders). This is mine.
  • You'd also like to set it as a view's child.
  • Now create a local variable called "percentage" type "Real"
    pic3.png

    So we can have a percentage of health for each actor we create. Isn't that a bit realistic? Theoretically everything has a health xD
  • Now navigate to global code and put this:
    Code: Select all
    void set_health (Actor myactor, float value)
    {
        float p = value;
     
        if(p > 99.0) p = 100.0;
        if(p < 1.0) p = 0.0;
     
        myactor.percentage = p;
    }

    void update_health_meter (Actor myactor)
    {
        int i;
        double fraction = myactor.percentage * width / 100.0;
     
        // Background texture of the healthmeter
        for(i = 0; i < width; i += 2)
        {
            // Initializing our health gradient
            setpen(0, 0, 64, 0, 1);
     
            // Drawing our health gradient
            moveto(i, 0);
            lineto(i, height);
        }
     
        // Drawing health according to the fraction, skipping each 2nd pixel
        for(i = 0; i < fraction; i += 2)
        {
            // Initializing our health gradient
            setpen(255 - i, i * 2, 0, 0, 1);
     
            // Drawing our health gradient
            moveto(i, 0);
            lineto(i, height);
        }
     
        // Drawing our hybrid-alike decoration at the center of the healthmeter (OPTIONAL)
        //draw_from(gui_meter.name, width / 2, (height / 2) + 35, 1);
    }

    Note that it is not intended to be much fps-wise.

Now when something happen for instance the player gets in collision with an explosion, simply do that:
Code: Select all
myplayer.percentage -= 10;

To decrease 10% HP of myplayer's initial health.
and along that, update the health meter with
Code: Select all
update_health_meter(myplayer);


To save unneeded FPS consumption create one global variable "myplayer_suffers"
assign 1 to it when player gets injured and do that in the health meter's draw actor:
Code: Select all
if(myplayer_suffers)
{
    myplayer_suffers = 0;
    update_health_meter(myplayer);
}


health_meter_example.zip
(222.99 KiB) Downloaded 232 times

In the demo you can also decrease HP with down/up arrow key.

Re: Good health meter Tutorial

PostPosted: Wed Apr 01, 2015 9:51 pm
by koala
Neat. :)

Game Editor is very powerful tool when you know how to use C in it.

Re: Good health meter Tutorial

PostPosted: Wed Apr 01, 2015 11:01 pm
by bat78
koala wrote:Neat. :)

Game Editor is very powerful tool when you know how to use C in it.

Yes, add me in skype: psp-gen I would like to know how much C do you know.

Re: Good health meter Tutorial

PostPosted: Fri Jun 12, 2015 7:56 pm
by Turon
very nice little healthbar system :) , say, which part of the code effects the color of the bar? also I haven't gotten the enemy to kill the player yet :oops: ...

Re: Good health meter Tutorial

PostPosted: Fri Jun 12, 2015 8:26 pm
by bat78
Turon wrote:very nice little healthbar system :) , say, which part of the code effects the color of the bar? also I haven't gotten the enemy to kill the player yet :oops: ...


// Initializing our health gradient
setpen(255 - i, i * 2, 0, 0, 1);

Re: Good health meter Tutorial

PostPosted: Sun Jun 14, 2015 1:35 pm
by Turon
I've never worked with local variables before, how are they different to normal user variables?

Re: Good health meter Tutorial

PostPosted: Sun Jun 14, 2015 2:17 pm
by bat78
Turon wrote:I've never worked with local variables before, how are they different to normal user variables?


There is no standardized notion such as "normal user variables".

In Game-Editor there are 3 types of variables in regard to linkage
  • Global Variables
    Visible to every scope and actor. (global scope)
    Runtime extend (permanent lifetime / duration)
    External static linkage
    They are stored in "data" memory segment. Program's runtime memory.
  • Local Variables
    Visible only to their block / scope { scope } (local scope)
    Scope-limited extend (unless declared with the static storage-class modifier keyword)
    Internal lexical/dynamic linkage.
    They are stored in the "stack" / "automatic storage class" memory segment.
  • Actor Variables
    Those declared from the Game-Editor Variable Declaration Pane VDP.
    Visible to every scope and actor. Accessed through Actor.
    Runtime extend
    External static linkage
    I don't know where gE stores them, probably in "heap"

Re: Good health meter Tutorial

PostPosted: Sun Jun 14, 2015 2:51 pm
by Turon
So global variables are different to actor variables in that they are stored data a memory segment as apposed to a "heap". but function exactly the same. So actor variables are declared through the script editor through the actor control?

Re: Good health meter Tutorial

PostPosted: Sun Jun 14, 2015 4:07 pm
by bat78
Turon wrote:So global variables are different to actor variables in that they are stored data a memory segment as apposed to a "heap". but function exactly the same. So actor variables are declared through the script editor through the actor control?


No I specifically wrote Game-Editor's Variable Declaration Pane.
Script Editor -> Variables -> Add..

Once you create a variable there it becomes an Actor variable. Which means a member of an Actor structure. Just like x, y, xvelocity, animpos....

Re: Good health meter Tutorial

PostPosted: Sun Jun 14, 2015 5:10 pm
by Turon
Yes, GE's variable declaration pane accessable from the script editor. Thanks, I now have a better understanding.