0
\$\begingroup\$

I have five objects: obj_Boss, obj_bossHealth, obj_GameOver, obj_BtnOK, and obj_rec2.

This is what I want. When obj_Boss has no more health, obj_GameOver and obj_BtnOK will appear.

This is my first method:

In obj_bossHealth, I have these events:

Create Event: set var global.bossHealth to 100

Draw Event: draw_healthbar

In obj_Boss, I have these events:

Collision with obj_rec2: set var global.bossHealth relative to -1.5; destroy the instance (other)

No More Health: create instance of obj_GameOver; create instance of obj_BtnOK

This is my second method:

In obj_bossHealth, I have these events:

Create Event: set var global.bossHealth to 100

Draw Event: draw_healthbar

Step Event: if global.bossHealth is equal to 0 then create instance of obj_GameOver and create instance of obj_BtnOK

In obj_Boss, I have these events:

Collision with obj_rec2: set var global.bossHealth relative to -1.5; destroy the instance (other)

The result of both methods:

obj_GameOver and obj_BtnOK won't appear. How can I fix this? Please explain your answer. Thanks.

I add some images: method1 method2

\$\endgroup\$
1
  • \$\begingroup\$ I heavily encourage the act of integrating all these things into one boss object. The health of the boss can just be stored as one variable and you can draw the health bar with the boss object. \$\endgroup\$
    – user64742
    Jun 3, 2017 at 21:46

2 Answers 2

1
\$\begingroup\$

The reason your obj_GameOver and obj_BtnOK won't appear is because you are checking the wrong variable. The No More Health Event is triggered by checking for the value of variable health. From the YoYo Documentation:

No More Health

GameMaker: Studio has a built-in health system, which, like Lives above, is basically a global scope variable that is created for you at the start of a game. There are actions to set and change the health variable as well as to draw a health-bar, with code available to change them too should you prefer to use GML. This event is triggered whenever the health becomes less than or equal to 0. It is typically used to reduce the number of lives or to restart the game or room.

Since in the first method you are working with the variable global.bossHealth, which was defined by you, instead of health, a built-in variable modeled around the GM:S framework functionalities, there's absolutely no chance your No More Health Events will be triggered. If you set global.bossHealth to 0 or less, health will still be 100 or somewhat else.

Being the health variable in unique wherever you read/write it from, that's why I encourage your second method.

It's just a math matter

The reason your buttons won't appear is just a mathematical issue: if your bossHealth starts from 100 and you subtract 1.5 per hit, we'll get to the point where the obj_Boss has been hit 66 times. Then, the health amount the Boss lost is:

66 [hits] * 1.5 [damage per hit] = 99 [damage]

At this point, its health will be:

100 [health] - 99 [damage] = 1 [health]

And here comes the trouble: if the Boss is hit again, his health will be:

1 - 1.5 = -0.5

But since you are checking for global.bosHealth to be equal to 0, this condition will never evaluate to true.

The solution

This is VERY simple: just edit your condition for global.bossHealth value: don't just check if it is equal to zero, but less than or equal to.

In the Step Event of object obj_bossHealth, your code would look as follows:

// Step event - Check health
if (global.bossHealth <= 0)
{
    instance_create(x1,y1,obj_GameOver);
    instance_create(x2,y2,obj_BtnOK);
}

Here, x1, y1, x2, and y2 are meant to be set to the right values for your objects.

\$\endgroup\$
2
  • \$\begingroup\$ It's working. Yes, it's very simple. But, I didn't realize it before. Thanks for your help. \$\endgroup\$ Jun 14, 2016 at 11:35
  • \$\begingroup\$ I suggest you to read the documentation (press F1 when GM:S is open) if something doesn't look clear to you, I still find it very helpful. Keep up the good work! \$\endgroup\$
    – liggiorgio
    Jun 14, 2016 at 12:17
1
\$\begingroup\$

I haven't worked in GameMaker Studio before but just from looking at your screenshot it appears that when obj_rec2 collides with obj_Boss it is literally setting bossHealth to -1.5, so that after the collision it bossHealth = -1.5. Therefore bossHealth is never reaching 0 to create instances of obj_BtnOK and obj_GameOver

\$\endgroup\$
1
  • \$\begingroup\$ No, I set it to relative. Which means bossHealth -= 1.5. When obj_Boss collides, bossHealth is reduced by 1.5 \$\endgroup\$ Jun 14, 2016 at 6:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .