how to calculate enemy defeat time in unreal and display it on screen

i tried everything but still dont know how to calculate it this is what im working with the proplem with this is the time is increasing


Set either one of these conditions:-
-first aggro
-first hit by the player/enemy

get game time.

after the enemy is dead, get game time. subtract from the game time above.

you can do this with a lot of methods like adding a number to a variable after the aggro or the first hit initiates, adding +1 every second into that variable and stop adding if deaggroed or defeated and give that time to the UI to display.

Here is how I would do this in order to keep track of other details too.
Big Step 1:

  • Create a struct that matches the details you need, for example:
    (Right click in content drawer → Blueprint → Structure) → Name it as you want usually from what I saw people use **S_**someName

  • Open the struct and add next properties (variables)

Name | Type
PC | PlayerController
EnemyId | int (maybe you have some datatable where you keep track of what enemies you have available) and you can match that id with the one from the datatable.
FirstHitTime | Date Time
LastHitTime | Date Time
DefeatTime | Date Time

  • Next, Create an GameState Blueprint
    (Right click in content drawer → Blueprint Class → All Classes → Search for ‘GameState’) → Open GameMode go to Classes → Game State Class → here set the new gamestate that you’ve just created.

  • Inside the GameState Blueprint add a variable call it as you want I will name it for reference “PCActionOverEnemyTrackers()” the type of this variable will be the struct that you’ve created on the top **S_**someName instead of single make it array

  • Inside the GameState Blueprint add 1 function for reference I will call it UpdatePCActionOverEnemyTrackers having next parameters
    PlayerController | Player Controller Object Reference
    EnemyId | int
    Defeat? | boolean

  • Inside this function UpdatePCActionOverEnemyTrackers() loop trough the “PCActionOverEnemyTrackers” array to find if there’s an existing entry for the given playercontroller, if found update the lasthittime or defeatime based on the parameter Defeat?. If the enemy is defeated then get the curent time (right click in bp, and type “Now”) set DefeatTime to “Now” result, if there is no entry for this playercontroller AND Defeat? is false then create a new entry for PCActionOverEnemyTrackers storing the playercontroller, firsthittime (result of “Now”), lasthittime will be the same as firstHittime, and will be updated on the next call if there is an entry and the enemy is not defeated.

Big Step 2:

  • Inside your Enemy BP, on event begin play store a reference of your game state reference, don’t forget to cast it to the type of the game state you’ve created it should look something like this
    **Event Begin Player → Get Game State(It’s a pure function so don’t try calling it while you pull the node (knot) idk how is that thing called → the result of Get Game State-> Cast to YourGameState → promote the result as a variable and set it → if it fails add a delay of 0.5 seconds and retry so you are always sure that the Enemy BP has a valid reference to the game state.

Next:

  • Event AnyDamage → YourGameStateVariable → Call UpdatePCActionOverEnemyTrackers
    and pass the necessary parameters, Instigated by => PlayerController, here you handle must of the necessary logic, like if the mob is defeated idk let’s say that damage > current enemy hp → then Defeat? = true

Last Step:
In your widget blueprint

  • Create a function or binding that will fetch the defeat time from GameState → PCActionOverEnemyTrack → add this to your viewport and it’s all good to go :slight_smile: .

Please have some attention while trying this I’ve wrote this thing without testing it, but in theory it should work, lmk if it doesn’t

thank you it worked

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