If statement && ||

N

NoFontNL

Guest
Hi all! I am making a CPU for a game. It's a 2D shooting game. But that doesn't matter. To get straight to the point, let's take a look at my code.

Me :: obj_player_red
Bot :: obj_bot

Code:
///Step Event obj_bot
if keyboard_check_pressed(vk_space){                              // If you pressed space ...
    if (obj_player_red.grounded) {                                         // If you are on the ground
        if !(ammo == 1 && obj_player_red.ammo == 0) {        // HERE!!
            scr_bot_shoot();
        } else {
           // do nothing
        }
    }
}
Where I placed a comment 'HERE!!' is where I somehow messed up. If you have full ammo, you have 5 bullets.
Don't ask me why I did the
Code:
  if !(ammo == 1 && obj_player_red.ammo == 0) {        // HERE!!
            scr_bot_shoot();
        } else {
           // do nothing
        }
because it is hard to explain, and yes, this code is needed.
The real problem: I want the bot to shoot when I shoot, but because of the code the bot won't shoot when I've 1 bullet left and when he has 1 bullet left. Because:

if !(ammo == 1 && obj_player_red.ammo == 0) {
First statement TRUE
Second statement FALSE
with the ! before it, it will reverse, but because of the &&, it will still return false, so it won't shoot.
I want the bot not to shoot when I've 0 ammo and when the bot has 1 ammo. So when both statemetns are both exactly what they are. So not when one statement is false that the whole statement between () is false.

To shorten everything:
How would I get it, that the if statement doesn't return false, when one of the statements inside it is false, but only when both of the statements are false.
So litterally if NOT (obj_player_red.ammo ==0 AND obj_bot.ammo==1) scr_bot_shoot
(When obj_player_red.ammo is 0 at the same time as obj_bot.ammo is 1) ** so not that if only obj_bot.ammo ==1 that it will return false.

I still don't exactly know how to explain it, but here's another explanation if you don't understand.
I've made this:
Code:
var disable0 = ammo == 1;
var disable1 = obj_player_red.ammo == 0;
var canshoot;
if (disable0) && (disable1) {
    canshoot = 0;
} else {
    canshoot=1;
}
If both of the disable variables return true, the bot can't shoot.
If one (or both) of the disable variables return false, I want the bot to shoot ( when you press spacebar )
But the bot doesn't shoot when I've 1 ammo and when the bot has 1 ammo (and I press spacebar)
Only I shoot.
 

Slyddar

Member
I want the bot not to shoot when I've 0 ammo and when the bot has 1 ammo
I want the bot to shoot ( when you press spacebar ) But the bot doesn't shoot when I've 1 ammo and when the bot has 1 ammo
Your explanation is difficult to follow, but below satisfies the 2 things you've asked for above. The bot will only shoot if the player has 1 or more ammo and the bot has 2 or more. Not sure if there are other conditions?

Code:
if (ammo >= 1 and obj_player_red.ammo > 0) {
  scr_bot_shoot();
}
 

TheouAegis

Member
- is transmutative across values, ! is not transmutative. You have to evaluate everything that's inside the parentheses first before evaluating the unary !NOT.

A = ammo == 1
B = obj_player_red.ammo == 0

If bot bos 1 ammo, A is true. If bot has any other amount of ammo, A is false.
If player has no ammo, B is true. If player has any ammo, B is false.

if !(A && B)

If A is true and B is true, the condition is false. If either is false, the condition is true.
 
D

drowned

Guest
if !(ammo == 1 && obj_player_red.ammo == 0)
is the same as
Code:
 if(ammo != 1 || obj_player_red.ammo != 0)
I don't think I follow your explanation, but are you sure this is what you want?
 
K

kevins_office

Guest
How would I get it, that the if statement doesn't return false, when one of the statements inside it is false, but only when both of the statements are false.
So litterally if NOT (obj_player_red.ammo ==0 AND obj_bot.ammo==1) scr_bot_shoot

Code:
if (ammo > 1 && obj_player_red.ammo > 0) scr_bot_shoot();
Same as...
Code:
if (ammo > 1 && obj_player_red.ammo > 0) {
    scr_bot_shoot();
}
FYI: There is never a reason to put an else {//do nothing}.
If there is nothing to do then don't have an else statement.
 
D

drowned

Guest
upon rereading, I'm thinking maybe what you are looking for is
Code:
if(obj_player_red.ammo > 0 && ammo != 1) scr_bot_shoot();
 
K

kevins_office

Guest
upon rereading, I'm thinking maybe what you are looking for is
Code:
if(obj_player_red.ammo > 0 && ammo != 1) scr_bot_shoot();
You do not want to use == or != in this case. He wants the enemy to shoot as long as it has more than one bullet.
When you use != 1 that literally means as long as it does not equal 1. Well 0 (zero) does not equal 1 so its okay to shoot if he has zero bullets?
The appropriate code for what he is looking for is...

Code:
if (ammo > 1 && obj_player_red.ammo > 0) scr_bot_shoot();
 
D

drowned

Guest
You do not want to use == or != in this case. He wants the enemy to shoot as long as it has more than one bullet.
When you use != 1 that literally means as long as it does not equal 1. Well 0 (zero) does not equal 1 so its okay to shoot if he has zero bullets?
The appropriate code for what he is looking for is...
If he is using that variable as an ammo counter, then you are correct. However, I'm under the impression that he's using it as a flag (because I read the original post, try it sometime)
var disable0 = ammo == 1;
 
N

NoFontNL

Guest
I still think I didn't explain it well...

I want the bot not to shoot when he has 1 ammo as long as I have 0 ammo. So I want that the bot actually can shoot when he has 1 ammo, and when I have 1 ammo.
 

Slyddar

Member
Is the variable ammo a Boolean or a representation of the number of ammo the bot or player have left?

I'll assume it's a Boolean, meaning it has a true or false value depending if they have ammo left or not.

I want the bot not to shoot when he has 1 ammo as long as I have 0 ammo. So I want that the bot actually can shoot when he has 1 ammo, and when I have 1 ammo.
if obj_player_red.ammo and ammo {
//player and bot have ammo
scr_bot_shoot();
}
 
N

NoFontNL

Guest
Is the variable ammo a Boolean or a representation of the number of ammo the bot or player have left?

I'll assume it's a Boolean, meaning it has a true or false value depending if they have ammo left or not.



if obj_player_red.ammo and ammo {
//player and bot have ammo
scr_bot_shoot();
}
It is an integer. If you reload, your ammo will be set to 5;
 

jo-thijs

Member
I still think I didn't explain it well...

I want the bot not to shoot when he has 1 ammo as long as I have 0 ammo. So I want that the bot actually can shoot when he has 1 ammo, and when I have 1 ammo.
The code you posted already does that.

if !(ammo == 1 && obj_player_red.ammo == 0) {
First statement TRUE
Second statement FALSE
with the ! before it, it will reverse, but because of the &&, it will still return false, so it won't shoot.
I want the bot not to shoot when I've 0 ammo and when the bot has 1 ammo. So when both statemetns are both exactly what they are. So not when one statement is false that the whole statement between () is false.
This is wrong, TRUE && FALSE equals FALSE and !FALSE equals TRUE, so the if-statement succeeds and the bot will shoot.

The real problem: I want the bot to shoot when I shoot, but because of the code the bot won't shoot when I've 1 bullet left and when he has 1 bullet left.
This might have an other reason.
If the code that makes the player shoot runs before the code you posted
and as a result obj_player_red.ammo gets decreased by 1 before the code you posted runs,
you get ammo == 1 and obj_player_red.ammo == 0, which fails in the if-condition,
causing the bot not to shoot.

Can you verify this is the case?
 

Slyddar

Member
If it's an integer then this code will do what you want. Bot will only fire if both bot and player have ammo. If that doesn't work, might be something else happening.
Code:
if obj_player_red.ammo > 0 and ammo > 0 {
  //player and bot have ammo
  scr_bot_shoot();
}
 

jo-thijs

Member
If it's an integer then this code will do what you want. Bot will only fire if both bot and player have ammo. If that doesn't work, might be something else happening.
Code:
if obj_player_red.ammo > 0 and ammo > 0 {
  //player and bot have ammo
  scr_bot_shoot();
}
It won't, because NoFontNL doesn't want to check if both parties still have ammo left here.
He literally wants to check if either the robot can afford another shot (and still have one left afterwards) or the player has at least one bullet left (and froms a danger to the robot?).
 
N

NoFontNL

Guest
It won't, because NoFontNL doesn't want to check if both parties still have ammo left here.
He literally wants to check if either the robot can afford another shot (and still have one left afterwards) or the player has at least one bullet left (and froms a danger to the robot?).
Exactly!
(When I jump, the bot shoots a perfect bullet so I land on top of it and can't do anything.)
But when I place a trampoline, the bot detects that and doesn't shoot. So I place a trampoline, jump and shoot one time. My ammo is 4; ammo of bot=5; I shoot to the right (towards bot) he shoots to the left ( Ammo - Me:3;Bot:4) I shoot again. (Me:2;Bot3) I shoot again. (Me1;Bot:2) I shoot again. (Me:0;Bot:1)
Now, when I press spacebar, the bot shoots and I can place a trampoline so the bot dies from his own bullet.
Trampolines reverse the direction of the bullet and the whoShot variable. So I want to check when the bot has 1 bullet left and when I have 0 ammo (bullets) left, that it won't shoot.
 

breakmt

Member
"when the bot has 1 bullet left and when I have 0 ammo" = "if (ammo == 1 && obj_player_red.ammo == 0) { }"
 
N

Nathanmg

Guest
Does the code work when both player and bot have more than 1 bullet left?

If so then:

This might have an other reason.
If the code that makes the player shoot runs before the code you posted
and as a result obj_player_red.ammo gets decreased by 1 before the code you posted runs,
you get ammo == 1 and obj_player_red.ammo == 0, which fails in the if-condition,
causing the bot not to shoot.

Can you verify this is the case?
 

jo-thijs

Member
Exactly!
(When I jump, the bot shoots a perfect bullet so I land on top of it and can't do anything.)
But when I place a trampoline, the bot detects that and doesn't shoot. So I place a trampoline, jump and shoot one time. My ammo is 4; ammo of bot=5; I shoot to the right (towards bot) he shoots to the left ( Ammo - Me:3;Bot:4) I shoot again. (Me:2;Bot3) I shoot again. (Me1;Bot:2) I shoot again. (Me:0;Bot:1)
Now, when I press spacebar, the bot shoots and I can place a trampoline so the bot dies from his own bullet.
Trampolines reverse the direction of the bullet and the whoShot variable. So I want to check when the bot has 1 bullet left and when I have 0 ammo (bullets) left, that it won't shoot.
Sounds like an interesting game.

Try this:
Have an additional variable "justShot" in obj_player_red.
Set this variable to 0 in the begin step event and set it to 1 when the player shoots a bullet.
Then change the if-statement in the code you posted to:
Code:
if !(ammo == 1 && obj_player_red.ammo + obj_player_red.justShot == 0)
 
N

NoFontNL

Guest
Thank you! Works like a charm! Thank you all for helping! It made me understand what the real problem was and how I could explain it! ^^
 
Top