Jump to content


Photo

Check # of Instances Within a Certain Distance?


  • Please log in to reply
6 replies to this topic

#1 thescandalousimplication

thescandalousimplication

    GMC Member

  • New Member
  • 10 posts

Posted 20 August 2011 - 09:39 PM

Hey everybody, I'm still getting used to using GM, and I'm not a programmer at all so I haven't really messed with scripting yet, but I suspect I'll need to script to do what i'm trying to do here.

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.
  • 0

#2 ATH Connect

ATH Connect

    GMC Member

  • New Member
  • 20 posts
  • Version:GM8

Posted 20 August 2011 - 09:41 PM

The function for finding the number of instances within a range:

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.

  • 0

#3 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2740 posts
  • Version:GM:Studio

Posted 20 August 2011 - 09:47 PM

Loop through each instance of the other object, check its distance to the object, add 1 to a total if it is within the range.

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

#4 thescandalousimplication

thescandalousimplication

    GMC Member

  • New Member
  • 10 posts

Posted 20 August 2011 - 10:00 PM

Sorry, really dense on this stuff. Like I said, no programming knowledge/experience. How/where do I use that scripting? I know how to make a script and insert it as an action in the base object, but I'm not sure what to do beyond that.

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

#5 ATH Connect

ATH Connect

    GMC Member

  • New Member
  • 20 posts
  • Version:GM8

Posted 20 August 2011 - 10:03 PM

Well my code is a simple version of it, still accurate though :P

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)


  • 0

#6 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2740 posts
  • Version:GM:Studio

Posted 20 August 2011 - 10:09 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?

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.

This is how you're supposed to be using it:
- Go into the step event of obj_hive.
- Insert :GM060:, type this verbatim into the expression field:
instance_range_count(obj_bee, 250) < 5
- After the :GM060:, create your obj_bee instances.

GameGeisha
  • 0

#7 thescandalousimplication

thescandalousimplication

    GMC Member

  • New Member
  • 10 posts

Posted 22 August 2011 - 05:43 PM

Thanks so much!! Man, I really need to figure out coding... but this helps a ton for now!

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 :D

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 :D


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users