Jump to content

Group.getUnits


Recommended Posts

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

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

Windows 10 64bit, i7 5930K OC 4.2 GHz, AMD 295x2, 16 Gb DDR4

Link to comment
Share on other sites

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...