Page 1 of 1

refuel an air aircraft that enters an area

Posted: Mon Oct 25, 2021 1:37 pm
by orca
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.

RE: refuel an air aircraft that enters an area

Posted: Mon Oct 25, 2021 2:50 pm
by KLAB
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


RE: refuel an air aircraft that enters an area

Posted: Tue Nov 02, 2021 8:23 pm
by KnightHawk75
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.

Code: Select all

 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
 

Code: Select all

 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

Code: Select all

 	    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
 

Code: Select all

 --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)
 

RE: refuel an air aircraft that enters an area

Posted: Fri Nov 05, 2021 12:31 pm
by orca
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


RE: refuel an air aircraft that enters an area

Posted: Fri Nov 05, 2021 3:49 pm
by KnightHawk75
Need to actually call the function after defining it, as show in example usage.

See attached functional sample scene.

att: ShowAutomaticRefueling.zip