Hi!
I have different types of enemies and I want to move them to specific locations if the player is in collision with that location. See the mockup below.
Currently I achieved it by writing conditions and actions for each enemy type and location, e.g.:
Player is in collision with red_location → move Enemy1 to red_location
Player is in collision with blue_location → move Enemy2 to blue_location
Player is in collision with green_location → move Enemy3 to green_location
I was trying to figure out if I can move the relevant enemies to their respectful locations using variables in a single condition and action, but variables is still black magic to me.
Hi Rabbit,
Unfortunately variables are bread and butter of a game. We’ll be using them in a bit.
To place an enemy at one of the location boxes, you’d use an expression to change the enemy/ies positions at the X and Y positions of the object.
When you’re creating the event to change position of the enemy/ies, there should be this button beside the X and Y position inputs:
You can find the relevant expression there, much like selecting an action.
Here’s an example using an object variable. I tried to explain it in the simplest terms because I have no idea what you currently know. Plus, it can help other people with any amount of experience.
Conditions work like filters.
First, we check if the player is in collision with an object in the location group and pick that object. Let’s say it’s the red location object. Next, it checks if the color variable of the objects in the enemy group equals the color variable of the currently picked location object. Since, the current picked location’s color variable is red, it picks the enemy object(s) with the same variable value of red.
There are 2 instances of each location object. The collision event picks the instance that is in collision with the player so it only uses 1 location object at a time. If there were multiple enemies. It would pick all of the enemies of that color. You could add another condition to further filter the pick list. You could pick a random enemy using the group name or pick the closest enemy or pick all of the enemies which are less than or greater than a certain distance away. You can use so many different conditions to narrow it down to the object(s) that you want. You can pick only the ones not moving or use other variable to track other states.
The action tells it to move the picked object(s) in the enemyGroup to the picked object(s) in the location group. There will only be 1 enemy and 1 location picked with the current setup.
It uses
1 player object with the draggable behavior
3 enemy objects in a group named enemyGroup with the pathfinder behavior (default settings)
3 location objects in a group named locationGroup
The enemy and location objects all have a variable named color with the objects color. You can add the variable and then copy and paste it into the other objects and change the value to red, green or blue.
Thanks Reina, I may have exaggerated a bit
I understand some basics and I have used simple variables, but I struggle grasping more complex solutions. I have not found a decent tutorial yet that would explain how and when variables can be used.
Thanks Keith, that worked like a charm.
Thank you for submitting the game example, that helped a lot.
So basically, what I needed to do was to add the same variable (e.g. “Color”) to all enemy objects and location objects and add matching values. (e.g. enemy.red = location.red). Then, after putting the enemies and locations into groups, it could be used as a condition, as you showed.