[SOLVED] Need help with enemy health

I

Incorga Amelius

Guest
Hello, I have just begun using GameMaker a few weeks ago. I just registered on this forum a few minutes ago too, because I have a problem. Right now I am using the health for objects. So far, i made an event (with drag and drop) that
if an enemy tank collides with a bullet
it will lose 3 hp
When hp is less than 1 destroy the enemy tank
destroy the bullet that collides with the tank
When the enemy is created
set the hp to 9
When I run the game, this action stays true with the first enemy tank, but after the first enemy tank, the bullet seems to insta-kill the rest of the tanks once they hit them. What am I doing wrong?
 
F

FadenK

Guest
It seems that when you have the health decreasing, it decreases for EVERY tank instance ( not just the instance colliding with the bullet ).

It's hard to accurately diagnose the issue without seeing the logic you have. Maybe post some screenshots of your logic.

[Edit] Did you do something like click on "Object: obj_tank" in the "Applies To..." section on anything?
 
I

Incorga Amelius

Guest
It seems that when you have the health decreasing, it decreases for EVERY tank instance ( not just the instance colliding with the bullet ).

It's hard to accurately diagnose the issue without seeing the logic you have. Maybe post some screenshots of your logic.

[Edit] Did you do something like click on "Object: obj_tank" in the "Applies To..." section on anything?
The create event for enemy tank
upload_2016-9-29_21-35-46.png
The collision event for enemytank when it hits bullet
http://imgur.com/a/VQ6Eb
The collision for when the bullet hits the enemy tank
upload_2016-9-29_21-38-10.png

And for the enemytank, when it gets hit by the bullet, it destroys self, not object: applies to enemytank
 

Attachments

F

FadenK

Guest
You are using the built-in "lives" variable. This is NOT health. I can explain the issue, and how to fix this. You should learn more about variables. Understanding variables and the different types is imperative. I'll explain the best I can about all variables in GML.

First off, a variable is allocated memory for a value that can change. This is opposed to a constant, which is allocated memory which cannot change.

A variable can originate one of two ways. It can be built-in, or it can be manually created.

There are 3 types of variables, listed as follows:
instance variables: these variables are unique for an instance and can be accessed anywhere in that instance. As in any event or function (this is the most common type)
local variables: these are unique to a script or event. Declared with "var." and sometimes referred to "temporary variables"
global variables: global variables are not unique at all and can be accessed from any instance at anytime. A change to a global variable from one instance will change the value of that variable for all instances trying to access it.

you can read more in the link below:
http://docs.yoyogames.com/source/da...01_gml language overview/variables/index.html

From this, if you haven't guessed already, "lives" is a built-in global variable. So when a tank subtracts 3 from "lives," all other tanks access the new "lives" value. They don't all instantly destroy because you are only checking for lives less than one WHEN a bullet hits.

The best was to handle built-in variables is trying not use them whenever possible.

To resolve your issue:
Replace all commands using "lives" with the equivalent command for variables under the "control" tab.
The variable should apply to Self
name it: my_hp
set my_hp to 9 on create
set my_hp to -3 relative on collision with bullet
and test if my_hp is "less than or equal to 0" in collision event
if so, destroy instance
 
I

Incorga Amelius

Guest
You are using the built-in "lives" variable. This is NOT health. I can explain the issue, and how to fix this. You should learn more about variables. Understanding variables and the different types is imperative. I'll explain the best I can about all variables in GML.

First off, a variable is allocated memory for a value that can change. This is opposed to a constant, which is allocated memory which cannot change.

A variable can originate one of two ways. It can be built-in, or it can be manually created.

There are 3 types of variables, listed as follows:
instance variables: these variables are unique for an instance and can be accessed anywhere in that instance. As in any event or function (this is the most common type)
local variables: these are unique to a script or event. Declared with "var." and sometimes referred to "temporary variables"
global variables: global variables are not unique at all and can be accessed from any instance at anytime. A change to a global variable from one instance will change the value of that variable for all instances trying to access it.

you can read more in the link below:
http://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/variables/index.html

From this, if you haven't guessed already, "lives" is a built-in global variable. So when a tank subtracts 3 from "lives," all other tanks access the new "lives" value. They don't all instantly destroy because you are only checking for lives less than one WHEN a bullet hits.

The best was to handle built-in variables is trying not use them whenever possible.

To resolve your issue:
Replace all commands using "lives" with the equivalent command for variables under the "control" tab.
The variable should apply to Self
name it: my_hp
set my_hp to 9 on create
set my_hp to -3 relative on collision with bullet
and test if my_hp is "less than or equal to 0" in collision event
if so, destroy instance
It works! Thanks so much man. So the only reason you would want to use built-in variables is if you are using 1 enemy for the variable? Other than that, then create your own variables?
 
I

Incorga Amelius

Guest
The only reason to use a built in stat is for the player in a single player game.
Alrighty. Thanks. I'll get on to my game again. If I have anymore problems I'll ask the forum for help again
 
Top