FreeSpace Releases > Scripting Releases

Weapon hit detection script

(1/2) > >>

Goober5000:
This script was inspired by one that m!m originally created for Scroll.  I ended up almost entirely rewriting it as our needs expanded beyond the specific use case that m!m wrote it for.  With this script, you can monitor ships and subsystems for any hits by a particular class of weapon.
http://scroll.hard-light.net/downloads/weapon_hit-sct.tbm

There are two main functions to use:

WeaponHit:addHitDef(weapon_class_name, ship_name, subsys_name)
Call this function at the beginning of the mission add the "hit definition" consisting of the weapon class, an optional ship (or nil for all ships), and an optional subsystem (or nil for a hit anywhere on the ship).  The game will update these definitions whenever a weapon hit is detected.

WeaponHit:checkHit(variables, weapon_class_name, ship_name, subsys_name)
Call this function periodically to check for hits.  If any hit is detected, and you supply one or more variables, those variables will be populated with the results.  Possible variables are age, recording the age in seconds since a hit was detected, and x, y, and z, recording the world coordinates of the hit.


Example:


--- Code: ---$Formula: ( when
   ( true )
   ( script-eval-block
      "WeaponHit:addHitDef('TAG-B', "
      "'Arachnas', 'turret02')"
   )
)
+Name: Initial setup
+Repeat Count: 1
+Interval: 1
--- End code ---

Then:


--- Code: ---$Formula: ( when
   ( and
      ( perform-actions
         ( true )
         ( script-eval-block
            "WeaponHit:checkHit({age='hit-Ar"
            "achnas-turret02-age'}, 'TAG-B',"
            "'Arachnas', 'turret02')"
         )
      )
      ( = @hit-Arachnas-turret02-age[-1] 0 )
   )
   ( send-message
      "#Command"
      "High"
      "Tag successful"
   )
)
+Name: TAG successful
+Repeat Count: 1
+Interval: 1
--- End code ---



Woolie Wool:
This script crashes FSO in recent builds of FS2_Open whenever a ship targeted by the player is hit. Can it be updated?

Goober5000:

--- Quote from: Woolie Wool on January 26, 2019, 10:24:05 am ---This script crashes FSO in recent builds of FS2_Open whenever a ship targeted by the player is hit. Can it be updated?
--- End quote ---

It certainly can, but I'm having trouble reproducing the bug.  The crash was first reported by the Inferno team, and when I asked them for more information, they told me they hadn't been using the script recently.

The fact that it specifically crashes when a ship is hit that is targeted by the player is the first useful bit of information I've received so far, so thanks. :yes:  Would you be able to put together a simple mission that reliably reproduces the crash?

AdmiralRalwood:
If memory serves, I once had a crash with this while testing a mod and bizarrely got it to go away by changing hv.Weapon to hv.Object; since both are supposed to refer to the exact same thing, this was extremely confusing, and trying to debug Lua from the C++ code is exceedingly difficult.

Goober5000:
That's what I asked the Inferno people to try.  The problem is, of the places in the code where hv.Object is supplied but hv.Weapon isn't, none of them should be getting called for any use cases of the script.

Navigation

[0] Message Index

[#] Next page

Go to full version