Crossmann Posted March 1 Posted March 1 (edited) Context: What I'm Trying to Achieve I'm scripting a mission in DCS where a player-controlled unit (BLUE_SEAD_1-1) has an AI escort (AI_BLUE_ESC_2-1). The escort is set to Late Activation and only spawns if no human player takes control of it. I have written a Lua script that allows the player to use an F10 menu option ("Colt Report Position") to check: Their own current position (X, Y, Z) The escort's current position (X, Y, Z) The distance between them The bearing of the escort relative to the player (in degrees & clock position) The function retrieves the units dynamically and calls getPosition() on both to get the updated coordinates. The Issue: AI Escort's Position Doesn't Update While the player's position updates correctly every time they call the function, the escort's position remains frozen at its initial mission editor coordinates (even though the escort is moving in-game). This means: The getPosition() function returns the correct values for the player, but The escort's position always remains the same, even though the AI aircraft has moved. Debugging Attempts Checked if the unit is being retrieved correctly Unit.getByName("AI_BLUE_ESC_2-1") returns a valid unit, meaning it exists. Unit.getByName("AI_BLUE_ESC_2") also retrieves the unit correctly Confirmed that the function executes only after the AI escort is spawned The menu function is only available after the escort is activated. Tried multiple retrieval methods for escort position EscortUnit:getPosition() No update; position remains static. EscortUnit:getPoint() Same issue. Object.getByName("AI_BLUE_ESC_2-1") No difference. coalition.getGroups() to dynamically find the group Still no position updates. Checked the DCS logs The script logs confirm that the escort's position never changes after the first retrieval. The player’s position updates dynamically as expected. local function ReportEscortPosition() env.info("ReportEscortPosition called!") -- Retrieve Player Unit local LeadUnit = Unit.getByName("BLUE_SEAD_1-1") if not LeadUnit then env.info("ERROR: LeadUnit (BLUE_SEAD_1-1) not found.") return end -- Retrieve AI Escort Unit local EscortGroup = Unit.getByName("AI_BLUE_ESC_2-1") if not EscortUnit then env.info("ERROR: EscortUnit not found.") return end -- Get positions local LeadPos = LeadUnit:getPosition() local EscortPos = EscortUnit:getPosition() if not LeadPos or not EscortPos then env.info("ERROR: Could not retrieve positions.") return end -- Extract coordinates local PlyrPos = LeadPos.p local EscPos = EscortPos.p -- Calculate distance local dX = EscPos.x - PlyrPos.x local dZ = EscPos.z - PlyrPos.z local distance = math.sqrt(dX^2 + dZ^2) / 1000 -- Calculate bearing (this is not correct as no check for the quadrants is still implemented) local bearing = math.deg(math.atan2(dX, dZ)) if bearing < 0 then bearing = bearing + 360 end -- Convert to clock position (1-12) local clockPosition = math.floor(((bearing + 15) % 360) / 30) + 1 -- Debug logs env.info(string.format("Player Position: X=%.1f, Y=%.1f, Z=%.1f", PlyrPos.x, PlyrPos.y, PlyrPos.z)) env.info(string.format("Escort Position: X=%.1f, Y=%.1f, Z=%.1f", EscPos.x, EscPos.y, EscPos.z)) env.info(string.format("Escort is %.1f km away, Bearing: %.1f°, Clock: %d o’clock", distance, bearing, clockPosition)) end Questions for the Community Why is getPosition() not updating for the AI escort, even though the unit is moving in-game? Does Late Activation affect how getPosition() works? If so, how can I force it to return updated coordinates? Is there a workaround to get the escort’s real-time position dynamically? Is this a known issue in DCS scripting, or am I missing something? Any insights or fixes would be greatly appreciated! Let me know if you need additional logs or a test mission file. Thanks in advance! Edited March 1 by Crossmann wrote group instaed of unit in a place My other callsign is Corsair
Mistermann Posted March 1 Posted March 1 (edited) I just spent far too much time figuring out how to do this last week. I use it for late activated units and it works great. I use this command, not getPosition. local _targetCoord = Unit.getByName(_unitName):getPoint() <-- _unitName would be your "AI_BLUE_ESC_2-1" Try going that route. Edited March 1 by Mistermann System Specs: Spoiler Callsign:Kandy Processor:13th Gen Intel(R) Core(TM) i9-13900K - RAM: 64GB - Video Card: NVIDIA RTX 4090 - Display: Pimax 8kx VR Headset - Accessories: VKB Gunfighter III MCG Ultimate, VKB STECS Standard, Thrustmaster TPR Pedals, Simshaker JetPad, Predator HOTAS Mounts, 3D Printed Flight Button Box Video Capture Software: Open Broadcaster Software (OBS), Video Editing Software: PowerDirector 35 Into The Jungle (MP Mission) F18: Scorpion's Sting Apache Campaign - Griffins Kiowa Campaign - Assassins
cfrag Posted March 2 Posted March 2 17 hours ago, Crossmann said: Why is getPosition() not updating for the AI escort, Check your variable names, especially escortGROUP vs escortUNIT. Methinks EscortUnit isn't defined.
Crossmann Posted March 2 Author Posted March 2 Follow-up & Apology - Issue Resolved Hey everyone, I just wanted to follow up on my previous post regarding the AI escort's position not updating and apologize for any confusion or unnecessary troubleshooting effort I may have caused. After further investigation, I discovered that the issue was not actually related to getPosition() or the Late Activation of the AI escort itself. Instead, the root cause was a mission design choice in another part of my script, where I had implemented logic to dynamically spawn AI replacements for any client slots that were not occupied by human players. This resulted in the escort AI being spawned through a separate mechanism, which unintentionally interfered with how the unit was being referenced and tracked. What Was Happening The getPosition() function was working correctly. However, the unit reference I was retrieving was still pointing to the original, pre-spawned unit from the Mission Editor, not the dynamically spawned AI escort. Because of this, even though the AI escort was moving in-game, my script was still looking at the old, inactive unit and returning static coordinates. Solution Once I adjusted my mission logic so that the AI escort was properly referenced after it was spawned, everything started working as expected. Now, I retrieve the correct instance of the spawned AI escort, and its position updates dynamically every time the player requests a position report. I sincerely appreciate the time and effort that some of you may have spent trying to help troubleshoot this. Thank you all for your patience and support! Hopefully, this serves as a reminder to always double-check how units are being spawned and referenced dynamically in DCS scripting. Thanks again! 1 My other callsign is Corsair
cfrag Posted March 3 Posted March 3 13 hours ago, Crossmann said: the root cause was a mission design choice in another part of my script It's good that you were able to resolve the issue. Please be advised that your script still contains at least one show-stopping bug: local EscortGroup = Unit.getByName("AI_BLUE_ESC_2-1") if not EscortUnit then env.info("ERROR: EscortUnit not found.") return end Your script should not progress past that point, and always end with the message "EscortUnit not found". 1
Recommended Posts