Page 1 of 1
Lua - add cargo?
Posted: Wed Apr 04, 2018 9:24 pm
by Gunner98
Am playing around with the cargo function a bit and am wondering if Lua can help.
Situation: various bases have small land elements that are meant to be transported to a central hub, from there some transport Helicopters will do the tactical air assault.
Some remote bases have C-130 or C-160 tactical transports available and this is fine: I put the cargo components on those bases, player decides what bits to transport and all is good.
For the locations that don't have a tactical transport, I'm using civilian charter - and they don't have the ability to use the COMANO cargo function: So I would like to set up a Lua event for when the civilian charter arrives at the assembly base, certain cargo elements get added to the base.
Can that be done? I don't see a Lua function for it but have been away a lot so may have missed it.
Alternatively, I suppose I should ask for the aircraft in question to have the cargo function added.
Thanks guys.
RE: Lua - add cargo?
Posted: Thu Apr 05, 2018 3:30 am
by michaelm75au
You should be able to. I added some functions to support cargo when it came out. I'll just have to refresh my memory[:D], and added some code on how it should work
RE: Lua - add cargo?
Posted: Thu Apr 05, 2018 3:46 am
by michaelm75au
You can transfer cargo from one unit to another; you supply a table of {guids from the unit components}, or { {DBID, number}} of the cargo to move
local u = ScenEdit_GetUnit({name='Uji', guid='7078b0f2-5dca-4930-a45d-5abe2d86a429'}) -- unit with the cargo
local c = u:filterOnComponent('cargo') -- the cargo contents
local ok = ScenEdit_TransferCargo(u.guid, 'galco #1', {{2,2918},{5,132}}) -- transfer items from 'Uji' to 'galco #1'
--
To just add the cargo to a unit, I'm not sure about. Will need to look at my notes. I would suggest SE_UpdateUnit() should do it but I 'cargo' isn't one of the items that can be added.
I suppose you could hide a base somewhere and transfer items from it. But that seems messy.
RE: Lua - add cargo?
Posted: Fri Apr 06, 2018 9:37 am
by Gunner98
Or I could use the base of origin for the flight. That would work. I'll give it a try this weekend.
Thanks
B
RE: Lua - add cargo?
Posted: Fri Apr 06, 2018 10:59 am
by michaelm75au
Yea, putting the initial cargo into the starting base would make sense. That way, you could see the cargo disappear and know how much was left.
ORIGINAL: Gunner98
Or I could use the base of origin for the flight. That would work. I'll give it a try this weekend.
Thanks
B
RE: Lua - add cargo?
Posted: Sat Apr 07, 2018 1:22 pm
by Gunner98
Thank you once again Michael
Worked like a charm. I simplified the command to:
ScenEdit_TransferCargo('Moroni Airport', 'Camp Lemonier Barracks 10', {{2,2884},{1,2513}}) -- transfer items from 'Moroni' to 'Djibouti Airport'
Some pondering:
I think I understand the reason for using GUID vice the name, have never done that but probably should.
What is the reason for going with 'local' commands? Is there a guide to using them?
Thanks
Cheers
B
RE: Lua - add cargo?
Posted: Sat Apr 07, 2018 3:05 pm
by TitaniumTrout
Using local keeps the variable just for that usage. So you could define
and then call it out in a different function a minute later and use the exact same variable. Local is just thta, for a local function.
I believe you can store a variable globally but it is not persistent. So you could define
and call it in a different function, but the moment you save and reload that variable is cleared and any reference to it would return null. You can save variables using the ScenEdit_GetKeyValue and ScenEdit_SetKeyValue.
http://commandlua.github.io/index.html# ... etKeyValue
Be aware that it saves the data as a
string. So if you define a key as "7", it is as a string and not an integer. So any operations you perform thinking it's an integer will not work. First you need to define it using the
function.
http://www.lua.org/manual/5.1/manual.html#pdf-tonumber
RE: Lua - add cargo?
Posted: Sat Apr 07, 2018 6:51 pm
by michaelm75au
ORIGINAL: Gunner98
Thank you once again Michael
Worked like a charm. I simplified the command to:
ScenEdit_TransferCargo('Moroni Airport', 'Camp Lemonier Barracks 10', {{2,2884},{1,2513}}) -- transfer items from 'Moroni' to 'Djibouti Airport'
Some pondering:
I think I understand the reason for using GUID vice the name, have never done that but probably should.
What is the reason for going with 'local' commands? Is there a guide to using them?
Thanks
Cheers
B
1. Use of GUID: I have tended to go with this for several reasons over the years, mainly because it is more specific to the actual item I want to affect. Using the 'name' is fine in most cases, but as the name can be changed by the player, this can break Lua scripts which rely on the original name only. As you get deeper into some items (like unit components), names no longer are unique so you need to rely on GUIDs.
2. Local variables: By default Lua treats all variables as 'global'. This means that they stay around (and use up memory) for the entire session. It also means that they can be used anywhere in your scripts.
Which is fine, but the risk is that you accidentally use the same variable in more than one location and the value gets changed from what you expected.
By making them 'local', they only exist while running the script/function that they are in. This also helps in you don't pickup 'old' global values that may have carried over from some other script.
RE: Lua - add cargo?
Posted: Sun Apr 08, 2018 7:27 am
by Gunner98
Thank you both. Will try and expand my limited scripting skill to include these two bits.
B
RE: Lua - add cargo?
Posted: Sun Apr 08, 2018 9:43 am
by Gunner98
OK, playing around with this an must be doing something wrong:
2018-04-08 8:34:22 AM -- B998.7 --
Console:
local o = ScenEdit_GetUnit ({name='Moroni Airport', guid='0d199a59-5c5c-4f50-ba71-78d88c261454'})
local d = ScenEdit_GetUnit ({name='Camp Lemonier Barracks 12', guid='13256382-bdc9-4a52-8543-4ddfd54a4bf8'})
local ScenEdit_TransferCargo({o.guid, d.guid, {8,2884},{1,2916},{4,357},{4,1973},{1,2590},{4, 851}}) -- transfer items from 'Moroni' to 'Djibouti Airport'
ERROR: [string "Console"]:3: syntax error near <eof>
Remembering that I'm an old dog and this is a new trick - its probably something very basic that is messing up.
Thanks
B
RE: Lua - add cargo?
Posted: Sun Apr 08, 2018 4:42 pm
by michaelm75au
local ScenEdit_TransferCargo( o.guid, d.guid, { {8,2884},{1,2916},{4,357},{4,1973},{1,2590},{4, 851} } )
RE: Lua - add cargo?
Posted: Sun Apr 15, 2018 2:30 pm
by Gunner98
Thanks Michael - working like a charm.
Cheers once again.
B
RE: Lua - add cargo?
Posted: Mon Apr 16, 2018 8:00 am
by michaelm75au
I am adding a SE_UpdateUnitCargo() function that will act like the SE_UpdateUnit() but allow cargo only. Cargo behaves differently to the sensor/mount/etc components so made sense to isolate it.
RE: Lua - add cargo?
Posted: Mon Apr 16, 2018 11:15 am
by Gunner98
That's a good idea.
Another thought - the documentation at
http://commandlua.github.io/ is excellent, not sure if you're managing that. I find the organization of the page very helpful but there are getting to be a lot of functions. It would be helpful I think for some definition, guidelines on the difference and usage between 'Set', 'Get', 'update' etc.
Perhaps its just an issue of not understanding the fundamentals of scripting, and I can work through it. Just thought with more and more functions becoming available with each update that a tweak to the organization might be helpful.
Thanks again for all your help. I did a bunch of scripting this weekend without a single plea for help - success... if it all works that is. [8|]
B
RE: Lua - add cargo?
Posted: Tue Apr 17, 2018 7:29 am
by michaelm75au
I am intending to start creating example files to attach to the wiki page ( eg
http://commandlua.github.io/beta/index.html ).[:D]
Just trying to determine how deep to get in to it. There are other locations that show how to do Lua Command stuff, this is more to show the commands are used and how with simple examples.
RE: Lua - add cargo?
Posted: Tue Apr 17, 2018 9:46 am
by Gunner98
Right.
I was thinking one liners such as:
SE_getxxx: used to ...
SE_updatexx: used to ...
No problem. Thanks
B