Legacy GM Return Colliding Object's id

Posho

Member
I'm testing a collision between two objects and one of the objects should return the id of the other but it's not working.

I've used instance_place(), instance_position(), collision_rectangle() and none are working. I know this is beginners' stuff and I've made this work in the past but it's not working now and I have no idea why. Help.

 
P

PandaPenguin

Guest
do both objects even collide?
and do both objects have a proper collision mask (usually from their sprite) ?

a bit more code would also be helpful to find your problem
 

Posho

Member
do both objects even collide?
and do both objects have a proper collision mask (usually from their sprite) ?
The "testing script" executes with a collision event, and I only get the error message when they collide. It saying that the variable holding the supposedly-read id doesn't exist.
Also both objects have completely rectangular collision masks.
 

Posho

Member
On collision event:
Code:
target = instance_place(x,y,par_item)
if target != 0
    if (target.owner != "none" && hit = false)
        {
        //do stuff
        }
Error message:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Eventobj_coin
for object obj_squog:

Variable <unknown_object>.<unknown variable>(100014, -2147483648) not set before reading it.
 at gml_Object_obj_squog_CollisionEvent_14_1 (line 3) -     if (target.owner != "none" && hit = false)
############################################################################################
 

Posho

Member
Seriously, help. It works with instance_nearest() but it's very possible that it won't work well in the future.
 
P

PandaPenguin

Guest
On collision event:
Code:
target = instance_place(x,y,par_item)
if target != 0
    if (target.owner != "none" && hit = false)
        {
        //do stuff
        }
Error message:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Eventobj_coin
for object obj_squog:

Variable <unknown_object>.<unknown variable>(100014, -2147483648) not set before reading it.
 at gml_Object_obj_squog_CollisionEvent_14_1 (line 3) -     if (target.owner != "none" && hit = false)
############################################################################################
your problem is the first check for target != 0
if instance_place has NO collision it returns noone and that has a value of -4 !

so of course in your next check you try to access owner on an object that doesn't exist, that's why you get <unknown_object>.<unknown variable>
 

Posho

Member
your problem is the first check for target != 0
if instance_place has NO collision it returns noone and that has a value of -4 !

so of course in your next check you try to access owner on an object that doesn't exist, that's why you get <unknown_object>.<unknown variable>
But how can that be if it only checks for instance_place() when there's a collision with that exact same object?


What can I do instead?
 
P

PandaPenguin

Guest
But how can that be if it only checks for instance_place() when there's a collision with that exact same object?


What can I do instead?
but that's what it is supposed to do! it gives you the instance ID if there IS a collision, and noone if there is NO collision

just change your line to
Code:
if target != noone
so your next code will only run if a collision happened

EDIT:
I just noticed you have all of that in a collision event, sry must have overlooked that detail
can't tell you why it triggers and then has noone as a result when checked again

EDIT2:
oh wait I can tell you why
the collision event is different from instance_place!
a collision event checks your "borders" for collision and instance_place checks a specific point for a collision
since you collided with par_item on your borders of course there is NO par_item at the x/y you check with instance_place

sry I'm not that familiar with D&D so I can't tell you how you get the collision object ID with this methode
 
Last edited by a moderator:

Posho

Member
but that's what it is supposed to do! it gives you the instance ID if there IS a collision, and noone if there is NO collision

just change your line to
Code:
if target != noone
so your next code will only run if a collision happened
That's what I did earlier and nothing happens.
Both instances stop moving when they collide (because one is a solid). There is a collision because otherwise the script wouldn't execute but the target variable always ends in noone/-4.
 
P

PandaPenguin

Guest
That's what I did earlier and nothing happens.
Both instances stop moving when they collide (because one is a solid). There is a collision because otherwise the script wouldn't execute but the target variable always ends in noone/-4.
see my edits in previous post
 

FrostyCat

Redemption Seeker
Here's the information you're missing this whole time: In a standard collision event (i.e. NOT a collision function check in the step event), the colliding instance is referred to using other. To get its actual instance ID, use other.id.

The Manual entry on other said:
The special keyword other has two different ways that it can be used to reference a specific instance: when used in a with function (explained here) or when used in a collision event, which is what this section is going to explain.

A collision event can only happen between two instances. You can have multiple collisions between multiple instances, but they are all resolved by GameMaker: Studio on a 1-on-1 basis, with the instance that has the collision event and the "other" instance that is involved. Imagine you have a player object, multiple enemy objects and multiple bullet objects that the enemy can fire at you. You can assign each enemy a single bullet instance but with a different damage variable randomly assigned to it when created, for example:

Code:
var nnn;
nnn = instance_create(x, y, obj_Bullet);
nnn.damage = 5 + irandom(5);
nnn.speed = 8;
nnn.direction = point_direction(x, y, obj_Player.x, obj_Player.y);
 
Top