Using Lua to Call Specific Mounts

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
ExNusquam
Posts: 531
Joined: Mon Mar 03, 2014 11:26 pm
Location: Washington, D.C.

Using Lua to Call Specific Mounts

Post by ExNusquam »

So I'm trying to write a scenario where behaviors are generated based on weapons remaining on a unit (and where I'd need to change the number remaining on the fly). I can figure out the syntax to generate a table of mounts attached to a given unit via unit.mounts but I can't figure out the syntax to filter down to a single mount or weapon to pull or modify wpn_current. Does anyone have any idea how to accomplish this?
User avatar
michaelm75au
Posts: 12466
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Using Lua to Call Specific Mounts

Post by michaelm75au »

Simple script.
local unit = ScenEdit_GetUnit({name='mech inf'})
-- list of mounts (with devices present)
-- print(unit.mounts)
-- loop thru the mounts
for k,v in pairs(unit.mounts)
do
-- v is each mount
print('Mount ' .. k);print(v)
--loop thru the weapons on the mount
for k2,v2 in pairs(v.mount_weapons)
do
-- find particular weapon and do something to the first found on the mount
-- say reduce to 10 loads only if more than that
if v2.wpn_dbid == 1695 and v2.wpn_current > 10 then
print(v2)
local to = v2.wpn_current -10
local a = ScenEdit_AddReloadsToUnit({guid=unit.guid, wpn_dbid=v2.wpn_dbid,mount_guid=v.mount_guid, remove=true, number=to})
print('Mount ' .. k .. 'weapon load changed by ' .. a)
break
end
end
end
-- refresh unit
unit = ScenEdit_GetUnit({name='mech inf'})
print(unit.mounts)
Michael
User avatar
michaelm75au
Posts: 12466
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Using Lua to Call Specific Mounts

Post by michaelm75au »

As mounts are tables inside tables, you need to use the appropriate function to affect the elements inside them.
The complication to actual use the elements would be similar to the 'fuel' where you have to update the tables and then write them back in full.[:)]. I wanted to avoid this and the possibility of generating errors by not keying in the correct values.
Michael
ExNusquam
Posts: 531
Joined: Mon Mar 03, 2014 11:26 pm
Location: Washington, D.C.

RE: Using Lua to Call Specific Mounts

Post by ExNusquam »

Michael-
Thanks for the help! This is exactly what I was looking for.
bearhunter007
Posts: 23
Joined: Sat Mar 11, 2017 2:51 am

RE: Using Lua to Call Specific Mounts

Post by bearhunter007 »

Michael,

Thanks for documenting your code. It's a simple script for you but "tables" are different then the arrays I'm used to working with in c and vba.
User avatar
michaelm75au
Posts: 12466
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Using Lua to Call Specific Mounts

Post by michaelm75au »

Yea, I have to stop and think which array/table I'm using most of time at work; SQL, Java, Perl, ...[:D]
Michael
Post Reply

Return to “Mods and Scenarios”