TEMPEST.114 Posted November 12, 2022 Posted November 12, 2022 e.g. I want to get the group data (lua stuff) from a group. e.g. a carrier group and then traverse it looking for stuff. If I try: local carrierAirbase = Airbase.getUnit(airbase) local carrierID = carrierAirbase:getID() local allShipGroups = coalition.getGroups(2, 3) local carrierGroup = {} for _, gp in pairs(allShipGroups) do if tonumber(gp:getID()) == tonumber(carrierID) then carrierGroup = gp end end local tasks = carrierGroup.route.points[1].task.params.tasks This doesn't work. Even though I'm not changing, just looking up, I can't access it. However if I make a mutable deep copy of the 'gp' object, I can traverse and look stuff up easily. Why is this? Am I doing something wrong? What is the reason I can't just 'read' the stuff in the actual object?
cfrag Posted November 12, 2022 Posted November 12, 2022 Without having tried this myself, I'd guess (I'll see if this holds up when I have access to my dev environs) that the objects that DCS internally uses differ slightly from what Lua's interpreter expects. The line carrierGroup.route.points[1].task.params.tasks dereferences implicitly 6 times (including an index offset) - which is begging for trouble (if you break that apart into individual dereferences you'll find where the link breaks. I'm interested to see where it breaks for you, and would love to investigate why). With 'deep copy' you are probably referring to Mist's mist.utils.deepCopy() method that recursively iterates and walks down the entire structure, and re-allocates all objects fresh, clean and correctly with regards to Lua's context. That helps because DCS may have re-allocated, altered or added to objects with C or C++ libraries or perhaps added incompatible hooks into meta-tables). The fresh instances then wipe possible incompatibilities/strange hooks. I too have noticed that group tables seem to get "clobbered" when I use theem coalition.addGroup(), but I have never investigated. My guess is that some non-Lua based code is inserting it's hooks into the meta tables or table hashes that can break your code. Like you, I discovered that I need to clone group tables before I spawn them (although I didn't realize that I could repair them by deep-cloning them after they got clobbered - thank you for that great hint, I'll investigate as soon as I have a chance!). 1 hour ago, Elphaba said: Am I doing something wrong? No. The multiple chained dereferences is asking for trouble, though. Then again, I'd also hate it if I had to explicitly dereference each instance individually. So I guess the best approach is to see where the dereferencing chain breaks, try and find out why, and then code against that.
Recommended Posts