Krinje Posted January 21, 2017 Posted January 21, 2017 Hello, I'm learning how to script. I've taken some inspiration from a SAR mission I saw, cracked it open and looked at how the author approached the scripting. From there I started with similar structures to do a similar mission. I've been adding on single pieces of functionality at a time. IE: find an oil platforms world position with getPoint(), then subtract a helicopter's position from that to get the delta. At each stage I've managed to fix my mistakes and continue but I now feel I've hit an invisible wall, some detail isn't working and I can't see it. Before I added "for j = 1, #oil do" loop everything was fine, I only checked oil[1]. After adding in this loop, and then even with "for j = 1, 1 do" It seems to have broken. Excuse my rambling, I'm just a bit frustrated. I'm clearly doing something incorrectly I just don't know what. My only guess is variable scope, but again I'm not *seeing* a problem, but that may be because I'm not understanding lua's scope correctly. do local Heli = { [1] = { name = "Mi-8 Pilot #001" }, [2] = { name = "Mi-8 Pilot #002" } } local oil = { [1] = { name = "Oil Rig #001", Crew = 55, VIP = 1, HpadOffset = {x = -26.5, y = 39.08, z = 21.5}, HpadRadius = 12, bearing = 0 }, [2] = { name = "Oil Rig #002", Crew = 55, VIP = 1, HpadOffset = {x = -26.5, y = 39.08, z = 21.5}, HpadRadius = 12, bearing = 0 }, } local function Pickup() for i = 1, #Heli do local unit = Unit.getByName(Heli[n].name) if unit ~= nil then local heliPos = unit:getPoint() local heliGroup = unit:getGroup():getID() for j = 1, 1 do local rig = StaticObject.getByName(oil[j].name) local rigPos = rig:getPoint() local HpadOffset = oil[j].HpadOffset local deltaPos = { x = heliPos.x - rigPos.x - HpadOffset.x, y = heliPos.y - rigPos.y - HpadOffset.y, z = heliPos.z - rigPos.z - HpadOffset.z } trigger.action.outTextForGroup(heliGroup, " Delta X: " .. deltaPos.x .. " Delta Y: " .. deltaPos.y .. " Delta Z: " .. deltaPos.z, 3) end end end return timer.getTime() + 3 end timer.scheduleFunction(Pickup, nil, timer.getTime() + 5) end Thanks for any help! Eventually I will use this to detect landings on the Oil Platform helipad and moving cruiser helipads.
ESAc_matador Posted January 22, 2017 Posted January 22, 2017 I am a newby with lua but... local unit = Unit.getByName(Heli[n].name) "n" i guess should be the variable i=1,#Heli. You did it with thr Oil using "j" Why don you use local unit = Unit.getByName(Heli.name) Also, the iteration i=1... Is secuencial I think. If you are using the Heli numer two and there is no One it stops I think. But again i am a newby
Krinje Posted January 22, 2017 Author Posted January 22, 2017 (edited) Wow yeah, sometimes you get really wrapped up. It used to be n=1, Earlier I was so frustrated with the loops I wanted to make sure there wasn't a problem, and I apparently missed changing all the 'n's to 'i's Thanks, I'll give that a shot and see if she runs Edit: Yup that did it. I can't believe I overlooked that about a hundred times. Edited January 22, 2017 by Krinje
ESAc_matador Posted January 23, 2017 Posted January 23, 2017 Shit happens! Specially to me, all the time!
Recommended Posts