Jump to content

Basic Lua Scripting Help - How to leave triggers behind?


Recommended Posts

Posted (edited)

Help!

 

I am wondering if someone out there can help me with some bare essential lua syntax/logic.

 

I am trying to get away from having millions of simple triggers in my missions and would like to figure out how to write/read/delete basic variables in lua instead.

 

For instance, here is a basic template I would like be able to control in lua rather than triggers:

 

I label a trigger zone/area as "Cool Kids"

 

When a Blufor unit is in the zone, they are announced as a "Cool Kid" and keep the designation while they are there. When they are not in the zone, they are no longer a part of the "Cool Kid" designation.

 

I can kind of do this with triggers, but in reality it takes a half dozen individual triggers: "Unit enters zone, unit leaves zone for each unit. It would be much nicer to be able to label and reference these units in lua and get away from monotonous triggers, but the basic structure and syntax still baffles me. I am sure I could do a lot more in lua as well, such as count the number of "Cool Kids" in the zone, count the number of cool kids that left the zone, reference the number of "Cool Kids" that have died, or even make a message that says "Nerd Alert" if Opfor enters the zone. My end goal is to be able to count/reference/affect units in my missions.

 

I realize this is a silly example mission, but it is a much easier example than a full DCS mission with lots of different things going on. If someone could help me with this basic template I would really appreciate it. If I could get over this simple hump, I feel I could do much more with missions and triggers.

 

 

A basic picture of the "Cool Kid template":

 

http://s541.photobucket.com/user/TrueApothecary/media/CoolKids.jpg.html

attachment.php?attachmentid=18843&d=1443133362

Edited by NakedSquirrel

Modules: A10C, AV8, M2000C, AJS-37, MiG-21, MiG-19, MiG-15, F86F, F5E, F14A/B, F16C, F18C, P51, P47, Spitfire IX, Bf109K, Fw190-D, UH-1, Ka-50, SA342 Gazelle, Mi8, Christian Eagle II, CA, FC3

Posted

you best bet is to use a MIST function

 

http://wiki.hoggit.us/view/Units_in_zones

 

you would still place the trigger zone but feed it to the function

 

mist.flagFunc.units_in_zones{ units = {'Nameofunittocheck', '2ndunit', 'etc'}, zones = {'Cool Kids'}, flag = 100, zone_type = 'sphere' }

 

now when units are in the zone flag 100 equals true

 

if trigger.misc.getUserFlag(100) == true then

-- cool kids stuff here

end

 

pretty sure that should work (just not 100pct sure on how to check a boolean value) keep in mind you would need to make sure to load MIST and stuff prior to trying to use a MIST function. and welcome to the wonderful world of DCS LUA scripting.

Posted

Keep in mind I am using mist to shortcut some of the work.

 

updateCoolKids is a function that is run every second to keep track of all units that are designated as "coolkids". In this case I believe you mentioned that any blue coalition unit can be a coolkid, so thats all it checks for. It creates the coolKids table entries with the first "for loop".

 

The second for loop iterates known coolKids units and either does nothing, lists the unit as a lost kid, or adds the unit to deadKids and removes it from coolKids. Since tracking coolKids once they are dead isn't important I am simply adding to the deadKids table and remove those entries from coolKids. Man this example got dark quickly.

 

(((pos.x - zone.point.x)^2 + (pos.z - zone.point.z)^2)^0.5 <= zone.radius) is the check to see if a point is within a circle. It is basic trig, Pythagoras would by proud.

 

local coolKids = {}
local deadKids = {}
local lostKids = {}
local zone = trigger.misc.getZone('COOLKIDS')
local nerdAlert = false

local function updateCoolKids()
nerdAlert = false
for uName, uData in pairs(mist.DBs.unitsByName) do
	if Unit.getByName(uName) and Unit.getByName(uName):getPosition() then
		local unit = Unit.getByName(uName)
		local pos = unit:getPosition().p
		if (((pos.x - zone.point.x)^2 + (pos.z - zone.point.z)^2)^0.5 <= zone.radius) then
			if string.lower(uData.coalition) == 'blue' then
				coolKids[uName] = unit
			else
				nerdAlert = true
			end
			if lostKids[uName] then
				lostKids[uName] = nil
			end
		end
	end
end

for uName, _u in pairs(coolKids) do
	if Unit.getLife(_u) > 0 then
		if (((pos.x - zone.point.x)^2 + (pos.z - zone.point.z)^2)^0.5 > zone.radius) then
			lostKids[uName] = _u
		end
	else
		deadKids[#deadKids+1] = uName
		coolKids[uName] = nil
	end
end
end

local function displayCoolKidValues()
local msg = {}
msg[#msg+1] = 'Cool kids: ' .. #coolKids
msg[#msg+1] = '\nDead kids: ' .. #deadKids
msg[#msg+1] = '\nLost kids: ' .. #lostKids
msg[#msg+1] = '\nNerd Alert: ' .. tostring(nerdAlert)
trigger.action.outText(table.concat(msg), 20)
end

mist.scheduleFunction(updateCoolKids, {}, timer.getTime() + 1, 1)
mist.scheduleFunction(displayCoolKidValues, {}, timer.getTime() + 1, 15)

 

Reference this post for a lot of useful links you can use to learn more about lua.

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Posted

Thank you very much; this is gold.

 

Now to see if I can write a script!

Modules: A10C, AV8, M2000C, AJS-37, MiG-21, MiG-19, MiG-15, F86F, F5E, F14A/B, F16C, F18C, P51, P47, Spitfire IX, Bf109K, Fw190-D, UH-1, Ka-50, SA342 Gazelle, Mi8, Christian Eagle II, CA, FC3

  • Recently Browsing   0 members

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