B
Beggar Studios
Guest
Hey guys!
I have a system in my game where if you get close to a chest or npc a button prompt (little hovering A button) spawns above their head. This works fine, but the problem is that when you are in range of multiple entities all entities spawn a button prompt. I want to set it up so that if multiple entities are in range only the closest entity spawns a button prompt. I was thinking I could store all nearby entities in a data structure and use instance_destroy() on the button prompt if they are no longer the closest entity to the player, but I'm not sure. Here is my current code!
scr_move_state (detecting nearby entities and spawning the prompt generator for them)
The problem is this will spawn prompts for every nearby instance
obj_prompt_generator (drawing the prompts and destroying them when no longer near)
So this is how my system is currently set up. Does someone have an idea of how I could destroy the button prompt generator if the instance is no longer the nearest chest or npc instance to the player?
I have a system in my game where if you get close to a chest or npc a button prompt (little hovering A button) spawns above their head. This works fine, but the problem is that when you are in range of multiple entities all entities spawn a button prompt. I want to set it up so that if multiple entities are in range only the closest entity spawns a button prompt. I was thinking I could store all nearby entities in a data structure and use instance_destroy() on the button prompt if they are no longer the closest entity to the player, but I'm not sure. Here is my current code!
scr_move_state (detecting nearby entities and spawning the prompt generator for them)
Code:
// Initialize chest interaction prompts
distance = 30;
var chest_detected = collision_circle(x, y, distance, obj_container_parent, false, true);
if(chest_detected != noone && chest_detected.looted == false){
chest_prompt = instance_create(chest_detected.x, chest_detected.y, obj_prompt_generator);
chest_prompt.chestid = chest_detected;
chest_prompt.context = 'loot';
chest_prompt.kill_distance = distance;
}
// Initialize NPC interaction prompts
var npc_detected = collision_circle(x, y, distance, obj_npc_parent, false, true)
if(npc_detected != noone){
global.destination = npc_detected;
npc_prompt = instance_create(npc_detected.x, npc_detected.y, obj_prompt_generator);
npc_prompt.npcid = npc_detected;
npc_prompt.context = 'npc';
npc_prompt.kill_distance = distance;
}
obj_prompt_generator (drawing the prompts and destroying them when no longer near)
Code:
// Draw button prompt based on input selection
if(context == 'loot'){
if(global.gamepad_input == true){
draw_sprite(spr_prompt_gamepad, 0, x, y-20);
} else {
draw_sprite(spr_prompt_keyboard, 0, x, y-20);
}
}
if(context == 'npc'){
if(global.gamepad_input == true){
draw_sprite(spr_prompt_gamepad, 0, x+12, y-20);
} else {
draw_sprite(spr_prompt_keyboard, 0, x+12, y-20);
}
}
//Destroy the prompt when no longer near
var dis = point_distance(x, y, obj_player.x, obj_player.y);
var in_range = collision_circle(x, y, kill_distance, obj_player, false, true);
if(alpha < 1 && kill = false){
alpha += 0.01
}
if(in_range == noone){
kill = true
if(alpha > 0){
alpha -= 0.01;
}
if(alpha = 0){
instance_destroy();
}
}