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

Change variable for all instances one at a time using proximity

B

Brackleforth

Guest
Hello,

I'm trying to change a variable from false to true for all instances of an object.

However, I need the values to change one at a time according to which is the closest to the player object. So, the nearest object changes, then the second-nearest, then the third-nearest, etc, all in one process. Is there a way to select objects, in a for loop using instance_nearest maybe, and adjust their variables one at a time like that? I feel like this should be possible, but I can't visualize what to do.

Thanks!
 

cidwel

Member
Yeah.. I'm doing the same in my game.
1. Get a list of objects that are in range (circle) from a object (check getInstancesInRange in gmlscripts)
2. Create a priority list, then:
  • Iterate the previous list of instances with a for loop
  • Get the distance between origin and the iterated instance (distance_to_object)
  • Add to the list a relationship of IDs and distance between instances and origin
    Code:
    ds_priority_add(dsListId, objId, distanceFromOrigin)
3. When you use ds_priority_delete_min you will get the nearest instance ID of the previous list

Probably it is not the best method but this works fine and fits my needs
 
Top