Coding help on depleting my health bar

DreTazzz

Member
Hi to anyone that can possibly help. Ok, I am trying to create a code where if A or B is in the room with C, D, or E, then the health bar goes down. I have it currently as:

if instance_exists((obj_A || B) && (obj_C || obj_D || obj_E) && (RoomA[4])) {
global.rem_hp -= 30;
}

It doesnt seem to work. So, I have a two to three-part question: is that code correct, if not, what should it be; and, should the code go under CREATE, or STEP? Because I want it to be in effect the entire round. Thanks soooooo much to anyone that can help.
 

TsukaYuriko

☄️
Forum Staff
Moderator
That's not quite how they work. See How NOT to use && and || #2 for reference.

If you need code to be executed repeatedly, you have to put it in a repeating event, such as the Step event. (The Create event happens only once when the instance is created.)
 

Arvel

Member
Hi to anyone that can possibly help. Ok, I am trying to create a code where if A or B is in the room with C, D, or E, then the health bar goes down. I have it currently as:

if instance_exists((obj_A || B) && (obj_C || obj_D || obj_E) && (RoomA[4])) {
global.rem_hp -= 30;
}

It doesnt seem to work. So, I have a two to three-part question: is that code correct, if not, what should it be; and, should the code go under CREATE, or STEP? Because I want it to be in effect the entire round. Thanks soooooo much to anyone that can help.
This is what you want
GML:
var condition_1 = instance_exists(obj_A) || instance_exists(obj_B);
var condition_2 = instance_exists(obj_C) || instance_exists(obj_D) || instance_exists(obj_E)
if (condition_1 && condition_2) {
    ....
}
 

TailBit

Member
Giving some of them the same parent object can make it shorter, as you then only have to check if the parent exists
 

DreTazzz

Member
This is what you want
GML:
var condition_1 = instance_exists(obj_A) || instance_exists(obj_B);
var condition_2 = instance_exists(obj_C) || instance_exists(obj_D) || instance_exists(obj_E)
if (condition_1 && condition_2) {
    ....
}

I just saw this. Thanks so much. I will do this soon...I'm at work. Thanks again very much. 😊

That's not quite how they work. See How NOT to use && and || #2 for reference.

If you need code to be executed repeatedly, you have to put it in a repeating event, such as the Step event. (The Create event happens only once when the instance is created.)

Got it. Ok, thank you for responding. 😁

Hi to anyone that can possibly help. Ok, I am trying to create a code where if A or B is in the room with C, D, or E, then the health bar goes down. I have it currently as:

if instance_exists((obj_A || B) && (obj_C || obj_D || obj_E) && (RoomA[4])) {
global.rem_hp -= 30;
}

It doesnt seem to work. So, I have a two to three-part question: is that code correct, if not, what should it be; and, should the code go under CREATE, or STEP? Because I want it to be in effect the entire round. Thanks soooooo much to anyone that can help.
I just saw this. Thanks so much. I will do this soon...I'm at work. Thanks again very much. 😊
I just saw this. Thanks so much. I will do this soon...I'm at work. Thanks again very much. 😊
Hi there, Arvel. Sadly, it didnt work for me. Ok, so I entered the code exactly as you wrote - I just added the global hp I have. Now question, maybe I could have put the code in the wrong place. I added it in a STEP event, which is within the obj_Healthbar that holds the value of the global hp. Is that spot ok? Also, I had mentioned originally that I want the code to work in a certain room, RoomA[4]. Do I need to add the room as a variable too, or put the code you gave me in the room or something? Thanks so much for helping me.

var condition_1 = instance_exists(obj_A) || instance_exists(obj_B);
var condition_2 = instance_exists(obj_C) || instance_exists(obj_D) || instance_exists(obj_E)
if (condition_1 && condition_2) {
global.rem_hp -= 30;
}

Giving some of them the same parent object can make it shorter, as you then only have to check if the parent exists
Hi Tailbit. I am aware of parents, but I havent used any ever, so that might open up another canof worms for me, lol. Thanks all the same for repsonding though....I really appreciate that. :)
 

Yal

🐧 *penguin noises*
GMC Elder
. Do I need to add the room as a variable too, or put the code you gave me in the room or something?
Yes.

