Collision Event, Health bar, Automated Enemy Chasing

I'm trying to have my health bar deplete after characters collide. I have standard health bar code but it won't mix well with a Collisions of instances. How do I ?
 

Bryan112

Member
What do you mean it doesn't mix well? Is depth the problem? be a it more specific and show us a some code.
 
I was meaning the code, so any way here it is my collision/healthbar code:;


// State Variables
var current_hp= max_hp -damage
var damage= 1
var max_hp=10

if !place_meeting (x, y, Guy)
then
draw_healthbar(x + 0, y + 20, x + 250, y + 30, current_hp, $FF00FF00 & $FFFFFF, $FF0000FF & $FFFFFF, $FF00FF00 & $FFFFFF, 0, (($FF00FF00>>24) != 0), (($FF000000>>24) != 0));
// Declare Temp
var temp = 10;
var temp = current_hp
 
In what event are you putting this code? If it is the collision event like you said in the post title, then you should not need the place_meeting function. I would imagine draw_healthbar needs to go in a draw event too. Also, consider putting your color codes into variables/constants for better legibility.
 
I assumed the code needs to recognize a collision as basis for loss of max_hp thusly giving me current_hp. I'm sure this is done with a variable that recognizes a collision as damage. But how is it done?
 
You need { } brackets even if you use the (ugly and useless) then keyword.
You also have code that makes no sense at the end of it. You're missing semicolumns.
The logic of your code itself is to NOT draw the healthbar when you're colliding with GUY. You coded it yourself explicitly. This is why it doesn't show up.
Who knows what you were going for, but at any rate, a clean version of that code would be:
GML:
if !place_meeting (x, y, Guy) {
    var damage= 1;
    var current_hp= max_hp -damage;
    var max_hp=10;
    draw_healthbar(x + 0, y + 20, x + 250, y + 30, current_hp, $FF00FF00 & $FFFFFF, $FF0000FF & $FFFFFF, $FF00FF00 & $FFFFFF, 0, (($FF00FF00>>24) != 0), (($FF000000>>24) != 0));

   // Declare Temp
   //var temp = 10;                          //Totally useless, makes 0,0% sense whatsoever
   //var temp = current_hp             //Useless, just continue to use current_hp  in whatever code follows IN THIS BLOCK. If no code follows IN THIS BLOCK, delete the line
}
 
Step: Event

if alarm[0]=-1
{
if position_meeting(x,y,EnemyPirate)
{
alarm[0]=3;
}
}
_________________________________________________________________________________________________________
DRAW: Event
var current_hp=10
var damage= 1;

var max_hp=10;
var current_hp= max_hp -damage;



