refuel an air aircraft that enters an area (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion



Message


orca -> refuel an air aircraft that enters an area (10/25/2021 4:37:43 PM)

I would like to create an event that refuels any aircraft that enters an area for a particular side.

I would assume I'd use a trigger for unit enters area that would run a script. But how can I know which unit enters the area so that I can refuel that particular aircraft?

I could use a variation of this refuel scrip but don't know how to make it work to get the appropriate unit.

u = ScenEdit_GetUnit({UNIT THAT ENTERS AREA OR SOMETHING LIKE THIS...})
if u ~= nil then
local newfuel = u.fuel
newfuel[2001].current = newfuel[2001].max
u.fuel = newfuel
end

Does anyone have suggestions? Thanks.




KLAB -> RE: refuel an air aircraft that enters an area (10/25/2021 5:50:07 PM)

Apologies If i was near a Pcomputer id find the script i used but off the cuff and copied from https://commandlua.github.io/#ScenEdit_UnitX


ScenEdit_UnitX () -- this grabs the unit that triggered the event such as entering the area and from there it's just a matter of adapting your existing script I suspect?

Activating Unit ..that triggered the current Event.Otherwise,a nilis returned.

Note that UnitX()can also be used as a shortcut for ScenEdit_UnitX()

Returns

UnitThe triggering unit

Example

ScenEdit_SetUnitDamage({side=UnitX().side, unitname=UnitX().name, fires=1, components={ {'rudder','Medium'}, {'type',type='sensor',1} } })

local unit = ScenEdit_UnitX()

Etc





KnightHawk75 -> RE: refuel an air aircraft that enters an area (11/2/2021 11:23:35 PM)

Think I've posted these before but here they are again:
You only need the first one in your trigger, and you can make the func local if it's the only place you'll be using it.
function khRefuelUnit(myunit)
    local newfuel = myunit.fuel
    for i,v in pairs(newfuel) do
      v.current = v.max
      myunit.fuel = newfuel
    end
    newfuel = nil;
end
function khRefuelSide(thesidename,unittype,subtype)
    if unittype ==nil then unittype="All" end;
    if subtype ==nil then subtype="All" end;
    local a = VP_GetSide({Side =thesidename}) 
    for i,v in pairs(a.units) do
        local myunit;
        myunit = ScenEdit_GetUnit({name=v.name, guid=v.guid})
        if myunit ~=nil then
	    if unittype ~="All" and subtype ~= "All" then
                if myunit.type == unittype and myunit.subtype == subtype then
                    khRefuelUnit(myunit);
                end
	    elseif unittype ~= "All" and subtype =="All" then
                if myunit.type == unittype then
                    khRefuelUnit(myunit);
                end
	    elseif unittype == "All" and subtype ~="All" then
                if myunit.subtype == subtype then
                    khRefuelUnit(myunit);
                end
	    else --either ==All or both ==ALL
                if myunit.type ~= "Weapon" then
               	    khRefuelUnit(myunit);
		end
	    end
        end
        myunit = nil;
    end
    local a = nil;
end
--usage examples
--single unit refueling, all tanks
khRefuelUnit(ScenEdit_UnitX()) -- used inside a trigger refuel the triggering unit.
khRefuelUnit(ScenEdit_GetUnit({guid="SomeValidUnitGuid"})) -- get unit first, feed unit wrapper to function.

-- Side version: Feed it the name of the side, it will refuel all units of unittype to max capacity.
khRefuelSide('Blue'); -everything.
khRefuelSide('Blue',"Aircraft"); --only aircraft
khRefuelSide('Blue',"Aircraft",3101) --only aircraft with subtype code 3101 (bomber)
khRefuelSide('Blue',"Weapon",2001) --only weapons with subtype guided weapons.  (Yes you can refuel missiles if you really want too, handy for debugging purposes)




orca -> RE: refuel an air aircraft that enters an area (11/5/2021 3:31:24 PM)

Thanks for the replies. I'm sorry I'm very bad at LUA and despite a lot of effort usually resort to copying and modifying others scripts and then trial and error to trouble shoot! Despite this I can't get it to work.

I have an event composed of a trigger of a unit aircraft entering the area and the action is this script. The trigger works but when the script runs the unit does not refuel. Can you see what I'm doing wrong?

Thanks

this is the LUA script in the action:

local myunit = ScenEdit_UnitX()

function khRefuelUnit(myunit)
local newfuel = myunit.fuel
for i,v in pairs(newfuel) do
v.current = v.max
myunit.fuel = newfuel
end
newfuel = nil;
end





KnightHawk75 -> RE: refuel an air aircraft that enters an area (11/5/2021 6:49:30 PM)

Need to actually call the function after defining it, as show in example usage.

See attached functional sample scene.

att: ShowAutomaticRefueling.zip




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.640625