• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Limiting amount of player bullets on screen

L

L1NDHOLM

Guest
Hello, i'm having trouble with limiting the amount of bullets ( to 3) my obj_player can have on the screen at the same time. I can subtract bullets from my bullets_total, but adding won't work. Here is my code:

obj_player Create
Code:
//max bullets on the screen at the same time
bullets_total = 3;
bullet_speed = 6;
obj_player Step
Code:
//Bullets; direction, speed, etc.
if (keyboard_check_pressed(vk_control) && bullets_total > 0)
    {
        inst = instance_create_layer(x, y, "Bullets", obj_bullet);
        inst.direction = (obj_player.image_xscale == 1) ? 0 : 180;
        inst.speed = bullet_speed;
        bullets_total -= 1;
    }
obj_bullet Step
Code:
hit = instance_place(x, y, obj_enemy);

if (hit != noone)
{
    hit.hp -= 1;
    instance_destroy();
    bullets_total += 1;
}
//-----------------------------------------
var cam = view_camera[0];
var x1 = camera_get_view_x(cam);
var y1 = camera_get_view_y(cam);
var x2 = x1 + camera_get_view_width(cam);
var y2 = y1 + camera_get_view_height(cam);

if( !point_in_rectangle( x, y, x1, y1, x2, y2))
{
    instance_destroy();
    bullets_total += 1;
}
Any ideas on how to fix it or maybe there is better and cleaner way to do this?
 

Simon Gust

Member
You can add to bullets_total, you just need to add to the player's bullets_total and not the bullet's bullets_total.
Then again, there may be a better way.
Code:
bullets_total = 3 - instance_number(obj_bullet);
and you won't ever have to add or subtract to bullets_total. This formula will do it all for you.
 
L

L1NDHOLM

Guest
I tried this aswell:

obj_player Step
Code:
//Bullets; direction, speed, etc.
if instance_number(obj_bullet < 3)
    {
    if (keyboard_check_pressed(vk_control))
        {
        inst = instance_create_layer(x, y, "Bullets", obj_bullet);
        inst.direction = (obj_player.image_xscale == 1) ? 0 : 180;
        inst.speed = bullet_speed;
        }
    }
This did not work, no bullets coming out at all.
 

Simon Gust

Member
I tried this aswell:

obj_player Step
Code:
//Bullets; direction, speed, etc.
if instance_number(obj_bullet < 3)
    {
    if (keyboard_check_pressed(vk_control))
        {
        inst = instance_create_layer(x, y, "Bullets", obj_bullet);
        inst.direction = (obj_player.image_xscale == 1) ? 0 : 180;
        inst.speed = bullet_speed;
        }
    }
This did not work, no bullets coming out at all.
You have to watch your code logic here.
What do you expect this code
Code:
if instance_number(obj_bullet < 3)
to do?

obj_bullet is an object in your resource tree. It occupies a number based on it's ordering of the resource tree.
So essentially, you're checking if obj_bullet is somewhere in the first 3 spots of your resource tree.
Then if that is the case, it will pre-evaluate into this
Code:
if instance_number(true)
This now, will count how many instances there are of object 1 (true = 1), or the second object in your resource tree.

If obj_bullet was not in the first 3 spots in the resource tree. It will pre-evaluate into this
Code:
if instance_number(false)
This will count how many instances there are of object 0 (false = 0), or the first object in your resource tree.

This is definetly not what you want at all. You should seperate your logic like this
Code:
if (instance_number(obj_bullet) < 3)
 
L

L1NDHOLM

Guest
Thank you Simon Gust i'm very new to programming so all help is welcome.
I am trying what you preposed:
Code:
bullets_total = 3 - instance_number(obj_bullet);
but i'm getting the weirdest bug with my code:
Code:
if (keyboard_check_pressed(vk_control) && bullets_total > 0)
    {
        inst = instance_create_layer(x, y, "Bullets", obj_bullet);
        inst.direction = (obj_player.image_xscale == 1) ? 0 : 180;
        inst.speed = bullet_speed;
        bullets_total = 3 - instance_number(obj_bullet);
    }
if i shoot 2 bullets and let them get destroyed i can shoot 2 more BUT if i shoot 3 bullets and let them get destroyed i can't shoot at all after, i don't get why.
 

Dr_Nomz

Member
I have a better idea, I think:
Have a global variable for the total bullets you want (initiate it in an object that'll get loaded before everything else, otherwise you'll get errors) and in the bullet create event, have it increase by one, but lower it once it's destroyed. (So just before it hits a wall or enemy, global.total_bullets -=1, instance_destroy(). Having instance destroy before other code can make it not run any of the other code in some cases, so always have that last.)

I hope that helps.
 
That's because you're only updating bullets_total when you press space. So you shoot twice, bullets_total = 1, and then when you press space again it checks to see if bullets_total is greater than 0 (which it is) and it lets you fire more. However, if you fire 3 bullets, bullets_total = 0 which, because you're only updating bullets_total when you're pressing space AND bullets_total > 0, the code within the if statement will never fire and bullets_total will never again be able to get above 0. Following your code logic step by step in your head (or in the debugger) is a vital skill that you need to develop in order to be able to code self-sufficiently.

Move your bullets_total update outside of the if check to fix it.
 
L

L1NDHOLM

Guest
Thank you RefresherTowel, it works prefectly now, i'm sure there is a way to write it cleaner though.
You are right, i need to get better at following my code logic. I just recently started programming, it's fun but very hard (for me). I'm sure i'll get better with time, watching tutorials and getting help from you guys. Thanks again.
 
Top