Well, then you have a bug somewhere because this is exactly what Fighter
does, and that works. When the enemy you are attacking dies, it tries to find a new enemy in the vicinity and if it can’t, target
is set to null. Attack over.
I think you may be looking at these skills from the wrong side (unless this is really what you want).
I personally wouldn’t award skill xp when the enemy dies. I’d rather award a small amount of xp each time a skill is successfully used. If I shoot the enemy with my bow and hit, my bow skill is awarded +1xp. If I then switch to a sword and whack him 2 more times, my sword skill gets +1xp for each hit. If I miss a swing, 0xp. If the enemy took 10 hits to kill and I shot him 4 times and hit him 6 times, my bow skill will have received +4xp and my sword skill +6xp. No need to keep track of which weapon was used the most. The amount of xp awarded is not linked to any enemy. It’s just 1 (or some other small value you would like to use). Killing an enemy that takes 10 hits to kill with a sword will result in a total of 10xp gained for that enemy. An enemy that takes 25 hits to kill will award a total of 25xp. And all this xp is neatly distributed across the skills I used to defeat said enemy.
If you do want to only award xp after the fight, I’d go with what Brian said and you’d have to remember that in some cases (like the 10 hit enemy above) you may have shot him 5 times and hit him 5 times and now you have a tie for the top spot. Which one gets the xp? Do you share? Does it not matter and you go with whichever the algorithm returns? Basically, there’s a huge chance that the weapon you used first will be the one that gets returned; 2 shots with bow, 3 hits with sword, 3 shots with bow, 2 hits with sword - tie for first, bow wins the xp because bow is first in the dictionary.