Are you struggling with buggy enemy AI in GameMaker Studio 2? This step-by-step guide will help you understand a common problem and implement an effective solution using parent and child objects. Learn how to centralize your code and streamline your game development process.
Video Link: http://www.youtube.com/watch?v=ILiktxXXAxA
Understanding Parent and Child Objects
A key principle in object-oriented programming is inheritance, and GameMaker Studio uses a powerful system of parent and child objects. A parent object acts as a blueprint, holding all the common code, variables, and events that multiple other objects might need. The child objects inherit all the properties of their parent. This means any code written in a parent object automatically applies to all its children. This is an extremely efficient way to manage a project, especially when you have multiple enemies or other similar objects that share a lot of the same behaviors.
The issue addressed in the video is that the enemy AI code was initially placed in a child object, object_enemy_one. While this works for a single enemy, it becomes a problem when you want to create object_enemy_two, object_enemy_three, and so on. To avoid writing the same code over and over again, the solution is to move all the core AI functionality to a parent object, like object_enemy_parent.
Moving Your Code
To fix this issue, you will need to transfer your code from the child object to the parent object. This process is straightforward and can be done in a few simple steps.
First, open your child object and right-click on the event you want to move, such as the Create event or the Step event. A context menu will appear. From this menu, select “Copy Event” [01:10]. This will copy all the code contained within that event to your clipboard.
Next, navigate to your parent object. Here, you will add a new event of the same type (e.g., a Create event). Once the new event is open, you can paste the copied code into it [01:21]. This action centralizes the code, making it accessible to all objects that inherit from this parent.
Finally, return to the child object and delete the original event [01:37]. You will notice that once you delete the event, a small icon appears next to it. This icon signifies that the child object is now inheriting that event from its parent, which is exactly the result you want [01:53].
Fixing Collision Bugs
Bugs are a natural part of the game development process, and this video also highlights a common collision error. In this specific case, the game had a bug where the player’s collision code was incorrectly checking for a collision with object_player instead of the enemy. This prevented the player and enemy from interacting correctly [03:36].
The solution is to simply change the collision check to object_enemy_parent [03:47]. By doing this, you’re telling the game to check for a collision with any object that is a child of object_enemy_parent. This is another example of how using a parent object can streamline your code and make it much more efficient and less prone to errors.
A crucial lesson from this is the importance of frequent play testing. By testing your game early and often, you can catch these types of bugs before they become major problems [03:16]. It’s much easier to fix a small mistake than to hunt for it after you’ve built many layers of code on top of it. Following these steps will help you create a more robust and scalable AI system in your GameMaker Studio projects.
A Guide to Improving and Fixing Enemy AI in GameMaker Studio 2 was originally found on Access 2 Learn