Programming technique for moving left and right

Hi

What is the best way to make an object move a little to the left and then turn around and move a little to the right?
I mean the programming technique. Is there any ability for gdevelop event system?

Thanx

Depends on a few things…

  • Is this a top-down game where the same object might also move up and down?
  • Will the object move back and forth constantly in a loop, or only in response to an event?
  • Do you have separate animations for left & right movement, or is simply flipping the sprite horizontally suitable?
1 Like

1.No
2. Yes
3.No

I was reading this tutorial when I came across this method:

Step 3: Change the enemy’s direction

To make the enemy change direction, add two invisible objects to the scene – one called “Left” and one called “Right” – and place them on other side of the enemy.

Then, when the enemy collides with one of the objects, you can flip them around and move them in the other direction. This creates the illusion of the enemy following a set route.

This step focuses on creating the “Left” object. When the enemy collides with this object, they’ll change direction from left to right. (The step that follows focuses on the “Right” object.)
06-07

I think this method is extraordinary and useless. Is there any other method?

It depends why you find it useless… Is it because you have to add two extra objects/instances for each enemy?

If so, you could do something similar without the two collision objects… Place your character at the leftmost position and have it facing right. Then make an “At the beginning of the scene” event to start it moving. Create another event with a condition that checks the object’s X position, and triggers when it’s greater than a set amount (i.e. the distance you want the character to move). Add an action to flip the sprite and set it moving to the left. In another event you do a similar thing: check if the X position is less that the starting value and, if so, unflip the sprite and set it moving to the right again. That’s just off the top of my head, so it may need tweaking, but should give you an idea of what’s possible.

If that’s also unsuitable, I guess we’d need to know more about what exactly you’re wanting and what the requirements are…

1 Like

There is also an extension. Maybe this is more helpful for your case

Actually it is quite common also for other game engines to have colliders of some sort in a scene that trigger a specific action, so it is not extraordinary. It is also not useless since it works very well.

1 Like

Haha, which parts of the questions are you saying yes and no to?

Let me rephrase them to non OR questions:
2. Will the object move back and forth constantly in a loop?
3. Is simply flipping the sprite horizontally suitable?

What is it you don’t like? The movement or the changing direction?

Another option for back and forth movement is to give the object the sine behaviour. But you would still need to flip the animation.

Just to add to the tally. You can also do this using tweens - a positional change tween for the first direction, followed by a positional tween in the other direction. If you use an in-out easing, it can look like the object speeds up at the start, and slows down at the end of the straight.

Yes, you are right. Should I use this algorithm:

//define variables

direction= right
position.x=0

//moving the character
character.x.force=100

//check the x position
if the character x = 20
{
//changing the value of the variable
direction= left
}
//check the variable
if direction= left
{
character.x.force=100
character.flip
if the character x = -20
direction=right
}

This algorithm is far from ideal, prone to errors and rigid in its usage compared with the ‘useless’ method you dismissed earlier.

Why is this alogritnm not very good?

  1. You are using forces, so positions will be a float, and never be an integer. So character.x will never be an obsolute 20 or -20.

  2. The limits are hardcoded. If you want to change them, they’ll need to be searched for in the events and replaced (and if they appear in multiple places you need to remember to change ALL of them).

  3. It can only every apply to one character object. Unless you have a way of identifying each object, in which case you need to have a set of events for each object.

  4. You can bypass points 2 & 3 if you use a parameterised algorithm. But then you need to make sure you have all the parameters correct, and they can be prone to errors or messy to write & read.

In short, the method you have knocked is actually an ideal way of left & right movement control in GDevelop. It may not fit with how you might be used to programming things, but bear in mind GDevelop requires a change inn mindset and programming approach.

I whipped up a quick demo of how @SaeedP’s algorithm could be GDevelop-ised (addressing most/all of @MrMen’s comments):

Peek 2022-08-22 09-28

As you can see, you have have different distances for each character instance, and different speeds too. Here’s how it’s done:

The character object has two animation frames to simulate walking:

image

It also has the following variables with default values:

  • start_pos: 0
  • distance: 0
  • speed: 50

When I placed each character onto the scene, I set custom values as so:

Top character:

  • distance: 100

Bottom character:

  • speed: 150
  • distance: 600

And here’s the event sheet:

Hope that helps.

For sure it can be done; that wasn’t what I was getting at - you could store each movement position in an array and use that to control movement. But just because you can, is it the best approach?

With the parameterised method, each character has to have the parameters correctly set, and the character only starts in the centre of the movement path, unless you add more variables. And that just starts adding complexity.

Whereas with the turn-around objects all the dev need to do is place the character and 2 blocks (and maybe set the initial direction, though that can be mitigated by placing the character on a turn-around block). That’s it. It’s extremely quick and simple to add new characters, no typos and the dev can visually see the limits of the character’s movement. Yet the OP dismisses this as extraordinary.

Do you mean when you drag the object on the scene you updated the values of the variables or you updated them in the event sheet?

When I dragged them onto the scene, I updated the variables in the Instance Properties Panel: https://wiki.gdevelop.io/gdevelop5/interface/scene-editor#instance_properties_panel

Thanks for all of your help.