Jump to content


Photo

Ignored instances


  • Please log in to reply
10 replies to this topic

#1 peco

peco

    GMC Member

  • New Member
  • 28 posts
  • Version:GM8

Posted 12 May 2012 - 11:14 AM

Hello!
I'm almost done with my top-down exploration/shooter. If this goes well it could get me into a very cool game design school. :D

Anyhow, there's a problem.
I want a number of instances (part of a city menu) to appear when the player gets close to the object "city".
Using the same mechanics I'd like the menu to automatically disappear when the player leaves the city.
I have tried several different setups and all work - on ONE city. As soon as I have more than one city they cancel out each other.

This is how it works right now:
In steps event for city:

if point_distance(city.x,city.y,ship.x,ship.y)<150)
set city menu visible to 1.

Now the menu becomes visible close to one instance of object city, but not the other...
Any ideas?

btw
I use GM 8.1 lite
and use DnD/GML about 65/35% .

I'm grateful for all and any help!
  • 0

#2 creators124

creators124

    awesomeliciousmember

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 12 May 2012 - 11:28 AM

Hello!
I'm almost done with my top-down exploration/shooter. If this goes well it could get me into a very cool game design school. :D

Anyhow, there's a problem.
I want a number of instances (part of a city menu) to appear when the player gets close to the object "city".
Using the same mechanics I'd like the menu to automatically disappear when the player leaves the city.
I have tried several different setups and all work - on ONE city. As soon as I have more than one city they cancel out each other.

This is how it works right now:
In steps event for city:

if point_distance(city.x,city.y,ship.x,ship.y)<150)
set city menu visible to 1.

Now the menu becomes visible close to one instance of object city, but not the other...
Any ideas?

btw
I use GM 8.1 lite
and use DnD/GML about 65/35% .

I'm grateful for all and any help!

you may want instance_deactivate() right?
because I can put that code in the ship not the city and deactivate when too far but close enough activate it!
if (point_distance(city.x,city.y,ship.x,ship.y)<150)
{
instance_activate(city)
}
else if (point_distance(city.x,city.y,ship.x,ship.y)>150)
{
instance_deactivate(city)
}

  • 0

#3 Jack Indie Box

Jack Indie Box

    GMC Member

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

Posted 12 May 2012 - 11:36 AM

you will need(if using more than one city) to use instance_nearest in your objects(that your trying to hide or reapear, linked to the nearest city) story the nearest city instance which you can use in your if statements later

city_inst=instance_nearest(x,y,obj_city);
  • 0

#4 peco

peco

    GMC Member

  • New Member
  • 28 posts
  • Version:GM8

Posted 12 May 2012 - 01:11 PM

Thanks for two great answers!
Wouldn't the deactivation code do the exact same thing as my current code, and have the cities cancel each other out? If not this could be just the thing I need.

you will need(if using more than one city) to use instance_nearest in your objects(that your trying to hide or reapear, linked to the nearest city) story the nearest city instance which you can use in your if statements later

city_inst=instance_nearest(x,y,obj_city);

I tried using instance_nearest, I guess I just don't get how to use it. GML is still kinda new to me. Would it be too much to ask you to write out an entire example?

I just wan't the instance "sell" and "buy" next to the closest city appear...


Thanks again, both of you!
  • 0

#5 johnki

johnki

    GMC Member

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

Posted 12 May 2012 - 01:22 PM

Instance_nearest() literally just grabs the nearest instance on the x, y coordinate plane.

If you have objects at (0,0), (1,4), and (5,2), and you ask the instance at (0,0) to grab the nearest instance, then it will return the instance at (1,4). That's all there is to it.

To be specific, what instance_nearest() does is return the ID of the nearest object of the specified type, both confirming that it is the nearest and allowing you to do operations to directly modify its variables without actually KNOWING that ID.

So, say the nearest city to the ship was instance #106457. Normally, you'd have to type (106457).x or something similar to access its variables, but with instance_nearest(), if you assign instance nearest to a variable, say nearest_city = instance_nearest(x, y, obj_city), then you can do something like nearest_city.x to access variables, or maybe with (nearest_city), and so long as you keep reassigning nearest_city to the nearest instance of obj_city (perhaps in the step event), then you won't have to worry about knowing the ID numbers and the ID numbers will continue to change, allowing you to do whatever operations between the ship and the city that you need to. :)

Edited by johnki, 12 May 2012 - 01:23 PM.

  • 0

#6 Jack Indie Box

Jack Indie Box

    GMC Member

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

Posted 12 May 2012 - 02:05 PM

