Couple of scripts

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
gamer69
Posts: 12
Joined: Thu Mar 19, 2020 5:42 am

Couple of scripts

Post by gamer69 »

Hi all
Just am new to lua I wonder if someone can help explain how to do 2 scripts.
1.i want to do a script that returns all the dbids of units with SAM in the title
2. A simple lua script that makes a side “Unautodetectable at scenario start

Thanks
Mark352
Posts: 59
Joined: Thu Jul 25, 2024 2:35 pm

Re: Couple of scripts

Post by Mark352 »

I'll take a crack trying to explain how this might be done.

To make an entire side "Unautodetectable" at scenario start using Lua you first need to retrieve the full side structure using VP_GetSide. The side structure includes side.units which is a table of all units belonging to the side. Then you need to loop through every unit on the side. After that I think you need to extract the unit GUID's. Finally you can use ScenEdit_SetUnit to set autodetectable = false.

To return the DBID of all units with SAM in the title uses a similar process. Again you need to use VP_GetSide to access to side.units. You need to loop through all the units on the side. I think you still need to access the GUID's for each unit using ScenEdit_GetUnit to get to all the unit data and loop through to gather the units with SAM in the name. To finish you want to print the DBID and unit.

I can't write these from scratch. I search the Lua Legion for code and use Co-Pilot AI as a guide when I want to try something new. Being a former Harpoon user from way back, I didn't know anything about Lua when I found my way to CMO and I am far from an expert now but, I'm learning. I find Lua to be a really cool tool. There are lots of resources out there to help players become familiar with Lua and incorporate it into scenarios. There are even some basic Lua scripts in the manual that demonstrate some of the basic things we can do with Lua.

So hope this helps.
User avatar
Nikel
Posts: 2461
Joined: Tue Mar 24, 2009 10:51 am

Re: Couple of scripts

Post by Nikel »

After getting bored with ChatGPT errors, tried Perplexity for your first request.

It created this code that seems to work, but you have to change the name for each side.

Code: Select all

-- Change this to the exact side name you want to search
local sideName = "China"

-- Get the side object
local side = VP_GetSide({ Side = sideName })
if side == nil then
  print("ERROR: side '" .. sideName .. "' not found")
  return
end

-- side.units is a table of unit objects on that side. [attached_file:1][web:22]
for _, u in ipairs(side.units) do
  local name = u.name or ""
  if string.find(string.upper(name), "SAM", 1, true) then
    print("SAM UNIT: " .. name .. "  (GUID=" .. (u.guid or "nil") .. ")")
  end
end

This is what is printed for the China side in the scenario Tighten the Straitjacket.
SAM UNIT: SAM Bn (HQ-12) (GUID=2962e57e-3a63-429a-a426-1abf22e383fa)
SAM UNIT: SAM Bn (SA-20a) (GUID=b50aa866-e22d-4df7-acfc-61ec04f9fc0e)
SAM UNIT: SAM Bn (SA-20a) (GUID=344bee43-8e91-4dcb-9f8b-2b5787c3a08b)
SAM UNIT: SAM Bn (HQ-9) (GUID=38bccc6c-f587-40ac-904b-388cd02e3d34)
SAM UNIT: SAM Bn (SA-20a) (GUID=aac2afd0-d8ba-4948-b661-0464e676cf58)
SAM UNIT: SAM Bn (SA-20a) (GUID=9090cda1-1167-4212-a556-dbd7fcbce73f)
SAM UNIT: SAM Bn (SA-20a) (GUID=715c0d6c-fd86-4d66-ba68-bd383b46fc1c)
SAM UNIT: SAM Bn (SA-20a) (GUID=e4892ec6-a2bb-4789-a677-858af1726cd8)
Post Reply

Return to “Lua Legion”