Modding Help How to Record When the Player Gets Hit

Discussion in 'Starbound Modding' started by PistolRcks, May 28, 2018.

  1. PistolRcks

    PistolRcks Void-Bound Voyager

    So, here's the preface—I'm making a tech that grants the player invulnerability (via status.addEphemeralEffect("invulnerable", args.dt [this is subject to change])), but I want it to take energy away when the player gets hit. Obviously, recording the amount of health from tick to tick to see if the player has taken damage can be thrown out of the picture, since the player will not be taking damage. I've been looking through the documentation to try and see what I can use, but I just can't find a solution. Is there a separate effect that I should use, or is there some way to do it? I'm open to suggestions.
     
  2. Cyel

    Cyel Scruffy Nerf-Herder

    You can access the status table from a tech, and with it you have status.damageTakenSince(), which gives you an object with a "healthLost" value and a "damageDealt" value, so I believe it'll work for you:
    (https://starbounder.org/Modding:Lua/Starbound_Datatypes#damageRequest)
    Code:
    #### `List<pair<DamageNotification>>`, `unsigned` status.damageTakenSince([`unsigned` since = 0]])
    
    Returns two values:
    * A list of damage notifications for the entity's damage taken since the specified heartbeat.
    * The most recent heartbeat to be passed into the function again to get the damage notifications taken since this function call.
    
    Example:
    
    ```lua
    _,lastStep = status.damageTakenSince() -- Returns the full buffer of damage notifications, throw this away, we only want the current step
    
    -- stuff
    
    notifications,lastStep = status.damageTakenSince(lastStep) -- Get the damage notifications since the last call, and update the heartbeat
    ```
    /stats/effects/thorns/thorns.lua is a good example of how to use it

    If it doesn't work, you can add a script to player_primary and wrap around applyDamageRequest(). A good example of it is the Memento Mori https://community.playstarbound.com/resources/memento-mori.4056/
    player.config.patch:
    Code:
      {
      "op": "add",
      "path": "/statusControllerSettings/primaryScriptSources/-",
      "value": "/mm_player_primary.lua"
      },
    mm_player_primary.lua:
    Code:
    require "/stats/player_primary.lua"
    require "/mementomori.lua"
    
    if not _mm_wrapped then
       _applyDamageRequest = applyDamageRequest
       _mm_wrapped = true
    end
    
    function applyDamageRequest(damageRequest)
       local r = _applyDamageRequest(damageRequest)
       sb.logInfo("r = %s", r)
       if next(r) ~= nil and r[1].hitType == "kill" then
         sb.logInfo("mementomori: R.I.P")
         world.setProperty(mementomori.deathPositionKey, mcontroller.position())
       end
       return r
    end
    

    Edit: the first one should work, actually, as applyDamage seems to be a listener for status.damageTakenSince from /scripts/status.lua
     
    Last edited: Jun 15, 2018
  3. PistolRcks

    PistolRcks Void-Bound Voyager

    Huh. I never knew about this before, so thanks for teaching me something. I'll get to work on it soon and will update you if it works or not. Thank you.
     

Share This Page