#how to check values of same objects in different places?

5 messages · Page 1 of 1 (latest)

high imp
#

hi so i have ran into a problem when trying to make latching points for grappling in my game (green anchors)

basically if press a grapple button you shoot a hook based on where you hold WASD, but if you're within the gray circle you will automatically shoot towards the anchor. since these anchors would be littered everywhere i tried added another one to see how it would behave and if you were within the gray circle on the right you'll still shoot towards the original one on the left

in my code i basically have it if you are in the gray circle and you press grapple button you point direction from player origin to obj anchor and shoot, since they will have different x and y values how do i make it so that it gets values from the one i shoot or closest or something, one thing that's in my mind that could possibly solve this is using 'other.' but i have no clue how

stray prism
#

there's a lot of ways you could do this but using instance_nearest(x,y, object) to get the ID of a specific grapple anchor is probably your best bet? this should even allow for overlapping gray circles.

instance_nearest() will look for the object of the type you give it that's closest to the position that you give it, and ends up equaling the ID of that object, which you can store and use!

not sure what your existing setup is but it would look something like

//if the grapple button was pressed and you're in a grey circle:
var _temp_anchor = instance_nearest(x,y, obj_anchor);
// shoot using _temp_anchor.x, _temp_anchor.y instead of obj_anchor.x, obj_anchor.y
#

When you refer to an object by name only, rather than by ID, it works well until there's more than one of them, as you've noticed. There can be some weird behaviour here, sometimes it's useful, sometimes it isn't. but in this case it should only ever get the values from the first instance of that object, aka the one you placed down first in the room.

high imp
#

hi thanks for telling me about this function as it's something that i've been looking for, i looked it up in there's an example and it's using that 'var inst' and 'inst.x' and 'inst.y', what's the difference between using 'var inst' and using '_temp_obj_anchor.x'? does the value of _temp update everytime there's a closer anchor?

also what if i want to get the value of the closest anchor through the projectile that i shoot? that way i don't hook to the closest one to my player object and i could calculate what happens next by switching states in the player object

stray prism
#

the difference between "var inst" or "var _temp_anchor" is just the name! you can name it whatever you want, was just trying to give it a name that gave a good idea of what it's used for. you could call it "var dinosaur" and it would work exactly the same way.

If you know for sure you'll only ever have one hook projectile, you could just go back to referring to the object by name, no ID needed. then you could use instance_nearest(obj_projectile.x,obj_projectile.y, obj_anchor); (again replacing these with whatever your actual object names are)