[Solved] Move different objects to different locations based on player's position using variables

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.

test

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.

Any suggestions?

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:
image

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.

You can try it by dragging the player with the face to any of the locations with an X. I’m guessing you’re using the pathfinder behavior. That’s what it uses to move the enemy.
https://games.gdevelop-app.com/game-531dfa4e-26f4-4bb6-9aa1-14315d65c952/index.html

Source: (click the green [code] and [download zip] unzip and open the JSON file with Gdevelop.
https://github.com/doug13579/gdevelop-pair-enemy-to-target-location

These are all of the events.

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

image

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.

2 Likes

Thanks Reina, I may have exaggerated a bit :sweat_smile:
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.

1 Like

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.

1 Like