• 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!

GML Implementation of the mechanics of the selection of weapons

Narhiz

Member
Hi. Recently I tried to get my character in game to pick up a weapon and drop it by pressing the F key. I was able to do this thanks to the help on this forum. But the selection of weapons took place in such a way that I simply replaced the standard sprite of my character with a sprite with a weapon and removed the weapon from the room. When the character dropped the weapon, his sprite changed back to the standard one and a new weapon appeared on the floor near the character. Now I want to implement this mechanic differently. It is necessary that when picking up a weapon, its sprite is replaced with another (when the character is holding it in his hand) and it is transferred from the floor to the character's hand. When throwing, it is necessary that a new weapon is not created, as in the old way, but that the weapon is moved from the hands to the floor and the sprite is replaced with the original one. In this case, after selection, it is necessary to make the weapon move and rotate with the character. I've tried using the lengthdir_x / y functions to implement the fit, but either they don't work or I'm using them incorrectly. I have not thought about synchronizing the movement and rotation of the weapon with the character yet, since it makes no sense without the right selection. Tell me, in which direction should I go to solve this problem? You do not need to upload the finished code, describe in general terms the principle of operation and what functions I should study.

Player Step Event:

GML:
// Movement

if (keyboard_check(ord("D"))) x += 4;
if (keyboard_check(ord("A"))) x -= 4;
if (keyboard_check(ord("W"))) y -= 4;
if (keyboard_check(ord("S"))) y += 4;

image_angle = point_direction(x, y, mouse_x, mouse_y);


// PickUp and drop the gun

if keyboard_check_pressed(ord("F"))
{
    if (player_state == PLAYER_STATE.emptyhanded)
    {
        var pos_gun = instance_place(x, y, obj_gun);
        if (pos_gun != noone)
        {
            lendir_x = lengthdir_x(36, image_angle)
            lendir_y = lengthdir_y(10, image_angle)
            player_state = PLAYER_STATE.holdingweapon
            obj_green_man.sprite_index = spr_green_man_gun
            with (pos_gun)
            {
                image_angle = point_direction(x ,y, mouse_x, mouse_y)
                pos_gun.sprite_index = spr_holding_gun
                x = obj_green_man.x + obj_green_man.lendir_x
                y = obj_green_man.y + obj_green_man.lendir_y
            }
        }
    }
    else
    if (player_state == PLAYER_STATE.holdingweapon)
    {
        player_state = PLAYER_STATE.emptyhanded
        obj_green_man.sprite_index = spr_green_man
        direction = point_direction(x, y, mouse_x, mouse_y);
        var xx = x + lengthdir_x(20, direction);
        var yy = y + lengthdir_y(20, direction);
        instance_create_layer(xx, yy, "Guns", obj_gun)
    }
}
Player Create Event:

GML:
enum PLAYER_STATE
{
    emptyhanded,
    holdingweapon
}

player_state = PLAYER_STATE.emptyhanded
P.S:
The text was written using google translate, so it may contain inaccuracies.
 

Ommn

Member
for the gun following the player
add the codes in last your codes in step event Player object:
GML:
obj_gun.image_angle=image_angle
obj_gun.x=x+lengthdir_x(36, image_angle)
obj_gun.y=y+lengthdir_y(10, image_angle)
 

Narhiz

Member
for the gun following the player
add the codes in last your codes in step event Player object:
GML:
obj_gun.image_angle=image_angle
obj_gun.x=x+lengthdir_x(36, image_angle)
obj_gun.y=y+lengthdir_y(10, image_angle)
lengthdir does not work in my case, because when I rotate my character, the x and y coordinates are constantly changing. Is there any way to change the x and y variables depending on the rotation of my character? To make it clearer, I left a link to the video.
 
The situation in your video is exactly what the lengthdir_* functions are for. Post your code for the lengthdir implementation so we can see where you are going wrong.
 

Narhiz

Member
The situation in your video is exactly what the lengthdir_* functions are for. Post your code for the lengthdir implementation so we can see where you are going wrong.
GML:
// Movement

if (keyboard_check(ord("D"))) x += 4;
if (keyboard_check(ord("A"))) x -= 4;
if (keyboard_check(ord("W"))) y -= 4;
if (keyboard_check(ord("S"))) y += 4;

image_angle = point_direction(x, y, mouse_x, mouse_y);

// PickUp and drop the gun

if keyboard_check_pressed(ord("F"))
{
    if (player_state == PLAYER_STATE.emptyhanded)
    {
        var pos_gun = instance_place(x, y, obj_gun);
        if (pos_gun != noone)
        {

            player_state = PLAYER_STATE.holdingweapon
            obj_green_man.sprite_index = spr_green_man_gun
            with (pos_gun)
            {
                pos_gun.sprite_index = spr_holding_gun
            }
        }
    }
    else
    if (player_state == PLAYER_STATE.holdingweapon)
    {
        player_state = PLAYER_STATE.emptyhanded
        obj_green_man.sprite_index = spr_green_man
        direction = point_direction(x, y, mouse_x, mouse_y);
        var xx = x + lengthdir_x(20, direction);
        var yy = y + lengthdir_y(20, direction);
        instance_create_layer(xx, yy, "Guns", obj_gun)
    }
}

if (player_state == PLAYER_STATE.holdingweapon)
{
    obj_gun.image_angle=image_angle
    obj_gun.x=x+lengthdir_x(36, image_angle)
    obj_gun.y=y+lengthdir_y(10, image_angle)
}

Here is the entire step event code for the player object. Moving the weapon along with the player using lengthdir starts at line 40
 
Couldn't you do this easily by just adding a "Holstered" variable in your code to check if or if not the weapon is currently being carried?

So in:
if (player_state == PLAYER_STATE.holdingweapon)
{
if(instance_exists(obj_gun)){obj_gun.Holstered=true}
}

Now in the gun object set.

if(Holstered){//Set Position to Player; sprite_index = spr_gun_holstered} etc...

Then when the player set's Holstered to be false say:
if(keyboard_check(ord("F"))){if(instance_exists(obj_gun)) {obj_gun.Holstered = false} }
The gun is dropped on the floor.
 
Change this:
Code:
if (player_state == PLAYER_STATE.holdingweapon)
{
    obj_gun.image_angle=image_angle
    obj_gun.x=x+lengthdir_x(36, image_angle)
    obj_gun.y=y+lengthdir_y(10, image_angle)
}
To this:
Code:
if (player_state == PLAYER_STATE.holdingweapon)
{
    obj_gun.image_angle=image_angle;
    var dist = 36;
    obj_gun.x=x+lengthdir_x(dist, image_angle);
    obj_gun.y=y+lengthdir_y(dist, image_angle);
}
And adjust the dist local variable until the gun is the right distance away. You generally do not want to be having two different numbers for x and y, otherwise the lengthdir will not return a proper circle (this is why your gun is barely moving along the y axis, but moves a lot along the x axis).
 
Top