Tick calling code in all actors. How can i avoid it?

Hi guys!! (Please suggest me a title :sweat_smile:)

I have a component for most of my actors, some of them are controlled by players and some by AI. In all of them i have a condition to do a specific behaviour if it is controlled locally.

I know that it’s not really expensive the operation. But it’s a bit ugly in code and if I have thousands of pawns that can be costly. Is there any way to inject the specific behaviour only to the controlled actor?

void UCombatComponent::TickComponent(const float DeltaTime, const ELevelTick TickType,
									 FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	if (Character && Character->IsLocallyControlled())
	{
		// Do something only to local controlled player
	}
	
	// Do something in all actors
}

Thanks for your attention :wink:

I don’t see anything wrong with a simple if in the tick.

You can store that condition in a bool variable on begin play like:

controlled = Character && Character->IsLocallyControlled();

then in the ticker you just do

if(controlled)

that way you save to call IsLocallyControlled() all the time in all the actors.

also think if your behavior needs to be called every tick. If not you can just avoid ticker and use a function with a timer for the specific behavior setting the timer function in begin play too.

also you can make a ‘special behavior’ actor and then if you are locally controller you can just create a child actor with that ‘special beehavior’ actor and what it does is ‘some special vehavior’ in its parent/owner

2 Likes

Thanks for your answer!!

Right now I am learning Unreal and so far I see that it is really declarative. Animations and components are programmed defensively and declaratively. I am following some courses to develop a multiplayer game. In the game there can be hundreds of pawns at the same time and the player can take control of them.

One of the problems is the large amount of actors/components/AnimBP in the scene in addition to the replicated actors with the same behaviors/components. I need some specific calculations just for the controlled pawn, For example CombatComponent/AnimBP Throw some linecast to make the player behavior more realistic.

I would like to avoid Tick for some components, but i need it for the controlled pawn.

Sounds interesting the idea of creating an inherited behavior, but I’m not sure how to save the variables, current state, anims, etc. And the system to destroy them when the player changes the pawn. I will keep looking and if I don’t find a solution given by Unreal I will try to implement something like this.

Turn off Tick on anyone you don’t need to tick until you do need them to?

Hey, Thank your for your contribution!

That’s not exactly what I’m looking for. What I’m looking for is how to avoid the comparison
if (Character && Character->IsLocallyControlled())
To make the code more efficient and clear.

To do this I am thinking of overwriting PossessedBy and UnPossessed to inject the desired behavior. But I don’t know how to do it efficiently and clearly.

That’s the doubt: How to do it by following SOLID and efficiently?

well if you’re possessing each character as you give them orders and unpossessing them when you don’t want them to do things, then yes, absolutely, turn on the relevant component’s ticks when possessed, and turn it off when not possessed.

when you follow arbitrary patterns, you are not likely to find the most efficient method for doing complex things. Do what works. Leave comments for your future self or others.

A page of comments explaining the specific reason for why you did something can replace dozens of lines of code of horrifying spaghetti that makes absolutely no sense whatsoever when you’re not in the same frame of mind as when you wrote it.