Check # of Instances Within a Certain Distance?
#1
Posted 20 August 2011 - 09:39 PM
Basically, I have an object that I want to check the area around it for the number of instances of another object. If there's fewer than a certain number, it'll create more, otherwise it'll do nothing. Anybody know how this would be done? I'm using GM 8.1 Pro version.
#2
Posted 20 August 2011 - 09:41 PM
temp=0
with (object) //Put in the object you're looking for.
{
if distance_to_object(other)<range //Whatever your range is.
other.temp+=1
}
The variable "temp" will be your number of that object in a range.
Edited by ATH Connect, 20 August 2011 - 09:42 PM.
#3
Posted 20 August 2011 - 09:47 PM
Script: instance_range_count
/* instance_range_count(obj, radius) */
//Arguments
var object, radius;
object = argument0;
radius = argument1;
//Loop through instances
var inst, i, maxinsts, total;
maxinsts = instance_number(object);
total = 0;
for (i=0; i<maxinsts; i+=1) {
inst = instance_find(object, i);
if (distance_to_object(inst) <= radius) {
total += 1;
}
}
//Done
return total;
You can now use this to calculate the number of objects in a circular area --- for example instance_range_count(obj_otherobject, 40) will equal the number of instances of obj_otherobject within a radius of 40 pixels of the current instance. You can then use that figure to determine whether to create more instances. Simple as that.
GameGeisha
#4
Posted 20 August 2011 - 10:00 PM
Basically, obj_hive is a beehive, that needs to spit out an instance of obj_bee if there are fewer than 5 bees within a 250-pixel area. So, I'd put a step event in obj_hive which triggers this script, and then what?
Thanks for your help, this is all pretty overwhelming for a total beginner. :D
#5
Posted 20 August 2011 - 10:03 PM
Step Event for your bee hive object. Just put this code down.
temp=0
with (obj_bee) //Put in the object you're looking for.
{
if distance_to_object(other)<250 //Whatever your range is.
other.temp+=1
}
if temp<5
instance_create(x,y,obj_bee)
#6
Posted 20 August 2011 - 10:09 PM
No, this is not the right way to use this script, it is not meant to be executed alone. Scripts can also be used like functions, much like sin(), cos() and tan(). The script that I gave you is one such example.Basically, obj_hive is a beehive, that needs to spit out an instance of obj_bee if there are fewer than 5 bees within a 250-pixel area. So, I'd put a step event in obj_hive which triggers this script, and then what?
This is how you're supposed to be using it:
- Go into the step event of obj_hive.
- Insert
instance_range_count(obj_bee, 250) < 5- After the
GameGeisha
#7
Posted 22 August 2011 - 05:43 PM
Actually, now that i've tried implementing it, that method (instance_range_count) didn't work at all. When I run the game it gives me an error message: "Unknown function or script: instance_range_count". Any suggestions?
Tried using your method too, ATH Connect, and while it does work, it instantly creates stationary objects. I want there to be some delay between obj_bee instances being spawned, as well as create moving instances. What should I try?
Oh man, I'm an idiot. instance_range_count is the script you posted earlier. Ignore my inability to grasp this, plz
Okay, works perfectly, now that i'm actually doing what you said I should do. Thanks again for all the assistance, both of you will definitely be getting Special Thanks credits in the final game
MODERATOR EDIT : I have merged your FIVE posts into one. Please, if you have something to add to a topic, use the EDIT button at the bottom of the post as consecutive posting is against the forum bumping rules.
Edited by Nocturne, 22 August 2011 - 10:34 PM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











