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!
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.
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 Timelinecould work really well here. A lot depends on the method chosen for the movement, ofc.
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!
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 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.
If you’re making 1 train, it does not matter. Choose what is more convenient to work with / understand.