GML RPG Chest?

M

Marco Savarese

Guest
Hello everyone, i've been trying to make a good chest opening system. First, having a chest placed in the map, it will open only if the player is in its radius and presses the space key. Also, i want this to happen only if the player is facing in the right direction. So, if the player's on the chest's left, he should be looking towards it, at the player's right. I managed to make the first point, but not the second one. Here's the code. How should I change it?
Player's image index is: 0-faces down, 1- faces up, 2-faces right, 3-faces left

global.range=1;
if (distance_to_object(obj_player) = global.range)
{
image_index=1; //chest image index
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
The x variable holds the horizontal position of an instance in the room.

You can compare the player's x and the chest's x to find out which one is on which side.
 

Vallo92

Member
Comprare the x or y position of the player and te x or y position of the chest. after that, based on the result, check the direction of the player based on its sprite.

Example:
Code:
if (player.x > chest.x) {
    if (player.image_index == 3) {
        // Open chest
    }
}
Spero di esserti stato d’aiuto! ;)
 

woods

Member
how i handled this type of situation was to make a target box that followed in front of the player so i had to be facing my chicken to pick it up.. simply add collision with the targetbox to your player/chest interaction code
https://steamcommunity.com/sharedfiles/filedetails/?id=1721262911


obj_target_box step event
Code:
/// follow in front of player

//if player
if instance_exists(obj_player)
{
depth = obj_player.depth-1
dir = (obj_player.dir)
   
    if (dir = 0)
        {
        x = obj_player.x + 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // right
        }
        else
    if (dir = 1)
        {
        x = obj_player.x
        y = obj_player.y + 32
        image_speed=0; sprite_index=spr_target_box // down
        }
        else
    if (dir = 2)
        {
        x = obj_player.x - 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // left
        }
        else
    if (dir = 3)
        {
        x = obj_player.x
        y = obj_player.y - 32
        image_speed=0; sprite_index=spr_target_box // up
        }
}
 
M

Marco Savarese

Guest
Comprare the x or y position of the player and te x or y position of the chest. after that, based on the result, check the direction of the player based on its sprite.

Example:
Code:
if (player.x > chest.x) {
    if (player.image_index == 3) {
        // Open chest
    }
}
Spero di esserti stato d’aiuto! ;)
Ho provato quella parte di codice ma ancora in determinate posizioni non funziona, e ovviamente ho fatto una if per ciascun caso. Grazie della risposta :D
 
M

Marco Savarese

Guest
The x variable holds the horizontal position of an instance in the room.

You can compare the player's x and the chest's x to find out which one is on which side.
If I do it and compare the Y values they won't open correctly if the player's on the top or the bottom side
 
M

Marco Savarese

Guest
how i handled this type of situation was to make a target box that followed in front of the player so i had to be facing my chicken to pick it up.. simply add collision with the targetbox to your player/chest interaction code
https://steamcommunity.com/sharedfiles/filedetails/?id=1721262911


obj_target_box step event
Code:
/// follow in front of player

//if player
if instance_exists(obj_player)
{
depth = obj_player.depth-1
dir = (obj_player.dir)
  
    if (dir = 0)
        {
        x = obj_player.x + 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // right
        }
        else
    if (dir = 1)
        {
        x = obj_player.x
        y = obj_player.y + 32
        image_speed=0; sprite_index=spr_target_box // down
        }
        else
    if (dir = 2)
        {
        x = obj_player.x - 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // left
        }
        else
    if (dir = 3)
        {
        x = obj_player.x
        y = obj_player.y - 32
        image_speed=0; sprite_index=spr_target_box // up
        }
}
Gonna try this tomorrow, thanks for your help!
 
M

Marco Savarese

Guest
how i handled this type of situation was to make a target box that followed in front of the player so i had to be facing my chicken to pick it up.. simply add collision with the targetbox to your player/chest interaction code
https://steamcommunity.com/sharedfiles/filedetails/?id=1721262911


obj_target_box step event
Code:
/// follow in front of player

//if player
if instance_exists(obj_player)
{
depth = obj_player.depth-1
dir = (obj_player.dir)
  
    if (dir = 0)
        {
        x = obj_player.x + 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // right
        }
        else
    if (dir = 1)
        {
        x = obj_player.x
        y = obj_player.y + 32
        image_speed=0; sprite_index=spr_target_box // down
        }
        else
    if (dir = 2)
        {
        x = obj_player.x - 32
        y = obj_player.y
        image_speed=0; sprite_index=spr_target_box // left
        }
        else
    if (dir = 3)
        {
        x = obj_player.x
        y = obj_player.y - 32
        image_speed=0; sprite_index=spr_target_box // up
        }
}
It worked!
 
Top