• 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 instance_nearest not working

I need the guide point for the pedestrians in my GTA 2 styled game to find the nearest direction object so they can start the loop of walking along sidewalks. Long story short, I want them to get the nearest ped direction object, using a variable called resetPoint. This is the code I used:

Code:
resetPoint = instance_nearest( x , y , obj_pedTopLeft) or instance_nearest( x , y , obj_pedTopRight) or instance_nearest( x , y , obj_pedBottomLeft) or instance_nearest( x , y , obj_pedBottomRight);
I rewrote all the code for both the ped guide, and the actual peds (which what all the peds really do is follow their guide, which does work using instance_nearest)

I tried to make a special reset point, (using resetPoint = instance_nearest(obj_pedReset) but that still didn't work. I made a debug key so I can see what point they are going to. According to that, the id is just "1". Sometimes they will move linearly in a random direction, or they will follow one of the car objects.

What's funny is that I accidentally made it so that they get into any car they are close to, so sometimes they will go to a random parked car and start driving it, or they will chase a car that's driving down the road, or they will chase their own car after I steal it and when I get out they get back into it. But, it stopped being funny and it turned into a massive headache.
 
Your code is returning a boolean value because you are using the "or" operator.

Make a parent object for all the obj_ped*** objects, and use the parent object in a single instance_nearest() function.

Because a boolean value is the equivalent of a 0 or 1, you code is currently being treated as finding the nearest instance with an object index of either the first or second object in your resource tree.
 

FrostyCat

Redemption Seeker
You need to read this article: How NOT to use && and ||
How to Avoid Misusing && and ||
If you don't want to use && and || where they don't belong and not know what hit you, follow these 2 guidelines:
  • DO NOT translate English sentences word-for-word into GML symbols.
  • If either side of an && or || operator is not meant to be treated as a Boolean value (true/false), the code is wrong.
instance_nearest() is never meant to be treated as a Boolean value, so your code is wrong. It just sets resetPoint to 0 or 1. Though these may be valid object IDs and you wouldn't get an error, that doesn't mean everything is fine.

The quick solution is to create a new object called obj_pedTop and set it as the parent of the object types, then just look for instance_nearest(x, y, obj_pedTop). But in the long run, I would highly recommend just having one object type and using a finite state machine pattern to manage the different behaviours, instead of having one object per behaviour and manually duplicating code. The latter is a bad organization habit that a lot of rookies get into.
 
Your code is returning a boolean value because you are using the "or" operator.

Make a parent object for all the obj_ped*** objects, and use the parent object in a single instance_nearest() function.

Because a boolean value is the equivalent of a 0 or 1, you code is currently being treated as finding the nearest instance with an object index of either the first or second object in your resource tree.
Oh wow.
So I went and used a single reset object, and and made it so the guide draws a line from itself, to the reset object, and it's working, but now the guide is going in some random direction way off the screen. It has the correct instance ID, but it's not going there.
I also set it up so it displays the direction it's going in, the it's randomly going east, even though it was never near the object that tells it to go east (I set it to reset by default).
 

Joe Ellis

Member
yep, using or || && and combines questions and results in true or false, so any function that returns an instance id, will be >0 and equate to true(1), or not if it returns noone (-4) which in a combined case returns (0),
So if it is true, it'll point to the last created instance of the second object in the resource tree, and if it's false, it'll point to last created instance of the first object in the resource tree,

Did you make sure the new single reset object is a parent to all the ones you want to check? In this case you don't need this parent object to have an instance in the room, its just used to group all the object instances you wanna include in the nearest check
 
Last edited:

TheouAegis

Member
Also post your actual movement code. You're supposedly getting the correct id, so maybe your movement code itself is wrong.
 
Top