Good health meter Tutorial

Learn how to make certain types of games and use gameEditor.

Good health meter Tutorial

Postby bat78 » Wed Apr 01, 2015 5:40 pm

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 595 times

In the demo you can also decrease HP with down/up arrow key.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Good health meter Tutorial

Postby koala » Wed Apr 01, 2015 9:51 pm

Neat. :)

Game Editor is very powerful tool when you know how to use C in it.
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Good health meter Tutorial

Postby bat78 » Wed Apr 01, 2015 11:01 pm

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.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Good health meter Tutorial

Postby Turon » Fri Jun 12, 2015 7:56 pm

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: ...
Turon
 
Posts: 862
Joined: Sun Jan 24, 2010 5:23 pm
Score: 32 Give a positive score

Re: Good health meter Tutorial

Postby bat78 » Fri Jun 12, 2015 8:26 pm

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);
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Good health meter Tutorial

Postby Turon » Sun Jun 14, 2015 1:35 pm

I've never worked with local variables before, how are they different to normal user variables?
Turon
 
Posts: 862
Joined: Sun Jan 24, 2010 5:23 pm
Score: 32 Give a positive score

Re: Good health meter Tutorial

Postby bat78 » Sun Jun 14, 2015 2:17 pm

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"
Last edited by bat78 on Sun Jun 14, 2015 4:09 pm, edited 1 time in total.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Good health meter Tutorial

Postby Turon » Sun Jun 14, 2015 2:51 pm

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?
Turon
 
Posts: 862
Joined: Sun Jan 24, 2010 5:23 pm
Score: 32 Give a positive score

Re: Good health meter Tutorial

Postby bat78 » Sun Jun 14, 2015 4:07 pm

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....
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Good health meter Tutorial

Postby Turon » Sun Jun 14, 2015 5:10 pm

Yes, GE's variable declaration pane accessable from the script editor. Thanks, I now have a better understanding.
Turon
 
Posts: 862
Joined: Sun Jan 24, 2010 5:23 pm
Score: 32 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron