Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Conditional Hooks
- $Application: FS2_Open
- $On Game Init:
- [
- function BeamHeal(ship) --We're expecting a ship type handle to be passed through
- if ship:isValid() then --Always check to see if these things are valid to be used!
- local healStep = ship.HitpointsMax * 0.002 --The heal rate is proportional to max health, so all ships heal to the max within the same amount of time. Alternatively healStep could be the damage of the weapon.
- --I'll leave that as a fun excercise for you to try! (Hint: you'll need an additional argument to get passed through in the function)
- if ship.HitpointsLeft <= ship.HitpointsMax then --It would be a good idea to only try to heal ships that have less than max health!
- --The ship in question will get its HP healed by healStep, so we should take what the end result would be after healing and make sure it won't go over the maximum amount of HP. If it doesn't we can just heal like normal, but if it does go over, we should just make it cap at max HP.
- --Mind you we could just heal and check afterwards if HP is over MaxHP, but it's probably better to ensure we touch ship properties as little as possible.
- if ship.HitpointsLeft + healStep <= ship.HitpointsMax then
- ship.HitpointsLeft = ship.HitpointsLeft + healStep
- else
- ship.HitpointsLeft = ship.HitpointsMax
- end
- end
- --In a ship's handle, there are all the normal attributes, but if we use [] indices on the ship handle, we get the subsystem handles.
- --So ship["communications"] should give us the communications subsystem. But does our ship have engine or engine01 and engine02?
- --Instead of explicitly going after the names of subsystem, we can just go through the whole thing with numerical indices.
- --for you people with some lua knowledge already, "for k,v in pairs()" won't work for these "virtual indexed" type of tables (they are slow for real time actions, so don't use them if you can avoid it!)
- --the best way is to go through these type of tables is to get the size of the table, assign it to a variable, then do a for loop through everything
- --real time looping should always be done with a numerical loop!
- local numSubsys = #ship --#ship is the number of subsystems on the ship that we should heal
- for i=1, numSubsys do
- --We'll do the same type of checks on HP here as we did with the main health.
- if ship[i].HitpointsLeft <= ship[i].HitpointsMax then
- if ship[i].HitpointsLeft + healStep <= ship[i].HitpointsMax then
- ship[i].HitpointsLeft = ship[i].HitpointsLeft + healStep
- else
- ship[i].HitpointsLeft = ship[i].HitpointsMax
- end
- end
- end
- end
- end
- ]
- $Weapon class: HealBeam#Tutorial
- $On Ship Collision:
- [
- --[[
- Here the the conditional hook is Weapon Class, and we've defined it to only look at the HealBeam#Tutorial class.
- We change the $Weapon class: to weapons to make other weapons heal.
- Our action is On Ship Collision, and one of the hook variables for that action is hv.Object, which is the Object that has been collided with. So this bit of code will only run with HealBeam#Tutorial collides with a ship!
- ]]--
- BeamHeal(hv.Object)
- ]
- #End
Advertisement
Add Comment
Please, Sign In to add comment