It is better to loop an event with a delay or use the event tick?



Hello everybody!

I am currently working on a train system, and I have a question.
Since I have to loop the event that keeps the train moving, I have currently found 2 approaches, but non of them seem correct to me.
The first one consists in using the even tick, and the second one of using a delay to keep looping the event.

Both of them work, but I don’t really know which one is the most efficient.
Any suggestions?
Also, in case you know alternative ways to do this I am open to changing my approach, thanks in advance! :slight_smile:

1 Like

Delay.


You did not ask but there’s a 3rd option → consider a Timer:

It’s like a Delay but you get extra features.

the engine is already set up to do Ticks() both have advantages and drawbacks

benefits
Tick: you get a lockstep to the “frame” where you know where in the frame step your code will execute, so everything in the Pre-Physics-Tick-Group will finish before the Physics step is triggered.
Delay: your code will fire with a known minimal interval regardless of the Frame step and mostly consistently, so physics might have just finished, but the timer hit so your code is getting called.

drawbacks
Tick: to many things happening in the same Tick-Group will result in hitching (players will notice frame delays especially if it is inconsistent
Delay: similar to the benefit you don’t have the known location in the frame-step for where your code is running, so if your position update is run after the physics step now your actor/pawn/character might be inside a wall or another character and physics will need to adjust on the next step.

middling things
Tick: many people will tell the evils of Tick, but this is usually the same people that don’t realize you can put a time step on Tick to get very close to those Delay-Loop timers.
you can move things into different steps, so you can do some position logic on the AI to avoid it ever going into the wall to begin with so you can put it after the physics.
Delay: each one needs to be set up and managed independently, turning them on/off, and even modifying the time step.

2 Likes

I thought your delay was 0.8. You run it 0.08 - as if it was Tick. Then Tick is better, simpler but you need to account for frame-time.

To be honest. If you need something moving properly, use a timeline. Especially if something is time sensitive and needs to last X seconds.

keeps the train moving

Perhaps you could elaborate a bit on how the movement is supposed to work. Knowing trains and their fondness of tracks, I assume it’s moving along a spline? A Timeline could work really well here. A lot depends on the method chosen for the movement, ofc.


Thanks guys!!
I am actually using a timeline, here is the full code that makes my train move.

Do you think using a timer would be better?

1 Like

And yes, it is moving along a spline!

1 Like

You’re using the timeline to increase the speed, but not move the train, right? You could have another timeline that is looping:

And use the Update to move the train, it triggers every frame.


Tick would be fine but I’d avoid combining these:

image


That’s correct. The movement happens due to this distance check based on the spline’s lenght. Then the “Move” function will be fired and the distance updated.

Thanks a lot @Everynone I’ll try the looping timeline, if not working I’ll just use the tick. Thanks! :smiley:

1 Like

An example, just in case:

This moves many separate objects simultaneously. Could be handy if the train has cars.


But it should work fine with Tick if that’s what you’ve got.

At the moment it has only one cart because I was thinking about a system similar to mine’s monorail, but implementing more carts in a procedural system might be actually pretty cool.
Thanks!

I’ll drop a couple of related links, explore at your leisure:

  • ticking spline train with controllable speed:
  • takes advantage of spline slope & physics:

Good luck!

Thanks a lot @Everynone this is extremely helpful!

Just a quick update on this. I wasn’t really able to use the looping timeline. Probably I just didn’t understand what you meant here.

However, I marked this as the solution since the tick works fine and I am going to use it :slight_smile:

1 Like

These two nodes :point_up: are enough to have an actor moving. To follow a spline, you’d:

Again, this is completely optional and just one of the methods. Choose the best working tool for job. Seems you got that going already.

Thanks man! The cool thing about game design is that you can always learn from more experienced people, just like I am doing right now :slight_smile:

I think I am still going to use the tick, but you triggered my curiosity now!

So, this is the function that I am using to move the cart along the spline:

What I do not understand is where I am supposed to place that timeline.
Here is the tick which handles the movement:


If I change this to a normal event, should I just place that after the second branch?

Start it with Begin Play, or a Custom Event or do not connect it to anything at all since it can be auto-started:

If Speed is null, we don’t move. Essentially, the TL acts here as a flexible source of Updates.


A timeline is a component that can be controlled like so:

image


Again, the crux is in the details - really depends on what the big picture plans are for the train. It could work.

This is really amazing man!! Thank you so much!!!

I’ll give it a try as soon as I can, just a last question since you are clearly more expert than me: performance-wise, do you think this approach is better than the tick?

Probably unmeasurable and utterly negligible unless you have a 1000+ trains or are making a legacy-compatible mobile product. In which case you’d probably opt for a time manager actor running timers and dispatching updates at specific intervals to a bunch of non-ticking actors who can interpret that.

And you’d not be using blueprints for heavy lifting.


This:

Tick is demonised because people put stuff in there that does not belong. If you want to see an update every frame… that’s what tick is for.


do you think this approach is better than the tick

  • spawn 1000 actors with a timeline updating script
  • spawn 1000 actors ticking the same script instead

See how it goes. :person_shrugging:

If you’re making 1 train, it does not matter. Choose what is more convenient to work with / understand.

One way or another, it’s gonna be like this ;p

Crystal clear!

Thanks a lot man!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.