Which one to do depends on what you want the effect to do more exactly.
  • Want health to go down over time? Add a third condition to check for the room (you can && it together like the other two) and put the code in the Step event of your player object, or a controller object.
  • You could also put the code as-is now in a special "damage control" object and place that in only the rooms where you're supposed to lose health, so you don't need your main control objects to run lots of code that only applies for certain rooms. Most people likes to segment their logic like this to avoid bloat.
  • If you only want the effect to trigger once, you could also use the "damage control object" method and have it run in Room Start or Create event instead of Step.
  • If you only want the effect to trigger once and you only have one room where it can happen, you could also add the code to the Room Creation Code (so you don't need to make a new object for the effect)
 

DreTazzz

Member
Thanks everyone soooooo much for your help. It still isnt working. Maybe I need to set the task I have more clearly. I have a room, where currently none of the objects are in there, randomly (obj_A || obj_B) will appear (which is fine if by themsleves)...but, if (obj_C || obj_D || obj_E) appear while A or B are in that room, then I want the HP to go down -30Hp. Hope that helps. Cause I cant get this to work still. I do want this code to happen repeatedly, since I have a random create object of C, D, and E like every 5 seconds.
This is what I have in a STEP event for a controller in the room:

var condition_1 = instance_exists(obj_A) || instance_exists(obj_B);
var condition_2 = instance_exists(obj_C) || instance_exists(obj_D) || instance_exists(obj_E);

if (condition_1 && condition_2) {
if ((condition_2 <= room_width) && (condition_2 <= room_height)) {
global.rem_hp -= 30;
}}


Hmmm, I am not sure if this is ok to do, or not. I have the above code in a script, and then I put that script in the STEP event area I mentioned above.
 

DreTazzz

Member
Define "isn't working". What happens, what is supposed to happen?
Object A, B, C, D, and E, enter the room just fine; however, the Hp gauge isnt going down. What should happen is the Hp gauge should go down -30hp, if obj C,D, or E is in the room, at the same time as obj A or B. (C, D, and E are the enemies).
 

chamaeleon

Member
Object A, B, C, D, and E, enter the room just fine; however, the Hp gauge isnt going down. What should happen is the Hp gauge should go down -30hp, if obj C,D, or E is in the room, at the same time as obj A or B. (C, D, and E are the enemies).
Why are you comparing boolean values (condition_1 and condition_2) to room width and height? In any case, this should be trivially debugged using a couple of show_debug_message() calls with interesting values (the results of all instance_exists(), the resulting condition_1 and condition_2 values, etc.), in conjunction with show_debug_message() indicating which code path is/is not taken. Sufficient use of show_debug_message() will very clearly tell you why the subtraction does not happen.
 

TsukaYuriko

☄️
Forum Staff
Moderator
So... let's break this down.

You're assigning boolean values to condition_1 and condition_2.

You then check if both are true, as in, an instance of one of A or B as well as an instance of one of C, D or E must exist.

If that is the case, you then check whether 1 (condition_2, which, if this code runs, is true, also known as 1) is less than or equal to room_width as well as room_height.

Chances are you don't want to require an instance of both groups to exist, and you certainly aren't comparing positions of any if these instances by comparing a boolean - you'd have to specifically get their IDs and compare those.
 

DreTazzz

Member
Why are you comparing boolean values (condition_1 and condition_2) to room width and height? In any case, this should be trivially debugged using a couple of show_debug_message() calls with interesting values (the results of all instance_exists(), the resulting condition_1 and condition_2 values, etc.), in conjunction with show_debug_message() indicating which code path is/is not taken. Sufficient use of show_debug_message() will very clearly tell you why the subtraction does not happen.

Ok, thank you. I will look into that. As far as boolean and room width...I think i collaged two suggestions into one from people - outside this chat I created.
 

DreTazzz

Member
So... let's break this down.

You're assigning boolean values to condition_1 and condition_2.

You then check if both are true, as in, an instance of one of A or B as well as an instance of one of C, D or E must exist.

If that is the case, you then check whether 1 (condition_2, which, if this code runs, is true, also known as 1) is less than or equal to room_width as well as room_height.

Chances are you don't want to require an instance of both groups to exist, and you certainly aren't comparing positions of any if these instances by comparing a boolean - you'd have to specifically get their IDs and compare those.

Thanks for info. I only added room height since I was suggested that in another forum last year. Do I even need that room stuff? If I can only have the hp go down if A or B is in the room with C, D, or E, then I would be happy. I dont want extra codes, IF I dont need them. Please advise. :)
 

chamaeleon

Member
Ok, thank you. I will look into that. As far as boolean and room width...I think i collaged two suggestions into one from people - outside this chat I created.
If you only need existence at all in the room, just drop room size comparison completely. If you need to use position information, you'll need to compare individual instance's x and y to perhaps some other instance's x and y, or the room boundary. It all depends on what your objective is.

