Joyride Posted June 21, 2013 Share Posted June 21, 2013 Basic lua question... I understand that the Group.getUnits returns an array of the unit name and number, but unsure how to run an iterative process using that array. In this example, what I'd like to do is spawn a group but check to make sure that all of the units in that group have NOT spawned on water. If any unit is on water, it respawns the group and tries again. So, there needs to be a "land.getSurfaceType" check on each unit. I highlighted in red my poor attempt just to show what I want to do, although I know that's very wrong. :D Do I need to use the lua pairs function? How exactly would I achieve this?? Many thanks for any tips... function checkSpawnLand(group) repeat local spawnunits = Group.getUnits(group) for i = 1,#group do local pos = Unit.getByName(group):getPosition() local posVec2 = {x = pos.p.x, y = pos.p.z} local spawnSurface = land.getSurfaceType(posVec2) if spawnSurface == 3 then <have a function here to respawn and try again> end end until spawnSurface ~= 3 end Link to comment Share on other sites More sharing options...
geloxo Posted June 21, 2013 Share Posted June 21, 2013 I think you should use #spawnunits in the "for" operator, as spawnunits is the array containing the units inside the group. For the position try something like this: unit = spawnunits pos = unit:getPoint() First one selects the array entry i on every for loop and then reads it position vector with getPoint. You can later access the x, y, z coordinates with pos.x, pos.y or pos.z Link to comment Share on other sites More sharing options...
Puddlemonkey Posted June 21, 2013 Share Posted June 21, 2013 You can try this. Another approach would be to have a function randomly generate the x,y coordinates and test whether that coord is on water before you pass it as the group and unit cords in the addGroup function. So you check before creating the group rather than after - better performance. function checkSpawnLand(group) repeat local spawnunits = Group.getUnits(group) for for _, v in pairs(spawnunits) do local pos = v:getPosition() local posVec2 = {x = pos.p.x, y = pos.p.z} local spawnSurface = land.getSurfaceType(posVec2) if spawnSurface == 3 then <have a function here to respawn and try again> end end until spawnSurface ~= 3 end Link to comment Share on other sites More sharing options...
Joyride Posted June 21, 2013 Author Share Posted June 21, 2013 I rolled in both suggestions (except checking the spawn point before putting units there...could still leave an outlying unit in the group on a different surface), but still getting errors. Here is where I am at...really trying to get the hang of Lua but it's a slow process!... function checkSpawnLand(group) repeat local grouptocheck = Group.getByName(group) local spawnunits = Group.getUnits(grouptocheck) for k,v in pairs(spawnunits) do local pos = v:getPosition() local posVec2 = {x = pos.p.x, y = pos.p.z} local spawnSurface = land.getSurfaceType(posVec2) if spawnSurface == 3 then spawnGroup(group) -- separate function to spawn the group end end until spawnSurface ~= 3 end Link to comment Share on other sites More sharing options...
Puddlemonkey Posted June 21, 2013 Share Posted June 21, 2013 what is your error message? Link to comment Share on other sites More sharing options...
Joyride Posted June 22, 2013 Author Share Posted June 22, 2013 what is your error message? "error in error handling" Link to comment Share on other sites More sharing options...
Grimes Posted June 22, 2013 Share Posted June 22, 2013 Disable slmod, it seems that slmod replaces normal error messages with that message for some reason. The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum Link to comment Share on other sites More sharing options...
Joyride Posted June 22, 2013 Author Share Posted June 22, 2013 Thanks, Grimes. I can't believe I went this long without knowing that (going to stick cold salad fork in my eye now) Link to comment Share on other sites More sharing options...
Recommended Posts