draw_healthbar(x + 0, y + 20, x + 250, y + 30, current_hp , red, blue, red, lime, 0,...etc
____________________________________________________________________________________________________
healthbar Object Event Alarm(0) --- no code written

Yes
This coding setup is getting my HealthBar Ok, however it's not reacting with a collision to affect current hp.

Any Ideas on how to have collision event affect my hp by applying damage.
 
Have you read your own code?
You SET your hp in the draw event as a hardcoded value (10, as a local variable, on top of that!). You then apply 1 damage. Leaving you with 9 hp. Every step.
You have to use your actual health values. And even if you used it, where do you calculate the (hp_now/max_hp) ratio?
There's so many things going wrong in that code, better go back to the manual for some basics.
I have no idea of what your step event is supposed to do. You know there's a collision event, right?
 

Nidoking

Member
var current_hp=10
var damage= 1;

var max_hp=10;
var current_hp= max_hp -damage;
It should be printing 9 HP every time, because that's the value you give it. They're only technically variables if they always have the same value. We call those "constants". If you want the variables to change, you have to put different values into them at least once. Ever. Perhaps when you detect a collision and then set an alarm that explicitly does nothing.
 
Correct Syntax for a HealthBar and nothing is happening with my collisions=? I need to corelate variable( take_damage ) to If collisions occur and only then. My guess is a step event with the event collision i have placed on my object. I have not solved where to place that in an event and have the correct coding at the same time. Health_BarSyntax_with_Variables.png
 
I can solve the semantics of healthbar's initial value after I solve this. The problem they see and the problem i'm having are related. So when collisions affect my healthbar the other will be automatically adjusted to accuracy.
 

Nidoking

Member
Nothing affects your healthbar. Nothing. You set it to 9 HP and 9 HP is where you set it. If you want something different, you'll have to set it to something that isn't exactly always 9 HP.
 
Anyone want to describe the steps involved within Drag and Drop for having one objects( direction and movements and speed )set to chase my object. A little amount of coding included may not be too bad, specially if that helps
 

chamaeleon

Member
In the step event set direction of your chasing instance to point_direction(x, y, obj_player.x, obj_player.y) (if you only have one instance of obj_player to chase, others you need to figure out which instance to chase before you can pick its x and y coordinates).
 
You probably also want to have some condition to put a stop (and a start, for that matter) to that chase. Usually, a distance or a line of sight is it (sometimes sound as well, like Metal Gear).
I don't know in DnD, but it's pretty simple to access another object's variables with either the dot notation other_instance.variable or the with() {...} block in GML
 
Last edited:
Supposing this is to do with automated chasing of objects... read more

My camera controller object with no sprite. That’s what is following my object in the game, player 1. My controller object is then followed by the camera in the room. I ask now how can I have the camera controller object follow one instance of the object(player1. And after it’s destroyed switch to following its secondary instances object in the room . with out problems. I use Drag and Drop but coding is alright in GML and sometimes is necessary.
 
Hey, I’m only trying this software for fun. So I am as far here as now trying to save x,y positions when leaving a room from where I entered the prior room. Much like the modern games have. I can give you an example enter a door to a house in a game, when you exit that house your location is next to it. Cant’t do this yet. Can anyone help me out. The current code is set up as a room_goto event started by a colliison with an object. GML is a likely solution to the problem yet would rather use Drag and Drop if you can.
 

chamaeleon

Member
Hey, I’m only trying this software for fun. So I am as far here as now trying to save x,y positions when leaving a room from where I entered the prior room. Much like the modern games have. I can give you an example enter a door to a house in a game, when you exit that house your location is next to it. Cant’t do this yet. Can anyone help me out. The current code is set up as a room_goto event started by a colliison with an object. GML is a likely solution to the problem yet would rather use Drag and Drop if you can.
Setup / Room editing
Code:
In room editor store a numerical id for each door in the room and a numerical id for the door it represents in the other room (so bidirectional identification) using variable definitions
Just before moving a new room
Code:
Store the numerical id of the door you want to emerge at somewhere (global variable or a player instance variable) using the destination id for the door you are entering through
Room start event for the player
Code:
Apply To block for door object
    If Block comparing door id with desired door id
            Set Instance Variable X coordinate for player object to x
            Set Instance Variable Y coordinate for player object to y
    Break block
 

FrostyCat

Redemption Seeker
Why are you declaring your health as temporary variables in the Draw event? They will not last, and they will revert every frame. Lasting individualized properties should be declared as Instance variables in the Create event or the Object Variables interface, then used in later events.

I strongly suggest that you drop whatever you are doing, and learn what the basic language elements do and how they come together. What you are doing is not "trying this software for fun", you are just typing randomly ordered junk into the IDE and expecting that to give a meaningful result. This kind of "infinite monkey programming" is not an effective way to work with, learn, or try new software.
 
Anyone think of how to both add or subtract from this health bar providing different collision, So i want to add health if i have one collision and subtract health if a different collision occurs. using GML
 

Attachments

TailBit

Member
When you use object.x and there is more then one object, then it will get the x from the first instance created..

You could use
Code:
if(place_meeting(x,y,object)) health-=1;
To check if this instance collide with a object at its own position

You should not use place_meeting in a "with", because it only returns true or false, which gamemaker reads as 1 and 0, which it will think is the object index of the 2 first created objects

Instead you can use instance_place as it returns the id or noone, "with" will ignore noone:
Code:
with(instance_place(x,y,enemy)) health-=1;
Or you can use a variable:
Code:
var ins = instance_place(x,y,object);

if(ins != noone) health-=1;
But .. you could just use the collission events?

Collission with object
health-=1;
 
Top