Code structure for synchronized events in time

Hi guys, I want to ask you about a beginer dilema that I have. It’s about the code structure.

I have a function that is triggered when the player pawn is hit. In this function I execute several things.

function Hit()
{
	thing#1
	thing#2
	thing#3
	thing#4
	thing#5
}

With the function implemented this way, all the 5 things will be executed at the same time.

Let’s say that the thing#3 is an interpolation operation that will take 1 second to finish and I want the thigs #4 and #5 to be executed once finished the thing#3.

How do you recommend to structure the code to achieve this?

I made somthing like this, it’s working but I’m not sure it’s the best way in terms of good practices:

function Hit1()
{
	thing#1
	thing#2
	thing#3 with a callback to Hit2()
}
function Hit2()
{
	thing#4
	thing#5
}

I don’t like it because I’m splitting the Hit logic in various functions just to handle the timings and delays of the events that occur inside. I made the recursive version but still feel that’s not right.

function Hit(iteration)
{
	switch(iteration)
	{
		case 1:
			thing#1
			thing#2
			thing#3 with a callback to Hit(2)
			break;
		case 2
			thing#4
			thing#5
			break;
	}
}

Any advice?

(Footnote, for context purposes, I’m trying to implement a kind of MortalKombat fatality mechanic, this is why the Hit event handling is so complex)

A proper callback sounds like the right approach. Much better than an approximate timer.
If you want to keep everything in the same function, you can use a Lambda instead of a separate function :

void Hit()
{
    Thing1();
    Thing2();
    Thing3(FTimerDelegate::CreateWeakLambda(this, [&]() {
        Thing4();
        Thing5();
    }));
}

void Thing3(const FTimerDelegate& Callback)
{
    //...
    Callback.Execute();
}

Nice, I’ll try this way, thanks!

Usefull links related to this

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