0
\$\begingroup\$

I'm working on a platformer game using Gamemaker Studio 2. , and currently I'm dealing with implementing platforms:

(Platforms are able to pass through from the bottom, but remains solid when landing on)

It's all working good and well for a single platform, but when I add multiple, then only the lowest placed platforms will work properly. I'm already aware that the problem lies on the second if-statement in the code below.

if (place_meeting(x, y+mySpeed[1], o_platform) &&       //If the player collides with a platform
    mySpeed[1] > 0  )                                   //if the player is falling
{
    if ((y + 29) < o_platform.y)                        //if the player's feets are located higher than a platform
    {
        while !place_meeting(x, y+sign(mySpeed[1]), o_platform)
        {
            y += sign(mySpeed[1]);
        }
        mySpeed[@ 1] = 0;
    }
}

The problem is likely because it's reading information from any platform. but not the higher platform I'm colliding with. So I should need a check to make sure I'm talking about the same object I'm colliding with.

If it was a build-in collision event, I could use the other statement to get the right instance, but currently I'm using place_meeting in a step method to collide, and other would be too broad to use properly. So how would I be able to archieve this?

\$\endgroup\$
0
\$\begingroup\$

Instance_place did the trick.
If you want to use the object ID outside the collision event, then you can use Instance_Place instead of Place_Meeting.

var myObject = instance_place(x, y, object)
if (myObject != noone)
{
    //...you can use myObject as the object it's colliding with.
}
\$\endgroup\$
  • \$\begingroup\$ I have some doubt this solution works best if your character is colliding with many platforms at time. \$\endgroup\$ – liggiorgio May 11 '18 at 7:17
  • \$\begingroup\$ Could be, but I don't think that can happen in my case. I've an instance solid that I place over the layer of tiles. so there will only be one platform at a time. But it's good to take a notice of it in case it may go wrong. If you've a different solution, then let me know. \$\endgroup\$ – Steven May 11 '18 at 7:42
  • \$\begingroup\$ In case I need to come back to this for multiple collisions at once, I think this one can help: gamedev.stackexchange.com/questions/122130/… \$\endgroup\$ – Steven May 11 '18 at 9:43
-1
\$\begingroup\$
  • Have an array objects (or a dict of )
  • Give your objects a ulong ID field
  • Whenever you create an object, assign it an ID incrementally and then index it into the array with that id

Now whenever you collide with an object you just called:

var myCollider = arrayOfObjects[object.ID]

NOTE: It'd be better to do this with a Dictionary if you're in C#, but since I don't know what language you're in I gave you a starting point of using an array. An array in this implementation may well waste a lot of memory if you have lots of destructable objects.


The other way to do this is usually done by a Delegate (or a callback depending on your language.) I would imagine if you're in game-maker that you can probably find something out there on this; but the above will work either way.

\$\endgroup\$
  • \$\begingroup\$ I'm currently using Gamemaker Studio 2, who uses Game Maker Language. It's close to C#, but not the same. Sorry that I didn't mention that before. So far I've already tried 'memorising' the object ID by making it a variable before checking the if-statements. but that didn't worked out. Though I'm not sure if that works the same way as using arrays. I'll try and see if your other options might work. \$\endgroup\$ – Steven May 8 '18 at 6:30
  • \$\begingroup\$ This question was tagged with game-maker at the time of answering. So the engine was already known when answering. Do proper research before answering. \$\endgroup\$ – The Great Duck Dec 28 '18 at 7:20

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.