0
\$\begingroup\$

Let's say I have two objects. One is "obj_wall" and the other is "obj_player". For the sake of argument, let us assume that I cannot just add a collision event to obj_wall (I truly cannot due to limitations embedded within the nature of the project itself).

Is there a function or block that allows one to write a collision event within obj_wall's step event? I know there are various collision detection functions, but what I need is one that acts identically to the collision event and allows me to access the "other" property. If this is not possible, are there any suitable alternatives (such as a function that detects collision and returns the instance_id of an object colliding the wall)?

My primary issue here is that I want to be able to use collision in one instance of an object without having to have the collision event added. I'd prefer it to be as close as possible to the collision event so as to avoid unnecessary extra checks.

\$\endgroup\$
1
\$\begingroup\$

You can use the instance_place() function:

instance_place(x,y,obj) Returns the id of the instance of type obj met when the current instance is placed at position (x,y). obj can be an object or the keyword all. If it does not exist, the special object noone is returned.

You can create a structure like this:

// Check collision
var oth = instance_place(x,y,my_obj);
if (oth!=noone)
{
    // compute collision
}

Here, the variable my_obj is an instance id or an object index you want to check and collision with; and the local variable oth is the instance id of the instance the collision occurred with. If oth is equal to noone, no collision was detected.

\$\endgroup\$
  • \$\begingroup\$ This is perfect! I suppose it has a slight flaw when multiple objects are colliding at once, but it should serve fine for this particular situation (since there shouldn't be more than one player). Thank you for the great answer! \$\endgroup\$ – The Great Duck Apr 5 '17 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.