Quick AI brain question

Q Would it be possible to call something like this from a unit script?

local myBrain = self:GetAIBrain():GetCurrentEnemy() or false

Been digging around for another way to get a units current target from within the unit sim, without having to ask the weapon. Which I've often found the results from the weapon can be questionable or unreliable. Leading to sim errors in some specific situations.

I'm also looking into this...

local myFocus = self:GetFocusUnit() or false

To see what is given in comparison to the units weapon.

Cheers!

-Resin

Just to make sure I’ve understood, you want to know what enemy unit a unit (self) is currently trying to fire at? And you’d be ok with sim mod commands to achieve this (ie you’re not asking for purposes of a UI mod)? If so, then I think it’s possible to cycle through the unit’s weapons and return the unit they’re currently targeting, as you mention; for further away targets (where an attack order is queued up but the target isnt in range of the weapon) I think there are also some functions that can help.

I’d need to check my code to refresh my memory on them (GetFocusUnit was one if them I think) which I can’t do at the moment so if no-one’s able to respond in the meantime fee free to ping me later

@maudlin27

Previously I've used...

local myTarget = self:GetWeaponByLabel('Dummy'):GetCurrentTarget() or false

While this does work, it can at times be unreliable as the weapon states can vary based upon its a large number of factors. (Units can have more than one as well) So while yes, I've managed to make it work, it would be better to have the means to pull from the units brain. To see just what the unit is focusing on at that given moment.

Also... PlayUnitSound Is there a list of all the sounds available within FA? Been looking through the GitHub and thus far nothing stands out as "sounds".

nil is always false, so putting or false is excessive.

“Be a yardstick of quality. Some people aren’t used to an environment where excellence is expected.”
— Steve Jobs.
My UI Mods
Support me

@resin_smoker The ones I'm aware of for weapons are
:GetCurrentTarget() - returns the unit, if there is a unit being targeted
:GetCurrentTargetPos() - returns the target, i.e. for ground attack targets (I've not tested to see all the cases in which this triggers, e.g. if it will return a value for a unit target's position)
:WeaponHasTarget() - I've not used this one myself but I'd noted it down for reference when I was looking at how to get weapon target information

with weapons being cycled through with something like:

for iWeapon, oWeapon in oUnit.WeaponInstances do
    if oWeapon.GetCurrentTarget then
        local oCurTarget = oWeapon:GetCurrentTarget()
        if oCurTarget then
		‘Have a target for the weapon
	 end
    end
end

You can also use the Navigator object to get the current target position of a unit, e.g.:

oUnit:GetNavigator():GetCurrentTargetPos() - This isn’t just a move order target – e.g. if the unit is building something, it returns the target of that building
I found it unreliable, and made a note when I tested that with attack-move orders although I’ve not tested it extensively to confirm, it appeared that giving an attack-move order to a unit that caused that unit to then stop and attack a target, would result in the navigator position being that unit’s position.

There's also a :GetGoalPos() function (which I think is based off the navigator as well)

You can also use oUnit:GetFocusUnit() (which works e.g. in the case of a building a unit, I've not checked if it also works in the case of an attack order)

Another one is oUnit:GetGuardedUnit()
I've not tested this one but presume it works with the guard order.

My rough notes on these and other commands are in section 5 of the AI development guide (linked to from the FAF wiki https://wiki.faforever.com/en/Development/AI/AI-Modding) although I don't think they'll add much more than what's here, other than you can look at e.g. unit.lua in the FAF Git as various comments have been added documenting functions available for units (and similarly with other files) which might provide further alternatives.

You could also look into oUnit:GetCommandQueue() and analysing this - there might be documentation on this in the FAF repo as from memory some of the devs were able to figure out how to interpret the results of this (but it's not something I needed to look into beyond checking if a unit's command queue was empty as for my AI I track the orders given to each AI unit anyway which I rely on)