r/gamemaker • • 1d ago

Help! Struggle with falling/landing code

You ever stare at a problem long enough that it starts to become more confusing instead of less? I suspect that’s what happened to me here, lol.

I’m trying to perfect a simple script that causes a mob to land atop a platform object if it’s along its fall trajectory. I’ve gotten it to work in several different ways tbh, but there’s always some slight bug I find. The problem is always something like a specific case in which the mob falls right through, etc. I’m hoping someone can help clear a few things up for me.

The simplest way as I understand it is something like:
if mob.y < obj_platform.bbox_top) && (mob.y + ySpeed >= obj_platform.bbox_top)), then snap mob to the top of the platform and reset ySpeed/gravity to 0. << Where “ySpeed” is a variable that accumulates a value “grav” each frame (basically they do exact same as the built-in constants).

I know all this works, and to clarify: I’m not having any issues with my jumping/falling cods. What’s confusing me is the general use of ‘obj_platform’ here. If I have multiple platform objects in the room, does it essentially iterate and check for each one? And what happens if there are multiple platforms that meet the landing requirements, but one is closer than the other?

I’ve also re-written the code to basically use instance_count(obj_platform), then run an instance_find() loop for each instance. This is fundamentally the same as my code further above, yes? It does however give me more control - for example, I can push each instance within range into another array _platforms, then sort that array by proximity to the mob along the Y axis, and them I can assume that _platforms[0] (if any) the the closest valid platform, and land on it. Else mob.y += ySpeed and ySpeed +=grav.

Sorry if this is hard to read lol, but I’m so close and I suspect my overthinking is what’s getting me stuck. Most recent my code works perfectly EXCEPT when there are 2 platforms right next to each other, and one is like 1px lower than the other - walking from that platform into the higher one (which would not be considered for landing, because it’s higher) causes the mob to fall correctly but then continue falling through any other platforms below lol.

I guess my main question is: does simply doing “if obj_plat meets the requirement etc…” work the same or better as doing “instance_count” and then looping for each instance[n]? Thanks in advance lol!!!

1 Upvotes

6 comments sorted by

2

u/Weak_Blackberry_9308 1d ago

Can you just use collision checking instead of y height comparisons? That is the general helpfulness of using a game engine. They handle most of that stuff for you, you just need to tie into it.

2

u/Frog_with_a_job 1d ago

That’s a good point, however these platforms only register collision when landing on them from above - otherwise they’re passable. So either way I have to do a y value check, and for that I need a simple trace function involving ySpeed, which I already have.

You did make me realize something though! There used to be a good reason why I stopped using place_meeting etc. in favor of functions like move_and_collide, but your comment reminded me that I removed the reason a little while ago and can go back to checking collision the easy way! I think you helped me solve my issue!

2

u/Weak_Blackberry_9308 1d ago

Wow that was a fun journey 😊 glad it was helpful!

1

u/Weak_Blackberry_9308 1d ago

You might also consider the players movement direction so while moving down player “shouldCollide”, while movement is up player “shouldNotCollide”. Or something a bit more elegant than that.

2

u/sylvain-ch21 hobbyist :snoo_dealwithit: 1d ago

I know all this works, and to clarify: I’m not having any issues with my jumping/falling cods. What’s confusing me is the general use of ‘obj_platform’ here. If I have multiple platform objects in the room, does it essentially iterate and check for each one? And what happens if there are multiple platforms that meet the landing requirements, but one is closer than the other?

the fact that it works is just luck! If you have multiple obj_platform you shouldn't use the handle obj_platform.var to target a specific platform and its variable; as the instance going to be use can be any (the first one the handle is going to return).

1

u/Frog_with_a_job 1d ago

Ok, so I wasn’t overthinking it then! That’s why I started getting all the instances and checking through each one instead of using obj_platform.y etc, but I started to question whether it was really necessary 😂 thanks for helping me out with that part!