johnki did a great job of explaining so i hope you understand it
  • 0

#7 peco

peco

    GMC Member

  • New Member
  • 28 posts
  • Version:GM8

Posted 12 May 2012 - 02:56 PM

Wow! Thanks Johnki. I thought I understood "nearest" and that my actual coding skills is what lacked, but I can see it was both. Thanks a lot.

So, if I understand you helpful forum heroes correctly, putting below code in my "City's" step event will create object sell when "Ship" is close enough:

if point_distance(city.x,city.y,ship.x,ship.y<150)
{instance_nearest(0,0,sell) visible = 1} 
else
{visible = 0;}

Did I get that right?
  • 0

#8 johnki

johnki

    GMC Member

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

Posted 12 May 2012 - 03:13 PM

With that one, you'd have to do

if (point_distance(x, y, x2, y2) < 150) {
   with (instance_nearest(x, y, sell)) { 
      visible = true; 
   }
}
else {
   with (instance_nearest(x, y, sell)) { 
      visible = false; 
   }
}

OR, instead of with(instance_nearest() ) blah blah, you could probably get away with setting the nearest sell to a variable, like sell_nearest or something and just setting sell_nearest.visible = true/false;.

Okay, now, since I can tell you're struggling with this a bit, I'm going to go into a bit of detail with this.

First off, if you're going to use city and ship like that, YOU ABSOLUTELY HAVE TO MAKE SURE THAT SHIP AND CITY ARE VARIABLES THAT YOU'VE SET TO STORE THE ID OF THE NEAREST SHIP/CITY, DEPENDING, LIKE I DESCRIBED ABOVE. OTHERWISE, IF SHIP OR CITY ARE OBJECTS, THEN IT WILL TAKE THE FIRST CITY/SHIP IN ORDER FROM 0 to TOTAL_AMOUNT_OF_THAT_OBJECT, WHICH INSTANCE_FIND WOULD USUALLY LOOK FOR.

I would HIGHLY recommend naming all of your objects obj_<name here> and your sprites spr_<name here> to avoid any kind of conflicts between object/sprite/variable names that would normally break the game.

Secondly, if that's in the city's step event, then you don't need to make it city.x or city.y. When referring to an object's x/y within its own events, you just type x or y. In fact, if you have it set up like you have it currently, and your city/ship variables are set correctly, it'll look for the nearest city to IT, meaning the next city over, rather than THAT city.

Third, with() is a type of statement, like if() or for() that allows the instance in question to be controlled from another instance as if it were within that instance. Almost like coding outside of an instance. The only caveat is that while within the brackets describing the with() loop, you CANNOT access the variables or anything else of the object that you're currently coding in, meaning you have to be really careful of how you're using your with() statements. As Jack stated below, you can use the err...built-in(?) variable other to access the variables of your current object again.

Fourth, to get the instance nearest to the instance you're coding in, you absolutely have to set it up instance_nearest(x, y, object). If you put in a number for either of those instead of x and y, it will return the nearest instance to THAT POSITION, meaning that unless the cities start moving, it will always return the same city which may even be entirely across the map from the ship.

Fifth, I'd like to warn you that if you don't set up the sell objects to ignore input unless they're visible, you'll have issues with every one of the sell objects being controlled all of the time.

I think I've covered everything. If you have more questions, just ask. :)

Edited by johnki, 12 May 2012 - 10:56 PM.

  • 0

#9 Jack Indie Box

Jack Indie Box

    GMC Member

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

Posted 12 May 2012 - 03:20 PM

you CANNOT access the variables or anything else of the object that you're currently coding in, meaning you have to be really careful of how you're using your with() statements.

not true:

if your in obj_city at the moment and use:
with(obj_ship)
{
x=5;//this sets ship's x to 5
other.x=5;//this however will set obj_city's x to 5 or whateva object your coding in
}
  • 0

#10 johnki

johnki

    GMC Member

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

Posted 12 May 2012 - 03:22 PM


you CANNOT access the variables or anything else of the object that you're currently coding in, meaning you have to be really careful of how you're using your with() statements.

not true:

if your in obj_city at the moment and use:
with(obj_ship)
{
x=5;//this sets ship's x to 5
other.x=5;//this however will set obj_city's x to 5 or whateva object your coding in
}

Ah, you're right. Thanks for catching that. :)
  • 0

#11 peco

peco

    GMC Member

  • New Member
  • 28 posts
  • Version:GM8

Posted 12 May 2012 - 10:47 PM

Wow! I'm blown away by you guys's willingness to help!
I'll experiment with this and let you know how it goes.

Thanks again.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users