LUA script for RTB flying UAV/UCAVs and set them unavailable

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
eleos
Posts: 110
Joined: Mon Mar 21, 2016 11:57 am
Location: Mesoropi, Macedonia, Greece

LUA script for RTB flying UAV/UCAVs and set them unavailable

Post by eleos »

I'm trying to write a LUA script which
1) will pick all flying UAV/UCAVs of Bravo side, return them to base and set them unavailable
2) will pick all UAV/UCAVs of Bravo that are already grounded and set them unavailable

I came up with this script:
-- Define the get_side function
function get_side(sides, name)
for _, side in pairs(sides) do
if string.lower(side.name) == string.lower(name) then
return side
end
end
end

-- Define the return_to_base function
function return_to_base(units)
for _, unit in pairs(units) do
if unit:get_speed() > 0 then
local base = unit:get_airbase()
if base ~= nil then
unit:set_available(false)
unit:task_return_to_base(base)
end
else
unit:set_available(false)
end
end
end

-- Find all UAVs and UCAVs of Side B
local sides = get_sides()
local side_b = get_side(sides, "Bravo")
local uavs = side_b:get_units({type = {"UAV", "UCAV"}})

-- Return all flying UAVs and UCAVs of Bravo to their bases and set them as unavailable
return_to_base(uavs)

-- Set all grounded UAVs and UCAVs of Bravo as unavailable
for _, unit in pairs(uavs) do
if unit:get_speed() == 0 then
unit:set_available(false)
end
end

But when I run this script in LUA script console it comes up this error:
ERROR: [string "Console"]:26: attempt to call a nil value (global 'get_sides').

How could I solve this?
User avatar
lumiere
Posts: 286
Joined: Tue Mar 19, 2019 10:38 am

Re: LUA script for RTB flying UAV/UCAVs and set them unavailable

Post by lumiere »

I think you used ChatGPT for generating script (apologies if I was wrong): it/he/she still does not recognize functions particular in Command, and returns plausible but unavailable code.

So I wrote code from scratch: works fine in attached scenario. Hope it helps.

Code: Select all

local side = "Bravo"

--filter side unit by unit category 2001 (Fixed Wing)/subtype 8201 (UAV) and 8202 (UCAV)
local uav = VP_GetSide({name=side}):unitsBy("Aircraft", 2001, 8201)
local ucav = VP_GetSide({name=side}):unitsBy("Aircraft", 2001, 8202)

--function setup
-- if unit.airbornetime_v (unit airbornetime in second) is zero (=unit is grounded), directly set maintenance loadout
-- if unit.airbornetime_v is NOT zero (=unit is airborne), order unit to RTB and generate event "Set (unitname) as unavailable after landing event" using UnitBaseStatus trigger and Lua action
-- see https://www.matrixgames.com/forums/viewtopic.php?f=10236&t=393497 for more info about UnitBaseStatus trigger

function SetAircraftUnavailable(tablname)
	local unit
	for k, v in ipairs(tablname) do
		unit = ScenEdit_GetUnit({guid=v.guid})

		if unit.airbornetime_v == 0 then
			ScenEdit_SetLoadout({unitname=unit.guid, LoadoutID=4}) --maintenance loadout
		else
			unit:RTB(true)

			local eventname = "Set " ..unit.name.. " as unavailable after landing event"
			local TargetFilter = {
				TargetSide = unit.side,
				TargetType = unit.type,
				TargetSubType = unit.subtype,
				SpecificUnitClass = unit.class,
				SpecificUnitID = unit.guid,
			}
			local TargetCondition= 7 --unit is landed
			local trigger = ScenEdit_SetTrigger({mode="add", name=eventname, type="UnitBaseStatus",
			TargetFilter=TargetFilter, TargetCondition=TargetCondition})

			local ScriptText =
			'local unitx = ScenEdit_UnitX() \r\n' ..
			'ScenEdit_SetLoadout({unitname=unitx.guid, LoadoutID=4})'
			ScenEdit_SetAction({mode="add", name=eventname, type="LuaScript", ScriptText=ScriptText})

			ScenEdit_SetEvent(eventname, {mode="add", IsShown=false})
			ScenEdit_SetEventTrigger (eventname, {mode="add", name=eventname})
			ScenEdit_SetEventAction (eventname, {mode="add", name=eventname})
		end
	end
end
--

if uav ~= nil then
	SetAircraftUnavailable(uav)
end

if ucav ~= nil then
	SetAircraftUnavailable(ucav)
end
Attachments
Lua Testscen.zip
(15.79 KiB) Downloaded 20 times
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
My Lua garbages (Github)
User avatar
eleos
Posts: 110
Joined: Mon Mar 21, 2016 11:57 am
Location: Mesoropi, Macedonia, Greece

Re: LUA script for RTB flying UAV/UCAVs and set them unavailable

Post by eleos »

Your guess is correct as I am not very good at using LUA and write scripts. I tried to have a shot using chatGPT.
Nonetheless thank you for your time and effort. I will try it as soon as possible
Post Reply

Return to “Lua Legion”