Regardless, if you do not see an expected result in your game, definitely learn to use the debug tools at your disposal. show_debug_message() is convenient because you get a trace of the execution you can read over and over, and observe changes in this output compared to a previous set of messages as you make corresponding changes to the code. Break points and the debugger are very useful too (put a breakpoint at an if statement, look at local or instance variables in the various debug windows, click the step button and see if the debugger steps into the block or skips over it (either into an else block if it exists, or to the code following the if block), and figure out if your mental image of what should happen match what did happen when you clicked step in the debugger.
 

DreTazzz

Member
If you only need existence at all in the room, just drop room size comparison completely. If you need to use position information, you'll need to compare individual instance's x and y to perhaps some other instance's x and y, or the room boundary. It all depends on what your objective is.

Regardless, if you do not see an expected result in your game, definitely learn to use the debug tools at your disposal. show_debug_message() is convenient because you get a trace of the execution you can read over and over, and observe changes in this output compared to a previous set of messages as you make corresponding changes to the code. Break points and the debugger are very useful too (put a breakpoint at an if statement, look at local or instance variables in the various debug windows, click the step button and see if the debugger steps into the block or skips over it (either into an else block if it exists, or to the code following the if block), and figure out if your mental image of what should happen match what did happen when you clicked step in the debugger.

Thanks for the awesome, detailed info. I'ma start using debugging to help me. As for this project of mine, I just wanted the HP to deplete if they were in the room. They don't need to collide, and they don't shoot any projectiles....just C, D, and Es existence within the boundaries of the room, at same time as A or B is my goal. 😊
 

DreTazzz

Member
If you only need existence at all in the room, just drop room size comparison completely. If you need to use position information, you'll need to compare individual instance's x and y to perhaps some other instance's x and y, or the room boundary. It all depends on what your objective is.

Regardless, if you do not see an expected result in your game, definitely learn to use the debug tools at your disposal. show_debug_message() is convenient because you get a trace of the execution you can read over and over, and observe changes in this output compared to a previous set of messages as you make corresponding changes to the code. Break points and the debugger are very useful too (put a breakpoint at an if statement, look at local or instance variables in the various debug windows, click the step button and see if the debugger steps into the block or skips over it (either into an else block if it exists, or to the code following the if block), and figure out if your mental image of what should happen match what did happen when you clicked step in the debugger.

Hi there. Well, I am happy to say that the code help finally worked. I am super happy about that! However, the healthbar goes down fast to zero, once C, D, or E enters the room. Is there a way that I can only have the healthbar go down -30 once, and then when C, D, or E comes back it goes down again -30? Ok, so C, D, or E, come in the room - but they leave. So, I only want the healthbar to lower once per each time they enter the room. Could you offer any code help?
 

TailBit

Member
Set flag to false in create event.
GML:
if(your current statement){
    if(!flag){
      // lower hp here
      flag=true;
    }
}else{
  flag=false;
}
You could also use an alarm..
 

DreTazzz

Member
Hi everyone. Thanks for the help on my last problem - that works now. Lol, I now have a new issue I am asking help with, please.

What I want: so, whenever enemyA comes into the room, if I hold down SPACE bar I am not injured. But, if am not holding SPACE bar, then I only want the hp to go down -30 once.

What happens: when I am not holding SPACE bar, the hp goes all the way down, and the game ends.

enemyA does leave the room, and comes back randomly. I want the hp to go down -30 each time enemyA comes back, and the SPACE bar is not held down.


Here is my current code...

//code for HP meter depletion within the rooms

var condition_3 = keyboard_check(!vk_space);
var condition_4 = instance_exists(obj_enemyA);

if (condition_3 && condition_4) {
global.fear_hp -= 30;
}
 

Nidoking

Member
var condition_3 = keyboard_check(!vk_space);
Either you don't know what the ! operator does, or you don't know what vk_space means. Both can be found in the manual, although you might have to do a bit of creative digging into "operators" for the former. But if something isn't a boolean, you don't negate it. (Don't at me with the exceptions - if you know enough to know about exceptions, this post is not for you.) Negation turns true to false and false to true. vk_space is neither of those. It is a representation of a key on a keyboard. What is the opposite of a key on the keyboard? Don't ask me. The result of keyboard_check, however, is a boolean value and can be negated. You just have to put the negation in the right place.
 

DreTazzz

Member
...so is anyone nice enough to help me with the proper coding for my above problem - rather than just insult me? I would think these forums are to help uplift and motivate aspiring Coders - not tell them ONLY what they did wrong. Perhaps I will check on Reddit, since too many times people on YoYo like to cast stones, and act like Demigods. I can only hope in the future that if those people seek help in something they are not versed in, they are made to feel less-than.

No need to respond in lashing back to me - I dont break easily, but I made my point.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Please report offensive behavior instead of replying to it and let staff deal with it. ;)

Either way, you are negating a key code. That doesn't make much sense to your program, so the results will be unexpected. Did you mean to negate the check whether it's held? You'd have to put the ! operator in front of keyboard_check in that case, not in front of the key code.
 

DreTazzz

Member
Please report offensive behavior instead of replying to it and let staff deal with it. ;)

Either way, you are negating a key code. That doesn't make much sense to your program, so the results will be unexpected. Did you mean to negate the check whether it's held? You'd have to put the ! operator in front of keyboard_check in that case, not in front of the key code.
Hi. Thanks for responding in a cool way. I appreciate the advice too. I am not even sure if I need to negate anything at all. Would you know the proper coding to have the below accomplished?

What I want: so, whenever enemyA comes into the room, if I hold down SPACE bar I am not injured. But, if am not holding SPACE bar, then I only want the hp to go down -30hp once.

enemyA does leave the room, and comes back randomly. So, I want the hp to go down -30 each time enemyA comes back, and the SPACE bar is not held down.

Thanks so much - hope you can help.
 

Nidoking

Member
Not holding the space bar down: !keyboard_check(vk_space)
Holding the opposite of the space bar (whatever that may be) down: keyboard_check(!vk_space)

The ! goes in front of the thing you want the opposite of.
 
Last edited by a moderator:
Top