GameMaker Instances not creating at seemingly random times.

ceaselessly

Member
Hi there!

I'm working on an engine for a side-scrolling shooter. I'm having an unusual problem with creating bullet instances.

The player object is in charge of creating the bullet objects, and at seemingly random points in the room, the code that creates bullets stops creating them! Sometimes this happens when the player object is moving, and other times when it's still. Here's the code I've written for creating the bullets:

Code:
if (keyshoot) && (shootdelay = 0)
{
    //basic weapon
    if (weap = 0)
    {
        bb = instance_create_depth(x+10,y,depth+1,o_bullet);
        bb.master = self;
        bb.direction = 0;
        bb.speed = 25;
        shootdelay = 2;
    }
    
    //multi-shot
    if (weap = 1)
    {
        bb1 = instance_create_depth(x+10,y,depth+1,o_bullet);
        bb2 = instance_create_depth(x+10,y,depth+1,o_bullet);
        bb3 = instance_create_depth(x+10,y,depth+1,o_bullet);
        bb1.master = self;
        bb2.master = self;
        bb3.master = self;
        
        with (bb1)
        {
            direction = 0;
            speed = 25;
        }
        
        with (bb2)
        {
            direction = 8;
            speed = 25;
        }
        
        with (bb3)
        {
            direction = -8;
            speed = 25;
        }
        
        shootdelay = 5;
    }
}
The more bizarre part of this is that the latter part of this code, the "//multi shot" block, works *fine* for the "bb2" and "bb3" bullet objects, i.e. those that it creates at an angle. So it appears the problem is isolated to any bullets I create where direction = 0. I'm baffled on this one! If anyone has any insights, or has encountered issues like this before, I'd be thrilled if you could give me a hand!

Here's the whole step event, if that helps:
Code:
//key checks

keyup = keyboard_check(vk_up);
keydown = keyboard_check(vk_down);
keyleft = keyboard_check(vk_left);
keyright = keyboard_check(vk_right);

keyshoot = keyboard_check(ord("Z"));
keyweap = keyboard_check_pressed(vk_shift);

//movement

if (keyup) && (movey > -12)
{
   movey -= 1;
}

if (keydown) && (movey < 12)
{
   movey += 1;
}

if (keyleft) && (movex > -12)
{
   movex -= 1;
}

if (keyright) && (movex < 12)
{
   movex += 1;
}

//decel
if (!keyup) && (!keydown)
{
   if (movey > 0) movey -= 1;
   if (movey < 0) movey += 1;
}


if (!keyright) && (!keyleft)
{
   if (movex > 0) movex -= 1;
   if (movex < 0) movex += 1;
}

//movement based on acel/decel
hspeed = movex;
vspeed = movey;

//shooting
if (keyshoot) && (shootdelay = 0)
{
   //basic weapon
   if (weap = 0)
   {
       bb = instance_create_depth(x+10,y,depth+1,o_bullet);
       bb.master = self;
       bb.direction = 0;
       bb.speed = 25;
       shootdelay = 2;
   }
   
   //multi-shot
   if (weap = 1)
   {
       bb1 = instance_create_depth(x+10,y,depth+1,o_bullet);
       bb2 = instance_create_depth(x+10,y,depth+1,o_bullet);
       bb3 = instance_create_depth(x+10,y,depth+1,o_bullet);
       bb1.master = self;
       bb2.master = self;
       bb3.master = self;
       
       with (bb1)
       {
           direction = 0;
           speed = 25;
       }
       
       with (bb2)
       {
           direction = 8;
           speed = 25;
       }
       
       with (bb3)
       {
           direction = -8;
           speed = 25;
       }
       
       shootdelay = 5;
   }
}

//check shoot delay
if (shootdelay > 0)
{
   shootdelay -= 1;
}

//weapon cycling
if (keyweap)
{
   if (weap < weapmax)
   {
       weap += 1;
   }
   else
   {
       weap = 0;
   }
}

Thanks!
 
N

Nembo

Guest
You get the same issue with weap equals 1 and 0? Have you looked at your code while running the debugger?
 

Simon Gust

Member
The only thing that might be an issue is floating point precision with the first if-statement (shootdelay = 0).
The variable might never hit exactly 0. Try checking for less or equal.
Code:
if (keyshoot && shootdelay <= 0)
 

ceaselessly

Member
The only thing that might be an issue is floating point precision with the first if-statement (shootdelay = 0).
The variable might never hit exactly 0. Try checking for less or equal.
Code:
if (keyshoot && shootdelay <= 0)
Thanks for that tip! The "shootdelay" variable is decreased by 1 in the step event whenever it's above 0, so that unfortunately did not fix it. (I did change the code anyway though, so it's probably good practice to check variables within ranges anyway. :)) It seems to be happening at specific Y-axes in the room, but I'm not able to isolate the problem. I'm unfortunately not sure how to best describe the problem otherwise, so here's a screengrab of the problem:
https://imgur.com/a/ZcTZF

I'm holding the shoot key the entire time, so any time there are no bullets it is the issue I'm talking about.

You get the same issue with weap equals 1 and 0? Have you looked at your code while running the debugger?
Yes! Same issue, unfortunately. weap=1 creates three bullet objects, and the one where direction = 0 fails to create at the same points as the one when weap = 0. I'm fairly new to GMS (used to use GM back in the versions 5-7 days!) -- do you have any tips or resources for getting started with the debugger?

Thanks to you both!
 
Last edited:

TheouAegis

Member
The only thing that might be an issue is floating point precision with the first if-statement (shootdelay = 0).
The variable might never hit exactly 0. Try checking for less or equal.
Code:
if (keyshoot && shootdelay <= 0)
Doubtful because he's using whole numbers. Also doubtful because the multi-shot still works supposedly (minus the middle bullet).

I'd debug if the bullets legit aren't being created or if they are being destroyed instantly.
 
Top