Legacy GM instance_place question

DukeSoft

Member
Hi guys,

I'm trying to get my player object to play different footstep sounds based on what he's walking on. The ground exists out of objects (big, round, shapes) and they _can_ overlap.

Now the problem is that when I have 2 circles (1 grass, 1 stone), and they overlap, it seems like instance_place picks one at random.

My code;

Code:
var foundground = instance_place(x, y, obj_terrain);
    if (foundground != noone) {
        var footstepsound = asset_get_index(global.terrain[foundground.type, TERRAIN_FOODSTEPSOUND] + "_" + string(ceil(random(global.terrain[foundground.type, TERRAIN_FOODSTEPSOUND_COUNT]))));
    } else {
        var footstepsound = asset_get_index(global.terrain[global.default_terrain, TERRAIN_FOODSTEPSOUND] + "_" + string(ceil(random(global.terrain[global.default_terrain, TERRAIN_FOODSTEPSOUND_COUNT]))));
    }
It works fine when im walking on a single instance, or on none. But once I have stone, overlapping grass (the depth is lower for stone, its visible as well), GM still tends to tell me "grass" and returns that value.

Question is:

1. Can (if so, how) I get a list of all instances that i'm colliding with on that point?
2. Could i replace this code with something that takes depth into account? (e.g. checks for the highest collisions first?)

The other solution is:
create -> make list
Every step, clear list
every collision with obj_terrain, add instance to list
end step -> check list and pick highest obj_terrain (if any) and use that

But that's really lame, ugly, and inefficient.

So, that. Any idea's?

Oh, and if you know the difference between instance_place and instance_position, please let me know.
 

FrostyCat

Member
Whenever you hear yourself saying, "for all X that does/is Y", that's your cue to go "with X if Y".
Code:
var lastground = noone;
with (obj_terrain) {
  if (instance_place(x, y, other)) {
    lastground = id;
  }
}
 
Oh, and if you know the difference between instance_place and instance_position, please let me know.
This is answered within the first two lines of the description of both of those functions.
instance_place
With this function you can check a position for a collision with another instance or all instances of an object using the collision mask of the instance that runs the code for the check
instance_position
With this function you can check a position for a collision with another instance or all instances of an object. When you use this you are checking a single point in the room for an instance or an object.
 

TheouAegis

Member
It's not picking one at random. It's either picking the oldest instance at that position or the newest. I say "either" because I keep getting different results between versions and/or functions. One day I say "it picks the oldest" and then I test and it picked the newest.
 

DukeSoft

Member
It's not picking one at random. It's either picking the oldest instance at that position or the newest. I say "either" because I keep getting different results between versions and/or functions. One day I say "it picks the oldest" and then I test and it picked the newest.
Thats what I mean with at random haha :)

But I've fixed it in another (probably not much more inefficient) way;

Code:
///get_terrain_at_position(x, y)
var lowestFound = 10;
var foundground = noone
for (var i = 0; i < instance_number(obj_terrain); i += 1) {
    var fnd = instance_place(argument0,argument1,instance_find(obj_terrain,i));
    if (fnd != noone && fnd.depth < lowestFound) {
        foundground = fnd;
        lowestFound = fnd.depth;
    }
}
return foundground;
 
Top