Lua - Get Weapon Target

All discussions & material related to Command's Lua interface

Moderators: michaelm75au, angster, RoryAndersonCDT, MOD_Command

Post Reply
User avatar
blu3s
Posts: 1179
Joined: Fri Jul 08, 2022 9:45 am

Lua - Get Weapon Target

Post by blu3s »

Is there any way through the attributes of a weapon unit class, to know which is its target?

Thank you
User avatar
lumiere
Posts: 281
Joined: Tue Mar 19, 2019 10:38 am

Re: Lua - Get Weapon Target

Post by lumiere »

Getting target guid of given unit is somewhat trickey, because unit prolerty itself does not have the target guid list.

Method I came up with is to cross check weapon guid is in targetedBy field table of the contact.
local weapon = '87yzy5-0hmrsvl00sm3c' --weapon guid
local weapon = ScenEdit_GetUnit({guid=weapon})
local contacts = VP_GetSide ({side=weapon.side}).contacts
local hostile = {}

for k, v in ipairs(contacts) do --pick up hostile contact as "hostile" table
 local contact = VP_GetContact({guid=v.guid})
 if contact.posture == "H" then
  table.insert(hostile, v.guid)
 end
end
print(hostile)

local result = nil --check weapon guid is in contact targettedBy table
for k, v in ipairs(hostile) do
 local contact = VP_GetContact({guid=v})
 if contact.targetedBy ~= nil then
  for k1, v1 in pairs(contact.targetedBy) do
   if v1 == weapon.guid then
    result = v
   end
  end
 end
end

return result
Hope there is targetList field in unit property added instead of this roundabout method.
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
My Lua garbages (Github)
User avatar
blu3s
Posts: 1179
Joined: Fri Jul 08, 2022 9:45 am

Re: Lua - Get Weapon Target

Post by blu3s »

lumiere wrote: Wed Jul 05, 2023 3:05 am Getting target guid of given unit is somewhat trickey, because unit prolerty itself does not have the target guid list.

Method I came up with is to cross check weapon guid is in targetedBy field table of the contact.
local weapon = '87yzy5-0hmrsvl00sm3c' --weapon guid
local weapon = ScenEdit_GetUnit({guid=weapon})
local contacts = VP_GetSide ({side=weapon.side}).contacts
local hostile = {}

for k, v in ipairs(contacts) do --pick up hostile contact as "hostile" table
 local contact = VP_GetContact({guid=v.guid})
 if contact.posture == "H" then
  table.insert(hostile, v.guid)
 end
end
print(hostile)

local result = nil --check weapon guid is in contact targettedBy table
for k, v in ipairs(hostile) do
 local contact = VP_GetContact({guid=v})
 if contact.targetedBy ~= nil then
  for k1, v1 in pairs(contact.targetedBy) do
   if v1 == weapon.guid then
    result = v
   end
  end
 end
end

return result
Hope there is targetList field in unit property added instead of this roundabout method.
Uhh very nice approach! Yes, being able to have the information of the weapon object in its properties would be infinitely more convenient.
User avatar
lumiere
Posts: 281
Joined: Tue Mar 19, 2019 10:38 am

Re: Lua - Get Weapon Target

Post by lumiere »

Oh, weapon wrapper has validTargetList field which shows the target list though I haven't tried yet.

https://commandlua.github.io/assets/Wra ... per_Weapon
---
Edit: validTargetList field will be used in conjunction with ScenEdit_QueryDB() to show weapon's available target, not existing individual weapon's current target contact.

https://www.matrixgames.com/forums/sear ... 0%5D=10201
Last edited by lumiere on Mon Jul 10, 2023 1:10 pm, edited 1 time in total.
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
My Lua garbages (Github)
User avatar
lumiere
Posts: 281
Joined: Tue Mar 19, 2019 10:38 am

Re: Lua - Get Weapon Target

Post by lumiere »

Another finding. Unit wrapper got from ScenEdit_GetUnit() have "weapon" field which returns weapon shooter and target contact's table.
https://commandlua.github.io/assets/Wra ... apper_Unit
weapon { shooter, contact, detonated }

Table of shooter unit, at contact unit and detonated (when destroyed) if a weapon (READ ONLY)
So it will be:

Code: Select all

local unit = ScenEdit_GetUnit({guid=weaponguid})
return unit.weapon.contact.guid
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
My Lua garbages (Github)
User avatar
blu3s
Posts: 1179
Joined: Fri Jul 08, 2022 9:45 am

Re: Lua - Get Weapon Target

Post by blu3s »

lumiere wrote: Mon Jul 10, 2023 1:02 pm Another finding. Unit wrapper got from ScenEdit_GetUnit() have "weapon" field which returns weapon shooter and target contact's table.
https://commandlua.github.io/assets/Wra ... apper_Unit
weapon { shooter, contact, detonated }

Table of shooter unit, at contact unit and detonated (when destroyed) if a weapon (READ ONLY)
So it will be:

Code: Select all

local unit = ScenEdit_GetUnit({guid=weaponguid})
return unit.weapon.contact.guid
Uhh this is perfect! Thank you!
Post Reply

Return to “Lua Legion”