Get SaintAxe&Ror SpeechMaker Now
And build your conversations easily !
 
LUA programming - Tips - Functions

Here are some functions or some tips I use when I create a mission , using OFDR Mission Editor
Need to share Highlighted and indented LUA source code ? WRITE YOUR CODE HERE

Link : Official LUA Documentation

Added : 2011-09-13

Function isInSpawnRange(unitName, waypointName)

More complete function than isFarEnough(unitName, waypointName) I wrote previously.

This function is used for spawning PLA Units at a certain range from players using OFP:spawnEntitySetAtLocation(). The units are spawned at waypoints location set in Mission Editor. With this function I can spawn PLA between a minimum distance and a maximum distance from the player. No need to spawn a PLA at 1 mike away from the player


minSpawnRange = 250
maxSpawnRange = 700

function isInSpawnRange(unitName, waypointName)
	if not OFP:isAlive(unitName) then
		return true
        else 		
		local x = OFP:getDistance(unitName, waypointName)
		local dist = math.floor(x)
		
		if (dist > minSpawnRange) and (dist < maxSpawnRange) then
			return true
		else
			return false
		end
	end
end
 


This function is used like below.
Say player echelon members are called sar1, sar2, sar3, sar4 and we want to spawn an entityset at the waypoint location :

-- spawnWp is the name of the waypoint
-- spawnEset is the entityset

if isInSpawnRange("sar1", spawnWp)
     and isInSpawnRange("sar2", spawnWp)
          and isInSpawnRange("sar3", spawnWp)
               and isInSpawnRange("sar4", spawnWp)
then
     spawnId = OFP:spawnEntitySetAtLocation(spawnEset, x, y, z) 
end
 



If isInSpawnRange() returns false, that means the waypoint is too close or too far from one of the members of the player echelon, so we do not spawn any PLA unit. Make all that together in some loop/function in a mission "et voilà!"

 
Added : 2011-02-18

Function isFarEnough(unitName, waypointName)

This function is used for spawning PLA Units at a certain distance from players using OFP:spawnEntitySetAtLocation(). The units are spawned at waypoints location set in Mission Editor

spawnDistance = 200  -- increasing/decreasing  will allow units to spawn close/far

function isFarEnough(unitName, waypointName)
     if not OFP:isAlive(unitName) then
         return true 
     else
         local posU = {OFP:getPosition(unitName)}
         local posW = {OFP:getPosition(waypointName)}
 
         local distX = math.floor(math.max(posU[1], posW[1]) - math.min(posU[1], posW[1]))
         local distY = math.floor(math.max(posU[2], posW[2]) - math.min(posU[2], posW[2]))
 
         if distX > spawnDistance
              or distY > spawnDistance 
         then
              return true 
         else
             return false 
         end 
     end 
end
 

This function is used like below.
Say player echelon members are called sar1, sar2, sar3, sar4 and we want to spawn an entityset at the waypoint location :

-- spawnWp is the name of the waypoint
-- spawnEset is the entityset
 
local x, y, z = OFP:getPosition(spawnWp)
if isFarEnough("sar1", spawnWp)
     and isFarEnough("sar2", spawnWp)
          and isFarEnough("sar3", spawnWp)
               and isFarEnough("sar4", spawnWp) 
then
     spawnId = OFP:spawnEntitySetAtLocation(spawnEset, x, y, z) 
end
 


If isFarEnough() returns false, that means the waypoint is too close from one of the members of the player echelon, so we do not spawn any PLA unit.
Make all that together in some loop/function in a mission "et voilà!"

 
Added : 2011-02-17

Function isAllInSameVehic(echelonName, vehicleName)

This function verifies that all the units of an echelon are mounted in the same vehicle. There is a native function called isAllMounted(entityGroupOrEchelonName) but when it returns true, you are not sure that the units are in the same vehicle. They could be all mounted but in some different vehicles.

function isAllInSameVehic(echelonName, vehicleName)
     local size = OFP:getEchelonSize(echelonName)
     local fullSize = OFP:getEchelonFullSize(echelonName)
     local inSameVehic = 0

     for i = 0, fullSize -1 , 1 do
         local mate = OFP:getEchelonMember(echelonName, i)
         if OFP:isAlive(mate) then
	         if OFP:getMountedVehicle(mate) == string.lower(vehicleName) then
	             inSameVehic = inSameVehic + 1
	         end
	     end
     end

     if inSameVehic == size then
          return true
     else
          return false
     end
end
 

Function can be called like below :
When all the members of the echelon "echSAR" are mounted in the chopper called "sarHelo", the helo takes-off and moves.

function onMount_sarHelo(vehicleName, unitName, echelonName) 
    if OFP:isInEchelon(unitName, "echSAR") then 
        if isAllInSameVehic("echSAR", "sarHelo") then 
            OFP:takeoff("eSarHelo", "ADDTOFRONT")
            OFP:move("eSarHelo", "wpGoHelop", "ADDTOEND") 
        end 
    end 
end
 

 
Added : 2011-02-07

Function inArray(tableOfEntities,value)

This function allows to verify if an element is a part of a table (LUA array) . This is a similar function as PHP function called inArray()

function inArray(tableOfEntities,value)
  for i,v in ipairs(tableOfEntities) do
    if v == value then
      return true
    end
  end
  return false
end 

Example :
While playing online with some guys, I saw one of them mount a vehicle I set to not be mountable. Cheater was in !
So, I worked and found a solution in order to kill the cheater if he try to mount a vehicle he should not.

function onMissionStart() 
    -- table that contains the vehicles the players is not allowed to mount
    VehicNoMount = {"Jeep1", "Jeep2", "Jeep3"} 
 
    -- set the vehicles not mountable
    for i = 1, #VehicNoMount do
        OFP:setVehicleMountableStatus(VehicNoMount[i], 0)
    end
end 
 
function onMount(vehicleName, unitName, echelonName) 
    -- dont mount the vehics
    if OFP:isInEchelon("playerEchelon") then
        if inArray(VehicNoMount, vehicleName) then
            OFP:damage(unitName, "headzone", 999) -- kill cheater 
        end 
    end 
end