Casting VS Interface performance / general TICKING performance questions

I am making a HUD right now and a lot of other things :smiley:
BUT I am worried about the Tick’s performance cost in BP

What do I mean:

Currently I can either cast or interface to get the player’s health. I would like to know which one is better for performance.

Can your tick function(say in pawn) be so heavy that it causes your framerate to tank.

What if you have a lot of actors that do simple tick functions can this cause your framerate to tank.

I found this :

and I think that the BPtick is “in” the TG_PreAsyncGroup but I need confirmation.

So share your knowledge :slight_smile: Thanks!

I wouldn’t worry about it that much, but if you do care about this, then you should do neither, not in Tick or any similar manner anyway.

Try to always use cached references instead of constantly finding/casting the object you wanna access. For instance in most of my classes, I have an object called MyPC, which stores a reference to my player controller. In Hud I have an additional MyPlayer, and also in my player BP and any other object that I need to access hud constantly, I have a MyHud reference. You can do this for any number of objects you frequently access from a class, but these are the most used ones usually. (If you wanna keep it super small, you can always only have one cached reference, MyPC, and access anything else through that, but I wouldn’t suggest it for things like Hud that is likely to be accessed too frequently)

The thing to remember though, is you should carefully manage filling/updating those references. For instance whenever the player dies or maybe you switch between pawns for the player, IF you have a MyPlayer reference in a class, you have to update it with the new pawn otherwise it’ll be invalid.

As for filling it the first time, you can most of the time use BeginPlay event, but not always. You can also use ExposeToSpawn for objects that you spawn in realtime, like special projectiles.

As a bonus also, using cached references instead of constantly casting or using an interface is that your graph becomes less crowded.

Ok But I don’t know how to cache a reference to my Pawn. Do you mean creating a Var lets say in HUD that has a “Mypawn” variable type.
I tried this and it does not display the pawns accurate health (it displays 0).
Also how can I update it?

Thanks a ton!

In addiction to Farshad12 say, you can also learn about events and interfaces.

The hud is a perfect example, why you need ask to the player each frame what is your health, is crazy. Think in some friend asking to you each 16 ms if you are ok.
Imo is better wait to the player receive damage and send a menssage (using interface) to the hud.

Then you have only created the variable, like I said you need to fill it with a reference, in other words assign some object to it, with the Set node. You can get the object you want to assign to this variable by any of the ways you already know, casting, direct targetting, an interface, etc., bu tthe point is that will only happen once (plus anytime you need to update it), and not each frame or even 10 seconds. In most cases the assignment would happen only once in each level so it’s much faster and again, a bit tidier graph.

Getting specific variables as erWilly mentioned is exactly the same, you also want to cache those individually IF you’re accessing them frequently. In the same game I used as example before, I have the health, and pill count cached in the Hud class, because they need to be accessed every frame, and am updating them by using an event per variable.

I think in general, regardless of HUD or other BP, use Tick only when necessary is important.
Like others said, use event and interface and cache variable is also good to do, as any call has its overhead, and if it’s done poorly, your performance suffer.
For example, movement, do you really need to update velocity every frame when there are no input to move it?
PlayerController is already polled at every tick, so if you use the input events, you can already move or update velocity or force that way.
I even check if the result is zero or within threshold(no gamepad/keyboard/mouse movement), skip set player transform and other interpolation nodes all together.
(Note, it doesn’t count if your character has some sort of idle movement/animation that changes camera/pawn.)

For almost anything you can think of, there is always a way to drive it by events, except physics related.
Or, maybe something like a homing missile that needs to track player location per tick.(which btw, can also be driven by custom events by player, but it doesn’t make sense doing it that way.)

My rule of thumb would be to keep every state change driven by events, make BP as self contained as possible( in missile case, save a reference locked player is better than player send custom events to all missiles.),
avoid polling as much as you can(ie getAllActorsByClass just to cast one of them), and if necessary, makes it a one off event.

I totally agree that event based design is going to be more efficient than polling, and is a good way to design! I might still use it for something like a HUD polling the Pawn for its health, just to keep things simple, and the HUD code has to execute every frame anyway to do its drawing.