[SOLVED] Bullets not appearing in GM 2.3 using Scripts

T

T-Radar

Guest
Hello all

I have been following the basic "Space Rocks" Tutorial (I've finished the "factions" section) and the script for creating bullets from before the 2.3 update was:

GML:
    var _dir = argument[0];
    var _spd = argument[1];
    var _fac = argument[3];
    var _creator = id;

    audio_play_sound(snd_zap,1,false);
    var inst = instance_create_layer(x,y,"Instances",obj_bullet);

    with(inst)
    {
        direction = _dir;
        speed = _spd;
        faction = _fac;
        creator = _creator;
    
        if(faction == factions.ally) image_blend = c_aqua;
        else if (faction == factions.enemy) image_blend = c_red;
    }

After updating to 2.3, the script was automatically updated to a function but the bullets did not appear.
I saw the changes to scripts in this new version and tried to adapt the script correctly, ending up with this:

GML:
function create_bullet(_dir, _spd, _fac)
{

    var _creator = id;

    audio_play_sound(snd_zap,1,false);
    var inst = instance_create_layer(x,y,"Instances",obj_bullet);

    with(inst)
    {
        direction = _dir;
        speed = _spd;
        faction = _fac;
        creator = _creator;
    
        if(faction == factions.ally) image_blend = c_aqua;
        else if (faction == factions.enemy) image_blend = c_red;
    }

}
But the bullets still did not appear, although the sound plays every time I fire a bullet.

I tried the debbuger and all the variables seem to have the correct values and the object is marked as "visible".

Other odd things happened:

- When going in high speed with my ship and facing the other way... sometimes the bullets do appear briefly.

- When using the following piece of code on the step event of the ship (it's wrong but used it for debbuging):

GML:
if(keyboard_check_pressed(vk_space))
{
    create_bullet(image_angle, bulletSpd, faction);
    var inst = instance_create_layer(x,y,"Instances",obj_bullet);
}
The first bullet shows up (the one created by the create_bullet function) and the second one (created using the instance_create_layer) destroys my own ship (since it collides with it upon creation and the bullet in this case has an undefined "faction").

How can I fix my function in order for the bullets to appear?

Thank you in advance for any help.
 
E

eunit08

Guest
hello I did the same tutoriail. If you go to the bullet object where the collision with the faction event, the "if (other == creator) { exit; } " code is not run so it runs the instance_destroy code. if you comment out the instance_destroy, the bullet will come out but it will not destroy until it leaves the room. That's where the problem lies. I am searching for a reason why the creator code isnt running in that event. Something has changed in the new version of game maker regarding the ID or the creator variable.

If you can contact friendly cosmonaut then we can move forward with our lives
 
Yesterday, I migrated the Space Rocks GML code I did a month ago. I encountered the same problems.

To fix the bullet hitting and destroying your own ship, change the bullet object's collision script with faction object from:

GML:
if (other == creator) exit;
to:
GML:
if (other.id == creator) exit;
It seems a GMS 2.3 object instance does not resolve/stringify to its id when called directly. So, the id has to be explicitly called. The bullet's creator variable contains the same value as bullet creator's instance id (see the create_bullet() function).
 
Last edited:
E

eunit08

Guest
nice! I found a fix as well! I simply moved the instance_destroy statement to the bottom of the code. But your solution is better!
 
T

T-Radar

Guest
Yesterday, I migrated the Space Rocks GML code I did a month ago. I encountered the same problems.

To fix the bullet hitting and destroying your own ship, change the bullet object's collision script with faction object from:

GML:
if (other == creator) exit;
to:
GML:
if (other.id == creator) exit;
It seems a GMS 2.3 object instance does not resolve/stringify to its id when called directly. So, the id has to be explicitly called. The bullet's creator variable contains the same value as bullet creator's instance id (see the create_bullet() function).
The bullet destroying my own ship was an intentional bug I introduced on the ship step event in order to zero-in on the problem. But your suggestion worked, the bullets now appear! :D Thank you very much!
 
Last edited by a moderator:

BonezyBoy

Member
I followed the tutorial and changed my game code of your inputs, but my bullets don't shoot from my ship, they just spawn and when i move they stay there. What code am i missing and what code should i have in?
 
I followed the tutorial and changed my game code of your inputs, but my bullets don't shoot from my ship, they just spawn and when i move they stay there. What code am i missing and what code should i have in?
Check direction and speed in the create_bullet() function/script.
 
Top