d0ppler Posted 16 hours ago Posted 16 hours ago (edited) On some very rare occasions, I get error in my script, when trying event.initiator:getName(), though I test event.initiator for nil before I try to get its name. Does this mean that event.initiator doesn't necessary have the getName() function? The problem is that it's hard to replicate, since it almost NEVER occurs, only occasionally. Is there any safer way around this, to avoid this error? shootingHandler = {} function shootingHandler:onEvent(event) if event.id == world.event.S_EVENT_HIT then if event.target.getCoalition and event.initiator then local a = event.initiator:getName() -- here is where it breaks, since getName() on rare occasions returns nil end end end world.addEventHandler(shootingHandler) Should I test for event.initiator:getName() rather than event.initiator ? Edited 16 hours ago by d0ppler A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Mistermann Posted 15 hours ago Posted 15 hours ago Not sure if @cfrag is back these days, but he'd likely have a pretty good response. 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
d0ppler Posted 15 hours ago Author Posted 15 hours ago I've changed the script to if event.target.getCoalition and event.initiator:getName() then And so far it works (like it used to do). Lets hope this little tweak irons out the potential bug A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Solution Draken35 Posted 15 hours ago Solution Posted 15 hours ago (edited) What error do you exactly get? Hit event initiators should be an unit object with a getName(). It is been a while but if I recall correctly I got into a similar issue with Hit event detecting the initiator when the target was hit with the bomblets of cluster bombs. 10 minutes ago, d0ppler said: if event.target.getCoalition and event.initiator:getName() then That will fail if initiator is nil you can use a pcall to test the getName() if event.target.getCoalition and event.initiator then local status, a= pcall(function(p) return p:getName() end,event.initiator) end if status is true, a has whatever getName returned. If status is false, a will have the error description Edited 14 hours ago by Draken35
d0ppler Posted 14 hours ago Author Posted 14 hours ago Thanks, Draken35! I've now changed it (once more) to: shootingHandler = {} function shootingHandler:onEvent(event) if event.id == world.event.S_EVENT_HIT then if event.target.getCoalition and event.initiator then local a = "" pcall(function() if event.initiator and event.initiator.getName then a = event.initiator:getName() end end) end end end world.addEventHandler(shootingHandler) A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Draken35 Posted 14 hours ago Posted 14 hours ago (edited) 10 minutes ago, d0ppler said: Thanks, Draken35! I've now changed it (once more) to: shootingHandler = {} function shootingHandler:onEvent(event) if event.id == world.event.S_EVENT_HIT then if event.target.getCoalition and event.initiator then local a = "" pcall(function() if event.initiator and event.initiator.getName then a = event.initiator:getName() end end) end end end world.addEventHandler(shootingHandler) My pleasure! You are missing a () after the getName in the and. Also, what was the error you were getting? That initiator did not had a getName() function? Edited 14 hours ago by Draken35
Draken35 Posted 14 hours ago Posted 14 hours ago Ohhhh! This was for Polar Shield? Thanks for the campaign! It is my queue to fly it
d0ppler Posted 14 hours ago Author Posted 14 hours ago The error was the standard "nil" value error I'm using ".", not ":" to test this. event.initiator.getName I think it's correct to not use "()", or? Yes, its for polar shield A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Draken35 Posted 14 hours ago Posted 14 hours ago 3 minutes ago, d0ppler said: The error was the standard "nil" value error I'm using ".", not ":" to test this. event.initiator.getName I think it's correct to not use "()", or? Yes, its for polar shield Ah! I see what you mean.. Never thought of testing the existence of a method that way... Gotta test that later And that was a weird error... I can see an initiator not existing when hit event triggers (long range weapon and initiator destroyed before it reaches the target) but since the initiator should be an unit, it has to have the method... Anyway, it would be interesting to dump the whole initiator object to the log to see what it is in there.
d0ppler Posted 14 hours ago Author Posted 14 hours ago 5 minutes ago, Draken35 said: Never thought of testing the existence of a method that way I believe it uses a tiny bit less of cpu resources rather than check the actual value itself. A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Tobias00723 Posted 11 hours ago Posted 11 hours ago I would recommend taking a look here -> This explains it very well why objects can give nil errors DCS Mission Scripting Wizard | TGFB Owner | Dev of HIP Dynamic Server | Maker of TGFB Dynamic | tgfb-dcs.com | Contact me | TGFB Discord
kira_mikamy Posted 4 hours ago Posted 4 hours ago In a S_EVENT_HIT event, the event.initiator can sometimes be the ammunition object instead of the firing unit. Since ammunition objects don’t have a getName() method, calling it directly will return nil. To avoid this, you need to check that the initiator is a valid unit before trying to get its name. This can be done by verifying its object category.
Recommended Posts