DeDave98 Posted February 21 Posted February 21 Hey guys, so, I (with the help of chatGTP) created this script: Spoiler trigger.action.outText("Dogfight Skript geladen!", 10) dogfightMenu = missionCommands.addSubMenu("Dogfight") trigger.action.outText("Dogfight Menü hinzugefügt!", 10) fox2Menu = missionCommands.addSubMenu("FOX2", dogfightMenu) gunsMenu = missionCommands.addSubMenu("GUNS", dogfightMenu) local enemyTemplates = { F16 = {Fox2 = "Enemy_F16_Fox2_Template", Guns = "Enemy_F16_Guns_Template"}, F5 = {Fox2 = "Enemy_F5_Fox2_Template", Guns = "Enemy_F5_Guns_Template"}, Mig19 = {Fox2 = "Enemy_Mig19_Fox2_Template", Guns = "Enemy_Mig19_Guns_Template"}, Mig29 = {Fox2 = "Enemy_Mig29_Fox2_Template", Guns = "Enemy_Mig29_Guns_Template"}, Su27 = {Fox2 = "Enemy_Su27_Fox2_Template", Guns = "Enemy_Su27_Guns_Template"}, F4 = {Fox2 = "Enemy_F4_Fox2_Template", Guns = "Enemy_F4_Guns_Template"} } local function spawnEnemy(playerName, aircraftType, weaponMode) if not playerName then trigger.action.outText("Fehler: Spielername ist nil!", 10) return end trigger.action.outText("spawnEnemy aufgerufen für " .. playerName, 10) local playerUnit = nil for i, unit in ipairs(coalition.getPlayers(1)) do if unit:getPlayerName() == playerName then playerUnit = unit break end end if not playerUnit then trigger.action.outText("Fehler: Spieler nicht gefunden!", 10) return end trigger.action.outText("Spieler gefunden: " .. playerName, 10) local playerPos = playerUnit:getPoint() local playerHeading = playerUnit:getHeading() if not playerPos or not playerHeading then trigger.action.outText("Fehler: Konnte Spielerposition oder Heading nicht bestimmen!", 10) return end trigger.action.outText("Spielerposition: X=" .. playerPos.x .. " Z=" .. playerPos.z .. " Heading=" .. playerHeading, 10) local spawnDistance = 9260 -- 5 nautische Meilen in Metern local spawnX = playerPos.x + spawnDistance * math.cos(playerHeading) local spawnZ = playerPos.z + spawnDistance * math.sin(playerHeading) local spawnHeading = (playerHeading + math.pi) % (2 * math.pi) local groupTemplate = enemyTemplates[aircraftType][weaponMode] if not groupTemplate then trigger.action.outText("Fehler: Kein Template für " .. aircraftType .. " im " .. weaponMode .. " Modus gefunden!", 10) return end trigger.action.outText("Verwende Template: " .. groupTemplate, 10) local groupData = mist.cloneGroup(groupTemplate, true) if not groupData then trigger.action.outText("Fehler: mist.cloneGroup fehlgeschlagen!", 10) return end if groupData.units then groupData.units[1].x = spawnX groupData.units[1].y = spawnZ groupData.units[1].heading = spawnHeading else trigger.action.outText("Fehler: Keine gültigen Einheiten in groupData!", 10) return end trigger.action.outText("Setze Gegner-Koordinaten: X=" .. spawnX .. " Z=" .. spawnZ .. " Heading=" .. spawnHeading, 10) local spawnedGroup = mist.dynAdd(groupData) if spawnedGroup and spawnedGroup.name then trigger.action.activateGroup(Group.getByName(spawnedGroup.name)) trigger.action.outText("Gegnergruppe " .. spawnedGroup.name .. " gespawnt!", 10) else trigger.action.outText("Fehler: Gruppe nicht gespawnt", 10) end trigger.action.outText("Dogfight gestartet mit " .. aircraftType .. " im " .. weaponMode .. " Modus!", 10) end for aircraft, templates in pairs(enemyTemplates) do missionCommands.addCommand( aircraft, fox2Menu, function() trigger.action.outText("Menü-Callback erreicht: FOX2 für " .. aircraft, 10) local playerName = mist.utils.getPlayerName() trigger.action.outText("FOX2 gewählt, Spieler: " .. (playerName or "NIL"), 10) if playerName then spawnEnemy(playerName, aircraft, "Fox2") else trigger.action.outText("Fehler: Konnte keinen Spielernamen ermitteln!", 10) end end ) missionCommands.addCommand( aircraft, gunsMenu, function() trigger.action.outText("Menü-Callback erreicht: GUNS für " .. aircraft, 10) local playerName = mist.utils.getPlayerName() trigger.action.outText("GUNS gewählt, Spieler: " .. (playerName or "NIL"), 10) if playerName then spawnEnemy(playerName, aircraft, "Guns") else trigger.action.outText("Fehler: Konnte keinen Spielernamen ermitteln!", 10) end end ) end As you can see, it has lots of action.outText functions for error handling and this is where I´m lost. The ONLY messages I get are the ones at the top where it shows that the script and the submenus loaded properly and the "Menü-Callback erreicht:" Messages which is down at the last for-loop. What this script SHOULD do is spawn a unit from a template placed in the ME (and yes, these templates are placed and correctly named) 5nm ahead of the player who calls the option from the F10 menu. But instead, nothing happens. My guess is that mist.utils.getPlayerName somehow doesn´t work as intended and that the script doesn´t run further. The DCS.log doesn´t have any error messages about this in it either. Now asking all of you LUA-wizards, where did I turn wrong? What am I missing? I feel like the script itself is robust and SHOULD work and it´s a really dumb obvious error somewhere that I can´t identify. Mind you while I know basic coding in C++ and Pyhton, also object orientated, I´m not THAT proficient in LUA. And just to state the obvious here, of course the script is loaded in the mission (hence I wouldn´t get the options to click in the first place) and MIST and Moose in their current versions are loaded BEFORE that.
cfrag Posted February 23 Posted February 23 (edited) Unfortunately, you do not show when and how above code is invoked, nor how those templates work. There's one thing, however, that makes me very uncomfortable. On 2/21/2025 at 7:16 PM, DeDave98 said: with the help of chatGTP It's simply not there yet, and tends to make things worse for people trying to learn. Don't use chatGPT for mission scripting. Also On 2/21/2025 at 7:16 PM, DeDave98 said: mist.utils.getPlayerName() What is that? Edited February 23 by cfrag 1
DeDave98 Posted February 24 Author Posted February 24 Well, I guess you´re right. On second thought, if I were on the other side of this, my motivation to debug a code for someone who probably doesn´t get what I´m trying to say and thought that chatGTP can create such a script without errors would be pretty low. I´m sorry. Although I had some success with simpler stuff. I will push this project back and concentrate on learning more about LUA and DCS scripting. I already made some progress with spawning random air groups and random locations so I guess I´m on the right track. I also have a new project which is a good opportunity to learn more about scripting. As I said, I will push this project back and revisit it later when I know more. Although I already KINDA implemented what I tried to script with triggers but it´s not quite there yet.
ops Posted March 14 Posted March 14 On 2/24/2025 at 3:25 AM, DeDave98 said: Well, I guess you´re right. On second thought, if I were on the other side of this, my motivation to debug a code for someone who probably doesn´t get what I´m trying to say and thought that chatGTP can create such a script without errors would be pretty low. I´m sorry. Although I had some success with simpler stuff. I will push this project back and concentrate on learning more about LUA and DCS scripting. I already made some progress with spawning random air groups and random locations so I guess I´m on the right track. I also have a new project which is a good opportunity to learn more about scripting. As I said, I will push this project back and revisit it later when I know more. Although I already KINDA implemented what I tried to script with triggers but it´s not quite there yet. GPT in real life works different that what media likes to claim. Everything non-trivial, it makes up a huge pile of nonsense, even with documentation. In your case, it made up a non-existing function... in many cases worse than some toddler using google make sure you're able to see errors in the log, there has to be one, you can't call undefined functions without getting an error. feeding back this error into GTP might solve your issue, but be prepared to end up with a big mess that's otherwise a few lines of code.
Recommended Posts