Join Steven Moser for an in-depth discussion in this video Parent objects, part of Building a Physics-Based Platformer in GameMaker Studio Using GML.
- [Instructor] When using the physics engine, we can have three basic types of objects: static, dynamic, and kinematic. Dynamic objects are affected by the physics forces in the game. They move, and react, and act as any physical object would. We can also have static objects, where aren't affected by forces in the room. This is perfect for objects like floors, platforms, and walls. Kinematic objects are a bit of a mixture of the two. They're like static objects, in that they aren't affected by forces in the room, but we can also set them moving through code, if we want.
This is great for objects like moving platforms in a platformer game. A good practice is to create parent objects that all our static and dynamic objects will inherit from. This is a great way to allow objects to collide with one-another, without having to set up individual object collisions. I'll create a new group under objects, called parents, (typing) and create two objects in it. One will be our static parent, p_static, and the other will be our dynamic parent, p_dynamic.
(typing) I'll open up the static parent, and in it, we'll set up a collision event for both static and dynamic objects. I'll click on add event, collision, and in our parents folder, I'll choose the static parent object. This way we can detect collisions between static objects in our game. The only thing the physics engine in GameMaker requires to detect collisions, is that we put some sort of code in the collision event. So, I'll click on the control tab, and just drag a comment into the actions field.
And I'll just name it comment. The most important part, is to have the dynamic objects colliding with static objects, so I'll add another collision event, and this time, choose the dynamic parent. We'll add a comment in here as well, and we'll be all set. (typing) I'll close out the static parent, and do the same for the dynamic parent. (mouse clicking) I'll double-click to open up the object, (mouse clicking) add a collision event for our static parent, (mouse clicking) add a comment, (typing) (mouse clicking) and repeat for the dynamic object.
(mouse clicking) (typing) We have one more thing we wanna do here for our dynamic parent, and that's destroy any dynamic objects when they go outside of the room. If we have things like projectiles, or particles, we don't wanna have too many of them in-game, even if they go outside of the room. So, we'll destroy them in this event. I'll add an event, choose other, and select outside room. I'll drag a code block in here, and I'll add a triple-slash comment at the top that says destroy outside room, that's your GameMaker pro-tip, three slashes will show you the comments in the actions field when we close our code out.
And all we need to do is add in the function here: instance destroy. That will destroy the instance that calls this code when it's outside the room. I'll close out that code. We can see our comment here in the actions field, which is really nice, and I'll close out the object properties dialogue. With that, we have our static and dynamic parents set up, and we can create our first static object.
- Setting up basic game components
- Creating the player object
- Using Finite State Machines
- Making the player duck
- Creating physics fixtures in code
- Creating physics obstacles
- Box2D liquid physics
- Creating particles that behave like water
- Creating a floating blocks obstacle
- Creating a swinging chain obstacle and a rope bridge
- Adding the finishing touches