Windows Problem with detecting nearest object.

N

nickvm98

Guest
I need help!
I got a GUI button that's supposed to light up when I get close to a certain object but it doesn't. I have trouble with this for over 20 minutes. An error occurs saying that it doesn;t know what object.x is. I need the wizards of code to help.
Name: obj_hud_warning
Create Event
///Initial Action
image_index = 0;
image_speed = 0;
font = spr_font_default_small;(This is for a script.)
target=0;

Step Event
///Sprite
x=view_xview+712
y=view_yview+24
if global.inter = 0 {image_index = 1} else image_index = 0;

target = instance_nearest(obj_player.x,obj_player.y,obj_enemy);
if point_distance(target.x,target.y,obj_player.x,obj_player.y) <= 32 {global.inter = 1}
__________________^^error here.
Draw Event
///Sprite
draw_sprite(sprite_index,image_index,view_xview+712,view_yview+24);
scr_font(view_xview+718,view_yview+22,string(target),font) //This is for seeing if "target" works.
 
A

Aura

Guest
Does the error message show up even if an instance of obj_enemy exists in the room? If you're not sure if an instance would exist during a certain timeframe, you should always make sure one exists.

Code:
if (instance_exists(obj_enemy) && instance_exists(obj_player)) {
   target = instance_nearest(obj_player.x, obj_player.y, obj_enemy);
   if (point_distance(target.x, target.y, obj_player.x, obj_player.y) <= 32) {
      global.inter = 1
   }
}
Also, it would be better if you post the actual error message instead.
 
N

nickvm98

Guest
Nah, the enemies are in the room, I already put them there. There is like 6 in the room.
Sorry but here's the error message. It doesn't pick up obj_enemy.x or target.x.

ERROR in
action number 1
of Step Event
for object obj_hud_warning:

Error in code at line 6:
if point_distance(target.x,target.y,obj_player.x,obj_player.y) <= 32
^
at position 27: Unknown variable x
 

Roderick

Member
That means that there's no variable named x in the instance target. Since every instance of every object has an x, target has a value in it that isn't a valid instance id.

I just noticed that your create code says target=0. That's your problem. Use
Code:
target = noone;
 
N

nickvm98

Guest
I put that in the create event and it still had the same error. Is there a different way of doing this nearest object detection thing?
 
N

nickvm98

Guest
I did it! I used distance to object instead of point distance.
 
Top