Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Conditional Hooks
- $Application: FS2_Open
- $On Game Init:
- [
- function SwapClassTo(class, ship)
- if mn.Ships[ship]:isValid() and tb.ShipClasses[class]:isValid() then --always check validity of game objects!
- local thisShip = mn.Ships[ship] --Good form is to declare internal local objects
- local thisClass = tb.ShipClasses[class]
- local priWeapons = {} --Declaring primary and secondary weapon tables as an empty object for later population
- local priAmmo = {}
- local secWeapons = {}
- local secAmmo = {}
- --First let's figure out what weapons we have
- for i=1, #thisShip.PrimaryBanks do -- thisShip.PrimaryBanks has a # operator that returns the number of banks
- local thisBank = thisShip.PrimaryBanks[i]
- priWeapons[i] = thisBank.WeaponClass
- priAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax --Storing ammo counts as a percent of max ammo so if ammo capacity changes we're all good
- end
- for i=1, #thisShip.SecondaryBanks do -- SecondaryBanks too
- local thisBank = thisShip.SecondaryBanks[i]
- secWeapons[i] = thisBank.WeaponClass
- secAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax
- end
- --Now we switch the ship class
- thisShip.Class = thisClass
- --Now we swap back the weapon info
- for i=1, #thisShip.PrimaryBanks do
- local thisBank = thisShip.PrimaryBanks[i]
- if ( priWeapons[i] ~= nil ) then
- thisBank.WeaponClass = priWeapons[i]
- thisBank.AmmoLeft = priAmmo[i] * thisBank.AmmoMax
- end
- end
- for i=1, #thisShip.SecondaryBanks do
- local thisBank = thisShip.SecondaryBanks[i]
- if ( secWeapons[i] ~= nil ) then
- thisBank.WeaponClass = secWeapons[i]
- thisBank.AmmoLeft = secAmmo[i] * thisBank.AmmoMax
- end
- end
- end
- end
- ]
- #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement