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

Legacy GM Instances Finding other Instances

Valerie

Member
Hey folks!

So I'm pretty stuck on an issue. I have a game where an object known as a "civ" generates a random name. What I'm trying to do is to have an instance of that civ object to be able to recognize the id of a civ that is nearby, so they can interact. However, there is no instance_nearest function that excludes itself, meaning the civs constantly think the closest civ to them is itself.

Here is my Step Event:

Code:
var _x = x;
x -= 10000000;
var civ_seen = instance_nearest(_x, y, obj_civ_test);
x = _x;
if civ_seen != id && civ_seen != noone
{
// You found an instance!
return civ_seen;
}
else
return noone;
and here is my Draw Event, where I want it to draw the name of the instance of civ that is nearest to itself

Code:
draw_text(x-20, y-50, string(civ_seen.name));
When I do this, the game returns "civ_seen not set before reading it."

Any help regarding this would be greatly appreciated! This whole game is based around instances of the civ object interacting with each other, so if they can't call those specific instance ids and manipulate them I'm pretty much sunk. Thank you!
 

FrostyCat

Redemption Seeker
You declared civ_seen as a local variable, that makes it go away by the end of the Step event. Get rid of the var if you want it to travel between events, and also give it a starting value in the Create event. return also makes no sense here.

Create:
Code:
civ_seen = noone;
Step:
Code:
var _x = x;
x -= 10000000;
civ_seen = instance_nearest(_x, y, obj_civ_test);
x = _x;
if (civ_seen == id)
{
  civ_seen = noone;
}
Draw:
Code:
if (instance_exists(civ_seen))
{
  draw_text(x-20, y-50, civ_seen.name);
}
Read up on how to use variable scope properly. Local variables for temporary values, instance variables for individual properties, global variables for environment-wide properties.
 

Binsk

Member
FrostyCat is right on about the variable scope here.

That said, to also answer your question about nearest excluding self, you can do this by checking every obj_civ_test manually and measuring the distance between it and yourself. This is effectively writing your own instance_nearest script but with a little extra code added.

For example, it may look something like this:
Code:
// Define some local variables to last the length of this script
var _smallest_distance = undefined; // The currently smallest distance in our loop so far
var _nearest_id = undefined; // The id of the instance that had the smallest distance thus far

// Now, loop through each civ object:
with (obj_civ_test){
   // If the current object ID equals the calling instance's (other) id (aka, the instance CALLING the "with" piece), continue to the next instance.
   if (id == other.id) continue;

   // Find the distance between us and the calling instance:
   var _distance = point_distance(x, y, other.x, other.y);

   // If this is our first check (aka., smallest distance is undefined)
   // or the distance to this object is smaller than our closest yet, record it
   // as the new closest.
   if (is_undefined(_smallest_distance) || _distance < _smallest_distance){
      _smallest_distance = _distance;
      _nearest_id = id;
   }
}

// If we are the only instance, let's change the "nearest" to noone to avoid errors.
if (is_undefined(_nearest_id))
   _nearest_id = noone;

// At this point, whatever _nearest_id is is the closest instance excluding yourself.
 

Valerie

Member
@FrostyCat That was it! It was because I was declaring it as a local variable. Included a screenie of it working :) Thanks a lot!



@Binsk that makes sense! Thanks for the help. I might use a combination of these methods for some of the things Im trying to do later in this project
 
Last edited by a moderator:
Top