Page 1 of 1
Questions about using a Copilot-generated lua script
Posted: Sat Nov 01, 2025 11:49 pm
by Mgellis
I am a total novice with lua scripting (I can do a one-line action for an event, but that's about it), but I'm trying to learn what I can do with it. I asked Copilot to write a sample lua script for me and it did so. I tried to keep it fairly simple; it simulates a cyberattack on a destroyer and knocks out their radars at a certain time.
I have a couple of questions.
How do I use this? Do I simply set up an Event and copy and paste the whole script into the Action? It appears the trigger is already built into the script. Do I still have to set up a trigger for the Event? Or do I enter the script into the scenario in some other way?
I'm including the script so people can see what Copilot told me to do. (Just curious, is it a good script? The indentations were in the original script but for some reason the forum editor does not recognize them and prints everything flush left margin.)
Thanks.
-----cut here-----
-- Cyberattack simulation on DDG 173 Kongo
local unitName = "DDG 173 Kongo"
local radarSystems = {
"J/OPS-20",
"J/OPS-28C"
}
local triggerTime = "2024-11-03 03:50:00"
-- Function to apply damage and deactivate radar
function cyberattack()
local unit = ScenEdit_GetUnit({name = unitName})
if unit then
for _, radarName in ipairs(radarSystems) do
local sensor = ScenEdit_GetSensor({unit = unit.guid, name = radarName})
if sensor then
-- Set sensor to inactive
ScenEdit_SetSensor({unit = unit.guid, sensor = sensor.guid, isactive = false})
-- Apply medium damage
ScenEdit_SetSensorDamage({unit = unit.guid, sensor = sensor.guid, level = "Medium"})
else
print("Sensor not found: " .. radarName)
end
end
else
print("Unit not found: " .. unitName)
end
end
-- Schedule the cyberattack
ScenEdit_ScheduleEvent({
time = triggerTime,
event = {
description = "Cyberattack on DDG 173 Kongo",
action = cyberattack
}
})
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 1:21 am
by Kushan04
Same issue that ChatGPT has, it's making up functions that don't exist.
ScenEdit_GetSensor, ScenEdit_SetSensorDamage, and ScenEdit_ScheduleEvent are not real functions.
I use ChatGPT for a lot of my scripting. It's a useful tool but you really need to have some knowledge of Lua in order to pickup mistakes like that.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 1:41 am
by thewood1
My work is going through an attempted transformation using genAI tools. Building cookbooks and proper prompts is harder than most people think. In some cases, it takes longer and more effort than if you did the actual work. Its all about context and auditing the work. Failure to properly account for both leads to very bad results. ChatGPT has almost no context for CMO. You have to iterate with it, train it, and have a solid QA process to get good results. Too many people think because it seems like its able to almost carry on a conversation, genAI systems know everything and can reason like a real person. It doesn't and it can't.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 3:48 am
by Mgellis
All right, good to know. Thanks.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 10:52 am
by Uzabit
Generative AI can be a useful aid when coding.
As thewood1 already pointed out, Copilot certainly won’t hand you working code right away. You’ll first need to look up the required functions on
https://commandlua.github.io/
Generative AI tools can assist you with general LUA syntax. However, you’ll still have to manually provide the relevant functions, data types, and context within the conversation - essentially training the AI for your specific use case.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 4:13 pm
by Mark352
Kushan04 wrote: Sun Nov 02, 2025 1:21 am
Same issue that ChatGPT has, it's making up functions that don't exist.
ScenEdit_GetSensor, ScenEdit_SetSensorDamage, and ScenEdit_ScheduleEvent are not real functions.
I use ChatGPT for a lot of my scripting. It's a useful tool but you really need to have some knowledge of Lua in order to pickup mistakes like that.
I see CoPilot make up functions all the time. It happens so often I find it easy to pick them out now when I get code from it. It does get a little frustrating. I have told it many times "that function does not exist in CMO' but, it doesn't retain that knowledge beyond the current session.
When I run a script in the Lua console and get an ERROR from an AI generated script I'll give that error back to the AI. The AI wants to please and is so grateful I pointed out it's error.

It will provide a corrected version that runs...sometimes. Routinely get ERRORs from the updated scripts and then it finally ends up circling back to where we started.
I sometimes go between ChatGPT, Gemini and CoPilot and ask the same question to see if I get similar results. That can be helpful for trying see the different ways something might be accomplished with Lua. I often compare what the AI provides with the documentation in Lua Command Docs; both for my learning and the AI.

There are times a have pasted code and info from Lua Command Docs into CoPilot so it can reference that. Again, I don't believe it retains it beyond the current session.
I have found I need to be very specific when asking it to script something. It often provides more than I need, over complicating something with many lines of code that can done with fewer. Lots of trial and error.
Mgellis wrote: Sat Nov 01, 2025 11:49 pm
How do I use this? Do I simply set up an Event and copy and paste the whole script into the Action? It appears the trigger is already built into the script. Do I still have to set up a trigger for the Event? Or do I enter the script into the scenario in some other way?
This little bit of example code will remove the J/OPS-20 and the J/OPS-28C.
Code: Select all
--Remove sensors to simulate cyberattack
-- Remove J/OPS-20
ScenEdit_UpdateUnit({
guid = "H1WPKN-0HNGQ1P304BAB", --GUID of the ship/platform
mode = "remove_sensor",
dbid = 1036, --database ID for sensor
sensorId = "H1WPKN-0HNGQ1P304BAM" --GUID for the sensor
})
-- Remove J/OPS-28C
ScenEdit_UpdateUnit({
guid = "H1WPKN-0HNGQ1P304BAB", --GUID of the ship/platform
mode = "remove_sensor",
dbid = 1180, --database ID for sensor
sensorId = "H1WPKN-0HNGQ1P304BAN" --GUID for the sensor
})
Create an event with a time trigger and that Lua script as the action and those two sensors will be removed.
You'll need the GUID of the Kongo and each sensor in your scenario to build the script. This code will access that info for the sensors.
Code: Select all
--Print All Sensor GUIDs on a Specific Unit
local unit = ScenEdit_GetUnit({guid = "H1WPKN-0HNGQ1P304BAB"}) --Unit GUID
if unit and type(unit.sensors) == "table" then
print("Sensor Name,DBID,GUID")
for _, sensor in ipairs(unit.sensors) do
local name = sensor.sensor_name or "UNKNOWN"
local dbid = sensor.sensor_dbid or "UNKNOWN"
local guid = sensor.sensor_guid or "UNKNOWN"
print(string.format('"%s",%s,"%s"', name, dbid, guid))
end
else
print("Unit not found or has no sensors.")
end
I hope this helps. This exercise taught me a bit more. Now I might be starting to confuse myself with all this coding. I apologize for typos...especially if I messed up the code.
Cheers
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 7:25 pm
by Mgellis
Thanks for the additional information.
Question...am I correct that you can simply add the script (assuming it has no errors) as the lua action, just pasting it in, and that's how you insert the script into the scenario?
Sorry if this is a dumb question...I've been writing scenarios for a long time, but I'm a novice when it comes to coding in lua.
Thanks again.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 8:28 pm
by thewood1
Just reminding you that PGat has some basic lua tutorials and general event tutorials. Its where I learned a lot applying lua in CMO.
https://www.youtube.com/watch?v=RQgODnn ... _KWwLobkoT
You apply lua in a number of ways: As an action in an event, in the ops manager, in the console, as a script loaded in a file, and maybe a few others. I have always wanted a lua script trigger as part of a waypoint but, alas, its not to be.
Re: Questions about using a Copilot-generated lua script
Posted: Sun Nov 02, 2025 10:08 pm
by Mark352
Mgellis wrote: Sun Nov 02, 2025 7:25 pm
Question...am I correct that you can simply add the script (assuming it has no errors) as the lua action, just pasting it in, and that's how you insert the script into the scenario?
Yes. Just make sure you use the GUID for the unit and sensors in your scenario. When the time trigger activates the script will run and remove the sensors from the Kongo.
I'm still scratching the surface with Lua myself. Like I keep saying; always learning something new. If it doesn't work let me know.
thewood1 wrote: Sun Nov 02, 2025 8:28 pm
Just reminding you that PGat has some basic lua tutorials and general event tutorials. Its where I learned a lot applying lua in CMO.
PGat videos are a really great resource. Got my first exposure to Lua in those videos.